Search code examples
ansibleansible-inventory

Ansible nested variables in inventory


I've been using Ansible for a while now, and in general have no trouble with variables in inventories. However, for the first time I have to override a nested variable in the inventory, and the I'd expect it to work... doesn't.

The default/main.yml of the role looks like this:

archiver_config:
    archiver_folder: "/opt/archiver"
    source_folder: "/var/tmp/images"
    archive_folder: "/var/tmp/imagearchive"
    min_diskspace: 1e6
    logfile: "/var/log/archiver.log"   

I need to override the default archive folder for some hosts because some of them have an external filesystem attached for this purpose, so I did this in the inventory:

[tdevices]
10.8.0.38 adeploy_name=16014c archiver_config.archive_folder=/media/ext

I have also tried putting the value in double and single quotes, like e.g.

archiver_config.archive_folder='/media/ext'

But it doesn't work. Ansible doesn't throw any errors, but the default value does not get overridden. What's the correct syntax to do this?


Solution

  • There are no "nested variables" in your example. There is only one variable archiver_config which is a dictionary (hash).

    You cannot assign a value to a dictionary key in the inventory file.

    What you can do is add a variable in the defaults/main.yml, use it as a value for the key (now, this can be called a nested variable):

    archive_folder: "/var/tmp/imagearchive"
    
    archiver_config:
        archiver_folder: "/opt/archiver"
        source_folder: "/var/tmp/images"
        archive_folder: "{{archive_folder}}"
        min_diskspace: 1e6
        logfile: "/var/log/archiver.log"  
    

    and assign value to it in the inventory file:

    [tdevices]
    10.8.0.38 adeploy_name=16014c archive_folder=/media/ext