Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access ansible.cfg variable in task

How can I refer remote_tmp (or any other) value defined in ansible.cfg in my tasks? For example, in the my_task/defaults/main.yml:

file_ver: "1.5"
deb_file: "{{ defaults.remote_tmp }}/deb_file_{{ file_ver }}.deb"

produces an error:

fatal: [x.x.x.x]: FAILED! => {"failed": true, 
    "msg": "the field 'args' has an invalid value, 
            which appears to include a variable that is undefined. 
            The error was: {{ defaults.remote_tmp }}/deb_file_{{ file_ver }}.deb: 
           'defaults' is undefined\... }
like image 401
Ivan Velichko Avatar asked Oct 25 '16 18:10

Ivan Velichko


1 Answers

You can't do this out of the box.
You either need action plugin or vars plugin to read different configuration parameters.
If you go action plugin way, you'll have to call your newly created action to get remote_tmp defined.
If you choose vars plugin way, remote_tmp is defined with other host vars during inventory initialization.

Example ./vars_plugins/tmp_dir.py:

from ansible import constants as C

class VarsModule(object):

    def __init__(self, inventory):
        pass

    def run(self, host, vault_password=None):
        return dict(remote_tmp = C.DEFAULT_REMOTE_TMP)

Note that vars_plugins folder should be near your hosts file or you should explicitly define it in your ansible.cfg.

You can now test it with:

$ ansible localhost -i hosts -m debug -a "var=remote_tmp"
localhost | SUCCESS => {
    "remote_tmp": "$HOME/.ansible/tmp"
}
like image 73
Konstantin Suvorov Avatar answered Oct 16 '22 02:10

Konstantin Suvorov