jack liu
jack liu

Reputation: 39

I tried to use "with_items" inside when statement but failed

- hosts: 22rrvgndns01
  gather_facts: no
  vars_files:
    - /etc/ansible/dnschange/dns_resource_record.yml
  tasks:
- shell: grep "{{item.name}}" check_result.txt
    args:
      chdir: /cluster/dnschange
    when: "{{item.action}}" is match("delete")
    with_items: "{{resource_record}}"

Here is the resource_record:

- resource_record:
    - name: test.201.apn.epc.mnc002.mcc505.3gppnetwork.org
      record_type: naptr
      action: create
      view: MME
      ttl: 300
      order: 100
      preference: 999
      flags: s
      service: x-3gpp-pgw:x-gn:x-gp:x-s5-gtp
      replacement: wip-ows-pgw-e-NSW.node.epc.mnc002.mcc505.3gppnetwork.org

I got the error when I executed the script

The offending line appears to be:

chdir: /cluster/dnschange
when: "{{item.action}}" is match("delete")
                        ^ here

I 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 }}"

Can anyone help me out?

Upvotes: 1

Views: 176

Answers (1)

Smily
Smily

Reputation: 2568

I guess your indentation is wrong and change in when condition. Can you try as below

- shell: grep "{{item.name}}" check_result.txt
  args:
    chdir: /cluster/dnschange
  when: item.action == 'delete'
  with_items: "{{resource_record}}"

Upvotes: 1

Related Questions