cybertextron
cybertextron

Reputation: 10961

Dictionary inside Dictionary in Ansible

I have the following Ansible playbook file:

  1 ---                                                                                                                                           
  2 -   name: Provision cluster                                                                                                
  3     connection: local                                                                                                                         
  4     hosts: localhost                                                                                                                          
  5     gather_facts: False                                                                                                                       
  6     vars_files:                                                                                                                               
  7     -   ../vars_files/credentials/foo.yml                                                                                         
  8     -   ../vars_files/credentials/bar.yml                                                                                          
  9                                                                                                                                               
 10     vars:                                                                                                                                     
 11       - ec2_hosts:                                                                                                                            
 12         - node1:                                                                                                                              
 13           - address: 1.1.1.1                                                                                                               
 14         - node2:                                                                                                                              
 15           - address: 1.1.1.2                                                                                                               
 16         - node3:                                                                                                                              
 17           - address: 1.1.1.3                                                                                                              
 18         - node4:                                                                                                                              
 19           - address: 1.1.1.4

What I'm trying to do is imply to have a dictionary inside another dictionary. On the second I'm going to declare a dictionary between hostname and its IP address. However I'm getting the following error:

ERROR: Syntax Error while loading YAML script, provision.yml
Note: The error may actually appear before this position: line 10, column 1


        vars:

Upvotes: 1

Views: 1629

Answers (1)

Kashyap
Kashyap

Reputation: 17401

I personally prefer to use JSON (or Python'ish) expressions when defining complex variables as I find those more readable compared to complex vars defined in YAML syntax. E.g.:

vars:
  ec2_hosts: {
      node1: {
          address: 1.1.1.1,
          hostname: ec2_node1
      },
      node2: {
          address: 1.1.1.2,
          hostname: ec2_node2
      },
    },

If you must use YAML, hope that's actually the structure you want, it's different than the previous one:

vars:
  - ec2_hosts:
    - host1:
      - address: 1.1.1.1
    - host2:
      - address: 1.1.1.2

Here is the tested code in case there are some other syntax errors:

- name: Provision cluster
  connection: local
  hosts: localhost
  gather_facts: False
  # vars_files:
  # - ../vars_files/credentials/foo.yml
  # - ../vars_files/credentials/bar.yml
  vars:
    - ec2_hosts:
      - host1:
        - address: 1.1.1.1
      - host2:
        - address: 1.1.1.2
  tasks:
  - debug: msg="{{ec2_hosts}}"

PS: I copied LOTS of trailing whitespaces from your post, hope they don't exist in your actual playbook YAML.

Upvotes: 3

Related Questions