Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concat multiple variables and strings in ansible playbook

I'm trying multiple concatenation when preforming with_items for the destination section.

Right now it looks like this:

- name: create app except+lookup
  copy: content="" dest="{{ dir.comp ~ '/config/con2dd/' ~ item.name ~ 'File.txt' }}" force=no group=devops owner=devops mode: 0755
  with_items:
...

I get:

We could be wrong, but this one looks like it might be an issue with
missing quotes.  Always quote template expression brackets when they 
start a value. For instance:            

    with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - "{{ foo }}"

Tried couple of approaches but none resulted something that's working.

Is it possible to concat the variables with the strings?

like image 615
Moshe Avatar asked Jul 30 '17 14:07

Moshe


People also ask

How do you combine variables and strings?

In JavaScript, we can assign strings to a variable and use concatenation to combine the variable to another string. To concatenate a string, you add a plus sign+ between the strings or string variables you want to connect. let myPet = 'seahorse'; console.

How do you pass multiple vars in ansible?

To pass a value to nodes, use the --extra-vars or -e option while running the Ansible playbook, as seen below. This ensures you avoid accidental running of the playbook against hardcoded hosts.

How do you pass variables in ansible playbook?

The easiest way to pass Pass Variables value to Ansible Playbook in the command line is using the extra variables parameter of the “ansible-playbook” command. This is very useful to combine your Ansible Playbook with some pre-existent automation or script.


2 Answers

Don't mix pure YAML and key=value syntaxes for parameters. And always use YAML syntax for complex arguments:

- name: create app except+lookup
  copy:
    content: ""
    dest: "{{ dir.comp }}/config/con2dd/{{ item.name }}File.txt"
    force: no
    group: devops
    owner: devops
    mode: 0755
  with_items:
  ...
like image 51
Konstantin Suvorov Avatar answered Oct 04 '22 02:10

Konstantin Suvorov


You are not quoting the value associated with the key copy. For that to happen the first character has to be double (or single) quote. The example, given in the feedback, does this correctly, but is not explicit about it. Once a scalar start with a non-quote (yours start with the c from content quotes occurring in the scalar will no longer have a special meaning.

Because of a bug in the parser that Ansible uses, the : (colon space) in that scalar (mode: 0755) causes trouble, you should double quote the whole scalar and escape the double quotes that occur within it:

copy: "content=\"\" dest=\"{{ dir.comp ~ '/config/con2dd/' ~ item.name ~ 'File.txt' }}\" force=no group=devops owner=devops mode: 0755"

or alternatively use single quotes (which have different escape rules:

copy: 'content="" dest="{{ dir.comp ~ ''/config/con2dd/'' ~ item.name ~ ''File.txt'' }}" force=no group=devops owner=devops mode: 0755'

You can test scalars yourself on this online YAML parser, it has the same bug as what causes Ansible not to parse your YAML correctly.

This parser, handles the in scalar : correctly and will not produce an error with your input (but it has other problems).

like image 35
Anthon Avatar answered Oct 04 '22 00:10

Anthon