Nested loop in ansible. product undefined

Viewed 76

I have this simple playbook:

---
- hosts: all
  become: yes
  vars:
    my_hosts:
      - 192.168.0.1
      - 192.168.0.2
      - 192.168.0.3

  tasks:

    - name: Check ports
      wait_for:
        port: "{{ item.1 }}"
        host: "{{ item.0 }}"
        timeout: 10
      loop: "{{ product(my_hosts) | product([443, 80443]) | list }}"

When I run it like so ...

$ ansible-playbook -i,192.168.2.2 run_wait_for.yml

... I get this error ...

fatal: [192.168.2.2]: FAILED! => {"msg": "'product' is undefined"}

What am I doing wrong?

2 Answers

Fix the syntax

      loop: "{{ my_hosts | product([443, 80443]) }}"

For example
    - debug:
        msg: "Check host: {{ item.0 }} port: {{ item.1 }}"
      loop: "{{ my_hosts|product([443, 80443]) }}"

gives (abridged)

  msg: 'Check host: 192.168.0.1 port: 443'
  msg: 'Check host: 192.168.0.1 port: 80443'
  msg: 'Check host: 192.168.0.2 port: 443'
  msg: 'Check host: 192.168.0.2 port: 80443'
  msg: 'Check host: 192.168.0.3 port: 443'
  msg: 'Check host: 192.168.0.3 port: 80443'

I've Ansible 2.9 and this worked to me:

---
- name: Playbook for Check Ports
  hosts: localhost
  connection: local
  gather_facts: no

  vars:
    my_hosts:
      - 192.168.0.1
      - 192.168.0.2
      - 192.168.0.3
    ports:
      - 443
      - 80443

  tasks:

    - name: Check Ports.
      debug:
        msg: "Check host: {{ item[0] }} port: {{ item[1] }}"
      loop: "{{ my_hosts | product(ports) | list }}"
...

Related