Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define hash (dict) in ansible inventory file?

I am able to define a hash(dict) like below in group_vars/all:

region_subnet_matrix:
  site1:
    region: "{{ aws_region }}"
    subnet: "subnet-xxxxxxx"
    zone: "{{aws_region}}a"
  site2:
    region: "{{ aws_region }}"
    subnet: "subnet-xxxxxxx"
    zone: "{{aws_region}}b"

but for the life of me, I could not figure out how to define it under hosts file

[all:vars]
region_subnet_matrix="{
  site1:
    region: "{{ aws_region }}"
    subnet: "subnet-xxxxxxx"
    zone: "{{aws_region}}a"
  site2:
    region: "{{ aws_region }}"
    subnet: "subnet-xxxxxxx"
    zone: "{{aws_region}}b"
}"

I know it was incorrect, but I don't know the right way. Can someone enlighten me, please?

like image 702
Benson Jin Avatar asked Apr 24 '15 20:04

Benson Jin


People also ask

How do I specify Ansible inventory file?

Once your inventory is defined, you use patterns to select the hosts or groups you want Ansible to run against. The default location for inventory is a file called /etc/ansible/hosts . You can specify a different inventory file at the command line using the -i <path> option.

What is Dict in Ansible?

Note. This lookup plugin is part of ansible-core and included in all Ansible installations. In most cases, you can use the short plugin name dict even without specifying the collections: keyword.

What is list and dictionary in Ansible?

For Ansible, nearly every YAML file starts with a list. Each item in the list is a list of key/value pairs, commonly called a “hash” or a “dictionary”. So, we need to know how to write lists and dictionaries in YAML. There's another small quirk to YAML.

What is Host_vars and Group_vars in Ansible?

The host_vars is a similar folder to group_vars in the repository structure. It contains data models that apply to individual hosts/devices in the hosts. ini file. Hence, there is a YAML file created per device containing specific information about that device.


2 Answers

As I read the source code of Ansible, values of variables in inventory files are evaluated by "ast.literal_eval()" of Python. So you can describe dict variables in inventory files by one-line Python literals.

Your example might look like:

[all:vars]
region_subnet_matrix={'site1': {'subnet': 'subnet-xxxxxxx', 'region': '{{ aws_region }}', 'zone': '{{aws_region}}a'}, 'site2': {'subnet': 'subnet-xxxxxxx', 'region': '{{ aws_region }}', 'zone': '{{aws_region}}b'}}

Make sure that no variables are evaluated in this example.

N.B.: I don't know this kind of inventory variable definition is officially permitted.

like image 123
Norio Kimura Avatar answered Oct 17 '22 14:10

Norio Kimura


You can't use dict in inventory file because it use ini format. The preferred practice in Ansible is actually not to store variables in the main inventory file. Host and group variables can be stored in individual files relative to the inventory file.

Assuming the inventory file path is: /etc/ansible/hosts

If the host is named ‘testserver’ variables in YAML file at the following location will be made available to the host: /etc/ansible/host_vars/testserver.

The data in the this file might look like:

region_subnet_matrix:
  site1:
    region: "{{ aws_region }}"
    subnet: "subnet-xxxxxxx"
    zone: "{{aws_region}}a"
  site2:
    region: "{{ aws_region }}"
    subnet: "subnet-xxxxxxx"
    zone: "{{aws_region}}b"

Read more here.

like image 32
dizballanze Avatar answered Oct 17 '22 16:10

dizballanze