Search code examples
ansibleyamljinja2

Configuration file of Ansible does not seems to accept my Jinja variable


I tried to modify my default inventory list from /etc/ansible/hosts to $HOME/.ansible/inventory_list, below is my attempt ( I assume ANSIBLE_HOME point to my Ansible configuration path which is $HOME/.ansible).

inventory={{ ANSIBLE_HOME ~ "/inventory_list" }}

But this doesn't work, upon viewing with ansible-config dump --only-changed, I see this instead.

$ ansible-config dump --only-changed
DEFAULT_HOST_LIST(/home/liso/.ansible.cfg) = ['/home/liso/{{ ANSIBLE_HOME }}']

I expect this to be DEFAULT_HOST_LIST(/home/liso/.ansible.cfg) = ['/home/liso/.ansible/inventory_list'].

Can anyone help me figure out this problem?
I installed Ansible via pip3 install ansible.


Solution

  • The configuration file of Ansible is not a Jinja templated file.
    It does indeed accepts variables but only environment variables, and a really specific {{CWD}} macro as described in the section "Relative paths for configuration".

    In the same section, you can read:

    You can specify a relative path for many configuration options. In most of those cases the path used will be relative to the ansible.cfg file used for the current execution.

    Source: https://docs.ansible.com/ansible/latest/reference_appendices/config.html#relative-paths-for-configuration

    So, that means that, in your case, since your configuration file is /home/liso/.ansible.cfg, you could just go by

    inventory=.ansible/inventory_list
    

    Or you can use the $HOME environment variable:

    inventory=$HOME/.ansible/inventory_list