How to define complex variable in inventory file of Ansible

Viewed 646

According to the official documentation you can define variables in the inventory the INI way with the following syntax (key=value):

[atlanta] 
host1
host2

[atlanta:vars]
ntp_server=ntp.atlanta.example.com
proxy=proxy.atlanta.example.com

Is it possible to define complex variables e.g.:

[atlanta] 
host1 
host2

[atlanta:vars]
userParameters:
  - { string: 'foo', list: ['foo', 'bar'] }

I usually define complex variables in roles e.g. vars/main.yml. On this case due to different environments I would like to define different complex variables in inventories file.

I tried to define the variables with both INI and YAML file format and I keep getting the error (sample):

[WARNING]:  * Failed to parse /home/user/dir/inventories/development/hosts.ini with ini plugin: /home/user/dir/inventories/development/hosts.ini:9: Expected key=value, got:
userParameters:

[WARNING]: Unable to parse /home/user/dir/inventories/development/hosts.ini as an inventory source

[WARNING]: No inventory was parsed, only implicit localhost is available

[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
1 Answers

Q: "Define variables in the inventory the INI way ... Is it possible to define complex variables?"

A: Yes. It is possible. e.g.

[atlanta:vars]
userParameters=[{'string': 'foo', 'list': ['foo', 'bar']}]
Related