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?
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With