0

I need help to understand this issue : so I'm using ansible and basically I have a vault file that containe 3vvariables and in my role I have this section that is supposed to copy each variable of the vault to 3 different files :

- name: copy needed certificate
  shell: echo {{ client_kafka_rsyslog_cer }} >> {{ dest_cert_file }}
  shell: echo {{ client_kafka_rsyslog_key }} >> {{ dest_key_file }}
  shell: echo {{ client_kafka_rsyslog_sdr_authority_cer }} >> {{ dest_ca_file }}

so client_kafka_* are the variables in the vault files, and I should have 3 files outputed with each containing the data of the variables, but I get only one file and it is always the last one for exemple here i get the dest_ca_file and it containes the correct value, but the two others are not created at all. if I comment this last line like this :

- name: copy needed certificate
  shell: echo {{ client_kafka_rsyslog_cer }} >> {{ dest_cert_file }}
  shell: echo {{ client_kafka_rsyslog_key }} >> {{ dest_key_file }}
  #shell: echo {{ client_kafka_rsyslog_sdr_authority_cer }} >> {{ dest_ca_file }}

I get the dest_key_file with it's correct value, can someone explain me why it is only the last echo that is applied ? and how can i correct it.

thank you for your help.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Ait Zaid
  • 129
  • 12
  • 2
    Use the `copy` module with its [`content`](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/copy_module.html#parameter-content) attribute. – Zeitounator May 17 '21 at 16:39
  • Yes ^ You should avoid shell or command Ansible tasks whenever possible – OneCricketeer May 18 '21 at 13:36
  • thank you but copy can't copy a variable into a file or I'm I wrong ? to me copy needs the src: (a file) and dest: (another file) but what i want is to copy the content of a file – Ait Zaid May 19 '21 at 10:04

2 Answers2

2

You should do it in a loop, you are trying to execute 3 times the module shell inside a single task, another option is to execute it on a multi line shell with the pipe operand:

- name: copy needed certificate
  shell: |
    echo {{ client_kafka_rsyslog_cer }} >> {{ dest_cert_file }}
    echo {{ client_kafka_rsyslog_key }} >> {{ dest_key_file }}
    echo {{ client_kafka_rsyslog_sdr_authority_cer }} >> {{ dest_ca_file }}

Yaml Syntax

ikora
  • 772
  • 3
  • 16
0

I found an answer here :

ansible : how to pass multiple commands so what i should have written is this :

- name: copy needed certificate
  shell: |
    echo {{ client_kafka_rsyslog_cer }} >> {{ dest_cert_file }}
    echo {{ client_kafka_rsyslog_key }} >> {{ dest_key_file }}
    echo {{ client_kafka_rsyslog_sdr_authority_cer }} >> {{ dest_ca_file }}

but still don't understood why you can't put multiple shell modules under the same name.

Ait Zaid
  • 129
  • 12
  • 2
    You cannot do it because YAML is NOT a programming language. It was originally a markup language (Yet Another Markup Language). So each successive `shell:` actually overrides the previous one. – Jack May 17 '21 at 14:51