# Ansible skip  depending on output in a loop

**URL:** <https://forum.ansible.com/t/ansible-skip-depending-on-output-in-a-loop/38226>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [July 14, 2023, 10:29am UTC](https://forum.ansible.com/t/ansible-skip-depending-on-output-in-a-loop/38226 "2023-07-14T10:29:37Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![IbraSami](https://avatars.discourse-cdn.com/v4/letter/i/838e76/32.png) [@IbraSami](https://forum.ansible.com/u/IbraSami)\
**Post date:** [July 14, 2023, 10:29am UTC](https://forum.ansible.com/t/ansible-skip-depending-on-output-in-a-loop/38226/1 "2023-07-14T10:29:37Z")

</div>

Ansible skip depending on output in a loop

i am trying to create dhcp scope only if it does not exists, below is what i have tried:

- name: Get scopes  
ansible.windows.win\_shell: Get-DhcpServerv4Scope -ScopeId {{ item.ScopeId }}  
changed\_when: false  
register: dhcp  
with\_items: “{{ dhcp\_scope}}”

vars dhcp\_scope:

“EndRange”: “172.30.0.254”,  
“LeaseDuration”: “10:00:00”,  
“ScopeId”: “172.30.0.0”,  
“StartRange”: “172.30.0.1”,  
“SubnetMask”: “255.255.255.0”,  
“name”: “Office B”

dhcp register

“msg”: {  
“changed”: false,  
“msg”: “All items completed”,  
“results”: [  
{  
“ansible\_loop\_var”: “item”,  
“changed”: false,  
“cmd”: “Get-DhcpServerv4Scope -ScopeId 172.30.0.0”,  
“delta”: “0:00:01.528935”,  
“end”: “2023-07-13 21:47:35.792188”,  
“failed”: false,  
“item”: {  
“EndRange”: “172.30.0.254”,  
“LeaseDuration”: “10:00:00”,  
“ScopeId”: “172.30.0.0”,  
“StartRange”: “172.30.0.1”,  
“SubnetMask”: “255.255.255.0”,  
“name”: “Office B”  
},  
“rc”: 0,  
“start”: “2023-07-13 21:47:34.263252”,  
“stderr”: “”,  
“stderr\_lines”: ,  
“stdout”: “\r\nScopeId SubnetMask Name State StartRange EndRange LeaseDurat\r\n ion \r\n------- ---------- ---- ----- ---------- -------- ----------\r\n172.30.0.0 255.255.255.0 Office B Active 172.30.0.1 172.30.0.254 10:00:00 \r\n\r\n\r\n”,  
“stdout\_lines”: [  
“”,  
“ScopeId SubnetMask Name State StartRange EndRange LeaseDurat”,  
" ion ",  
“------- ---------- ---- ----- ---------- -------- ----------”,  
"172.30.0.0 255.255.255.0 Office B Active 172.30.0.1 172.30.0.254 10:00:00 ",  
“”,  
“”  
]  
},  
{  
“ansible\_loop\_var”: “item”,  
“changed”: false,  
“cmd”: “Get-DhcpServerv4Scope -ScopeId 172.31.0.0”,  
“delta”: “0:00:01.477053”,  
“end”: “2023-07-13 21:47:39.577233”,  
“failed”: false,  
“item”: {  
“EndRange”: “172.31.0.254”,  
“LeaseDuration”: “10:00:00”,  
“ScopeId”: “172.31.0.0”,  
“StartRange”: “172.31.0.1”,  
“SubnetMask”: “255.255.255.0”,  
“name”: “Office C”  
},  
“rc”: 0,  
“start”: “2023-07-13 21:47:38.100180”,  
“stderr”: “”,  
“stderr\_lines”: ,  
“stdout”: “\r\nScopeId SubnetMask Name State StartRange EndRange LeaseDurat\r\n ion \r\n------- ---------- ---- ----- ---------- -------- ----------\r\n172.31.0.0 255.255.255.0 Office C Active 172.31.0.1 172.31.0.254 8.00:00:00\r\n\r\n\r\n”,  
“stdout\_lines”: [  
“”,  
“ScopeId SubnetMask Name State StartRange EndRange LeaseDurat”,  
" ion ",  
“------- ---------- ---- ----- ---------- -------- ----------”,  
“172.31.0.0 255.255.255.0 Office C Active 172.31.0.1 172.31.0.254 8.00:00:00”,  
“”,  
“”

This is working, it is skipping the rc=0

- name: "Evaluate the scopes  
debug:  
when: “item.rc == 1”  
with\_items: “{{ dhcp}}”  
loop: “{{ dhcp.results }}”

but trying to apply it to create the scopes

- name: Create DHCP scopes  
win\_shell: |  
Add-DhcpServerv4Scope -Name “{{ item.name }}” -StartRange “{{ item.StartRange}}” -EndRange “{{ item.EndRange}}” -SubnetMask “{{ item.SubnetMask}}”  
with\_items: “{{ dhcp\_scope}}”  
changed\_when: false  
when: “item.rc == 1”  
loop: “{{ dhcp.results }}”

It is failing, due to the fact that is not getting the ScopeId, which is normal as the loop is going through the dhcp.results and not with\_items: “{{ dhcp\_scope}}” , how to make it so that it can skip the output that is rc=0 and only when item.rc == 1 ?

TASK [Create DHCP scopes] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* fatal: : FAILED! =\> {“msg”: “The task includes an option with an undefined variable. The error was: ‘dict object’ has no attribute ‘ScopeId’\n\nThe error appears to be in ‘/etc/ansible/dhcp\_check.yml’: line 57, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Create DHCP scopes\n ^ here\n”}

if i change it to:

- name: Create DHCP scopes  
with\_items: “{{ dhcp}}”  
changed\_when: false  
when: “item.rc == 1”  
loop: “{{ dhcp.results }}”  
ASK [Create DHCP scopes] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

FAILED! =\> {“msg”: “The task includes an option with an undefined variable. The error was: ‘dict object’ has no attribute ‘ScopeId’\n\nThe error appears to be in ‘/etc/ansible/dhcp\_check.yml’: line 57, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Create DHCP scopes\n ^ here\n”}.

How to make - name: Create DHCP scopes so that it can skip creating the scopes if rc=0 and only when item.rc == 1 ? similar with the debug which skips the scopes that exist?

i cant loop over dhcp\_scope it will throw an error: error FAILED! =\> {“msg”: "The conditional check ‘item.rc == 1’ failed. The error was: error while evaluating conditional (item.rc == 1).

What i am trying to achieve is run command on the remote host, if the dhcp scope exist i will skip creating it and only creates the one that do not exists.
