List and Dict combined

21 views
Skip to first unread message

Vishal Chainani

unread,
Jul 5, 2015, 4:10:00 AM7/5/15
to ansible...@googlegroups.com
Hi,

Can a list have a combination of dict and lists? Below is my data and jinja2 template, while running the play book get an error.

snmp:
      location: xyz
      contact: admin
    - community:
        name: public
        access: read-only
        clients:
          - hosts: 8.8.8.8
          - hosts: 10.10.10.10
    - community:
        name: private
        access: read-write
        clients:
          - hosts: 3.3.3.3
          - hosts: 4.4.4.4

snmp {
    location "{{ snmp.location }}"";
    contact "{{ snmp.contact }}";"
    {% for snmp in snmp %}
    community {{ snmp.community.name }} {
        authorization {{ snmp.community.access }};
        clients {
            {% for client in snmp.community.clients %}
            {{ client.hosts }};
            {% endfor %}
        }
    }
    {% endfor %}
}

(ansible)Vishals-MBP:ansible vishalc$ ansible-playbook main.yml
ERROR: Syntax Error while loading YAML script, /etc/ansible/group_vars/lab.yml
Note: The error may actually appear before this position: line 27, column 5

      contact: admin
    - community:
    ^


Vishal

ja...@ioctl.org

unread,
Jul 5, 2015, 7:40:18 AM7/5/15
to ansible...@googlegroups.com
Not quite, but you can have a dictionary member whose value is a list:

snmp:
location: xyz
contact: admin
community:
- name: public
access: read-only
clients:
- hosts: 8.8.8.8
- hosts: 10.10.10.10
- name: private
access: read-write
clients:
- hosts: 3.3.3.3
- hosts: 4.4.4.4

And here, each element of the 'community' list is a dict.
> --
> 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.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/17d716b2-0180-45a1-8c49-f61e6ef88ecd%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

--
ja...@ioctl.org http://ioctl.org/jan/ Short, dark, ugly: pick any three
The only certain way to prevent another 9/11 is via universal calendar reform.

Vishal Chainani

unread,
Jul 5, 2015, 9:31:09 AM7/5/15
to ansible...@googlegroups.com
That works, thanks!
Reply all
Reply to author
Forward
0 new messages