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.

3
  • 2
    Use the copy module with its content attribute. Commented May 17, 2021 at 16:39
  • Yes ^ You should avoid shell or command Ansible tasks whenever possible Commented May 18, 2021 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 Commented May 19, 2021 at 10:04

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.