0

I have an Ansible role that uses the variables: connection_name, connection_ip, etc. in order to create a connection.

In my hosts files, I could have numerous "connections" and have defined it as a dictionary of lists:

host_file:
hosts:
    host_one:
        domain: one
        ip: 172.18.1.1
        connection:
                - connection_name: two
                  connection_ip: 172.18.1.2

                - connection_index: three
                  local_hub_ip: 172.18.1.3

    host_two:
        domain: two
        ip: 172.18.1.2

    host_three:
        domain: three
        ip: 172.18.1.3

How do I pass in each "connection" object into my Ansible role so that it can use the nested variables "connection_name" and "connection_ip"? I have attempted using "with_items" and "with_dict" like so:

---
- name: Creating new connection
  hosts: "{{ host_name }}"
  gather_facts: no
  become: yes
  remote_user: "{{ user | default('root')}}"
  tasks:
      - include_role:
            name: connection-create
         with_items: "{{connection}}"

But I get a "FAILED: connection_name is undefined" because it passes in the object as {"connection_name": two, "connection_ip": 172.18.1.2}, instead of the inner portion, "connection_name": two, "connection_ip": 172.18.1.2 without the {}.

hotcheetos
  • 29
  • 1
  • 6

1 Answers1

0

In your case, connection is a list of dictionnaries,

so use item.connection_name and item.connection_ip in your task.

Take a look at the official doc on playbook loops

sebthebert
  • 12,196
  • 2
  • 26
  • 37