Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

ansible How escape comma while passing as a list

I have a play where i need to use a loop to iterate through items in it but the problem is the list items are having , in a string values such as ENT0005, bay 11 and ENT0005, bay 11 which i'm not able to get rid of.

My play:

- name: Create server profile
  oneview_server_profile:
    config: "{{ config }}"
    data:
      serverProfileTemplateName: "{{ server_profile_template }}"
      serverHardwareName: "{{ item }}"
    loop:
        - "ENT0005, bay 11"
        - "ENT0005, bay 12"
      name: "{{ server_profile }}"

My trial:

This does not work as well..

      serverHardwareName: "{{ item }}"
    loop:
        - "(ENT0005, bay 11)"
        - "(ENT0005, bay 12)"

Please suggest.

EDIT:

---
- hosts: localhost
  gather_facts: no

  vars:
    config: "{{ playbook_dir }}/{{ config_file }}"
    contents: "{{lookup('file', config)}}"
    server_profile_template: 'Test_apc_SY 480 Gen10 2 NVMe Application Template 20190601 V1.0'
    server_hardware: "SY 480 Gen9 1"
    #server_hardware_name: "ENT0005, bay 11" "ENT0005, bay 12"
    server_profile: "apc_SY 480 Gen10 2 NVMe Application Template 20190601 V1.0"
    template_name: []
    server_list: []

  tasks:

## Apply the server profile templates

        - name: Create server profile
          oneview_server_profile:
            config: "{{ config }}"
            data:
              serverProfileTemplateName: "{{ server_profile_template }}"
              serverHardwareName: "{{ item }}"
              loop:
                - "ENT0005, bay 11"
                - "ENT0005, bay 12"
              name: "{{ server_profile }}"
            params:
              force: True
          delegate_to: localhost
          register: result
        - debug: msg= "{{ result.msg }}"
        - debug: msg= "{{ result }}"
    
    ...

ERROR

fatal: [localhost]: FAILED! => {
    "msg": "The task includes an option with an undefined variable. The error was: 'item' is undefined

The error appears to be in 'serverProfile.yml': line 19, column 7, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


    - name: Create server profile
      ^ here
"
}

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

1 Reply

0 votes
by (71.8m points)

I don't know how many different ways we can say that the loop directive "should be at the same level as the module name". We mean:

- name: Create server profile
  oneview_server_profile:
    config: "{{ config }}"
    data:
      serverProfileTemplateName: "{{ server_profile_template }}"
      serverHardwareName: "{{ item }}"
      name: "{{ server_profile }}"
    params:
      force: true
  loop:
    - "ENT0005, bay 11"
    - "ENT0005, bay 12"
  delegate_to: localhost
  register: result
- debug: msg= "{{ result }}"

See how loop is aligned with oneview_server_profile? That means that these are both parameters of the task. Anything indented under oneview_server_profile is going to be an argument to the module itself (which doesn't know what to do with the loop directive).

Also note that it doesn't make sense to order your debug tasks like this:

- debug: msg= "{{ result.msg }}"
- debug: msg= "{{ result }}"

If result doesn't have a msg attribute, the first task will fail and you'll never see the contents of the result variable. Always order them less specific -> more specific:

- debug: msg= "{{ result }}"
- debug: msg= "{{ result.msg }}"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...