Concatenated multiple variables and strings in an inaccessible playbook

I am trying to do multiple preprocessing concatenations with_items

for a destination section.

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 a couple of approaches but none of them led to work.

Is it possible to concatenate variables with strings?

+3


source to share


2 answers


Do not mix pure YAML and key = value syntax 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:
  ...

      

+2


source


You are not specifying the value associated with the key copy

. To do this, the first character must be a double (or single) quote. The example given in the feedback does this correctly, but is not explicit. Once a scalar starts with some (the beginning with quotes c

from content

, found in a scalar, it will no longer have a special meaning.

Due to a bug in the parser that Ansible uses, the :

(column space) in this scalar ( mode: 0755

) is causing problems, you should specify the entire scalar twice and avoid the double quotes that occur inside 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 evacuation rules:



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

      

You can test the scalars yourself on this> online YAML parser, it has the same error as the cause Cannot parse your YAML correctly.

This parser handles scalar correctly :

and will not throw errors with your input (but it has other problems).

+1


source







All Articles