Search code examples
ansibleansible-inventory

Defining host as variable in Ansible hosts file


I have a rather simple hosts file

[clients]
qas0062
[dbs_server]
qas0063

For the users of the project we don't want them to modify hosts file but rather we have a separate user.config.yml file that contain various user-configurable parameters. There we have entry such as

dbs_server: qas0065

So the question is: is it possible to use a variable in the hosts file that would use a value defined in user.config.yml? And what would be the format?


Solution

  • Pretty sure you can't templatize the actual host key entry in the inventory, but you can templatize the value of its ansible_host connection var to achieve roughly the same effect, eg:

    [clients]
    clienthost ansible_host="{{ clienthost_var }}"
    [dbs_server]
    dbsserver ansible_host="{{ dbsserver_var }}"
    

    then set the value of those vars from external vars before the play starts executing (eg, with the vars_files directive or -e).