Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

ansible - yaml syntactical error with_items loop with a list

trying to loop with a list to create a append a yaml file; getting some syntactical error ? any idea how we can fix this ?

- name: "append yml"
  shell: echo "instances:

   - name: all
   - command: all
   - arguments:
     - cluster_name: mongo{{ item }} all
     - host: {{ipv4}}
" >> /etc/newrelic-infra/integrations.d/postgresql-config.yml
  shell: echo "     - auth_source: admin
     - ssl: false
     - ssl_insecure_skip_verify: false
    - labels:
     - env: env
" >> /tmp/newrelic-infra/integrations.d/postgresql-config.yml
  with_items: "{{ instances.stdout_lines }}"

ERROR! Syntax Error while loading YAML.

The error appears to have been in '/ansible/projects/newrelic/mongo_nr.yml': line 27, column 47, but may be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

    - name: "update yml"
      shell: echo "instances:

   - name: all
   - command: all
   - arguments:
     - cluster_name: mongo{{ item }} all
     - host: {{ipv4}}
" >> /etc/newrelic-infra/integrations.d/postgresql-config.yml
                                          ^ here

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 }}"
question from:https://stackoverflow.com/questions/65923957/yaml-syntactical-error-with-items-loop-with-a-list

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

looks like it's something similar to @Bharath Achar's suggestion, put single quotes around the : inside of the echo.

Also, are you certain two shell commands will work inside of the single task? I would make two tasks, like:

- name: "append 1st yml"
  shell: echo 'first part'
  with_items: "{{ my_list }}"

- name: "append 2nd yml"
  shell: echo 'second part'
  with_items: "{{ my_list }}"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...