Need to filter with_items values based on specific string

12 views
Skip to first unread message

Pandu jh

unread,
Nov 25, 2019, 8:42:35 AM11/25/19
to Ansible Project

I need to reject "none" value item in ping test task. It has to take only "non- none" values from the with_items in the task.
 Please help me to fix this.

      - set_fact:
          linux_ip: "{{ item.stdout_lines[0] }}"
          unix_ip: "{{ item.stdout_lines[1] | default('none') }}"
        loop: "{{ ip_address_api.results }}"

      - debug:  var=unix_ip
      - debug:  var=linux_ip


      - name: Ping Test
        win_shell:  |
            if (Test-Connection {{  item  }} -Quiet -Count 2) {
            write-host "Ping - Successful"
            }
            else {
            Write-Host "Ping - Failed"
            }
        register: ping_check
        ignore_errors:  yes
        with_items:
          - "{{ unix_ip  }}"
          - "{{ linux_ip }}"
      - debug: var=ping_check

I tired with reject filter but it did not work.

      - name: Ping Test
        win_shell:  |
            if (Test-Connection {{  item  }} -Quiet -Count 2) {
            write-host "Ping - Successful"
            }
            else {
            Write-Host "Ping - Failed"
            }
        register: ping_check
        ignore_errors:  yes
        with_items:
          - "{{  unix_ip | reject('match','none) | list  }}"
          - "{{ linux_ip   }}"
      - debug: var=ping_check


Stefan Hornburg (Racke)

unread,
Nov 25, 2019, 8:53:44 AM11/25/19
to ansible...@googlegroups.com
On 11/25/19 2:42 PM, Pandu jh wrote:
>
> I need to reject "none" value item in ping test task. It has to take only "non- none" values from the with_items in the
> task.
>  Please help me to fix this.
>
>       - set_fact:
>           linux_ip: "{{ item.stdout_lines[0] }}"
>           unix_ip: "{{ item.stdout_lines[1] | default('none') }}"
>         loop: "{{ ip_address_api.results }}"
>
>       - debug:  var=unix_ip
>       - debug:  var=linux_ip
>
>
>       - name: Ping Test
>         win_shell:  |
>             if (Test-Connection {{  item  }} -Quiet -Count 2) {
>             write-host "Ping - Successful"
>             }
>             else {
>             Write-Host "Ping - Failed"
>             }
>         register: ping_check
>         ignore_errors:  yes
>         with_items:
>           - "{{ unix_ip  }}"
>           - "{{ linux_ip }}"
>       - debug: var=ping_check

I can imagine various solutions to the problem, but

when: "item != 'none'"

should do the trick.

Regards
Racke

>
> I tired with reject filter but it did not work.
>
>       - name: Ping Test
>         win_shell:  |
>             if (Test-Connection {{  item  }} -Quiet -Count 2) {
>             write-host "Ping - Successful"
>             }
>             else {
>             Write-Host "Ping - Failed"
>             }
>         register: ping_check
>         ignore_errors:  yes
>         with_items:
>           - "{{  unix_ip | reject('match','none) | list  }}"
>           - "{{ linux_ip   }}"
>       - debug: var=ping_check
>
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/1f050f15-5ff1-456e-8726-16a316578dd4%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/1f050f15-5ff1-456e-8726-16a316578dd4%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

Pandu jh

unread,
Nov 25, 2019, 8:59:56 AM11/25/19
to Ansible Project
It worked. Thanks, much !!
Reply all
Reply to author
Forward
0 new messages