How to determine the hash (dict) in the inventory file?

I can define a hash (dict) as shown 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 life I could not figure out how to define it in the 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 this was wrong, but I do not know the right way. Can someone enlighten me please?

+6
source share
2 answers

When I read the Ansible source code, the values โ€‹โ€‹of the variables in the inventory files are evaluated using the "ast.literal_eval ()" Python. That way you can describe dict variables in inventory files with single-line Python literals.

Your example might look like this:

 [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 this example does not evaluate variables.

NB: I do not know that such an inventory variable definition is officially permitted.

+6
source

You cannot use dict in the inventory file because it uses the ini format. The preferred practice in Ansible is not actually storing variables in the main inventory file. Host and group variables can be stored in separate files relative to the inventory file.

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

If the host is named "testerver variables in the YAML file at the following location, it will be available to the host: /etc/ansible/host_vars/testserver .

The data in this file may look like this:

 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" 

More details here .

+5
source

Source: https://habr.com/ru/post/985912/


All Articles