nesting dictionary vars in vars

68 views
Skip to first unread message

Colin Byrne

unread,
Dec 29, 2015, 7:01:40 PM12/29/15
to Ansible Project
Hey so I am trying to loop through a dictionary list of servers I want to create, and want to evaluate some variables based on the iteration I am on. I am not sure if I has a syntax error or I am trying something not supported by ansible, or if it's caused by the dictionary not being evaluated at that point yet:

---

      - name: Launch instances based on server list

        ec2:

          key_name: "{{ server_type[item.key].key_name }}"

          group_id: "{{ server_type[item.key].security_group }}"

          instance_type: "{{ server_type[item.key].instance_type }}"

          image: "{{ server_type[item.key].image  }}"

          wait: false

          region: "{{ server_type[item.key].region }}"

          instance_tags:

            Type: "{{ item.key }}"

            Name: integ-{{ item.value.name }}

          exact_count: 1 #dont change!

          count_tag:

            Name: integ-{{ item.value.name }}

          vpc_subnet_id: subnet=blah

          assign_public_ip: no

        register: ec2

        with_dict: "{{ servers }}"


Then I have a dictionary and another list in my /role/vars/main.yml

---


  server_type:

      integ:

        security_groups: ['sg-81398ee4', 'sg-a6398ec3']

        image: ami-xxxxxx

        instance_type: t2.medium #needs to match the base image size

        region: us-west-1

        key_name: joeypeters

      production:

      mysql:


  servers:

    integ:

      name: joey

#     name: bob

    production:

    mysql:


...



I get the following error:


fatal: [localhost] => One or more undefined variables: 'None' has no attribute 'instance_type'


FATAL: all hosts have already failed -- aborting


I am up for structuring it entirely different if necessary.


Thanks a lot for the help.

chris meyers

unread,
Dec 31, 2015, 11:53:02 AM12/31/15
to Ansible Project
Colin,

The logic and code looks good to me. Maybe there is a spelling or similar simple error. Can you post everything? Also, can you get more specific with the error (i.e. what iteration it's failing on).

Colin Byrne

unread,
Jan 4, 2016, 1:00:24 PM1/4/16
to Ansible Project
Hey, sorry for the poor response time. It ended up being on the 'production' iteration that failed, which makes sense. The code works when I add values for production, so its all good. Thanks for checking it over though. Gave me confidence in debugging it.  -C
Reply all
Reply to author
Forward
0 new messages