Reputation: 49
I am trying to create one task based on the number of elements in a key.
my input will like below as json input
"srcaddr": "IP_192.168.10.10, IP_192.168.10.12"
or in array of json
"srcaddr": ['IP_192.168.10.10', 'IP_192.168.10.12']
- name: ADD IPv4 IP ADDRESS GROUP
chkr_fwobj_address:
ipv4: "group"
group_name: "ansibleIPv4Group1"
group_members: "{{ srcaddr }}"
adom: "{{ adom }}"
when: "{{ srcaddr |length > 1}}"
I want to execute the above task when the count of the element is more than one, of it is more that one the task will create a group and add the members in to the group. if we have only one element then group creation task will be ignored
Upvotes: 0
Views: 2379
Reputation: 68144
when condition should not be expanded. Correct syntax is
when: srcaddr|length > 1
The play below
- hosts: localhost
vars:
srcaddr1: ['IP_192.168.10.10']
srcaddr2: ['IP_192.168.10.10', 'IP_192.168.10.12']
tasks:
- debug:
msg: There is more then 1 address in the list srcaddr1.
when: srcaddr1|length > 1
- debug:
msg: There is more then 1 address in the list srcaddr2.
when: srcaddr2|length > 1
gives:
PLAY [localhost] *******************************************************
TASK [debug] ***********************************************************
skipping: [localhost]
TASK [debug] **********************************************************
ok: [localhost] => {
"msg": "There is more than 1 address in the list srcaddr2."
}
PLAY RECAP *************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0
Note: The length of srcaddr: 'IP_192.168.10.10'
is the length of the string. The length of srcaddr: [ 'IP_192.168.10.10' ]
is the length of the list.
Upvotes: 1