Ansible skip depending on output in a loop

53 views
Skip to first unread message

IbraSami

unread,
Jul 17, 2023, 9:40:10 AM7/17/23
to Ansible Project
Ansible skip  depending on output in a loop

Can someone please assist,   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.

IbraSami

unread,
Jul 17, 2023, 9:40:38 AM7/17/23
to Ansible Project
Ansible skip  depending on output in a loop


IbraSami

unread,
Jul 17, 2023, 9:40:40 AM7/17/23
to Ansible Project
Reply all
Reply to author
Forward
0 new messages