can extra-vars be a parameter from the host file ?

Hello,

I would like to set a lot of parameters when playing my playbook, depending on the server/user where it will be deployed so the idea is to set it in the hosts file.

It doesn’t seem to work as my varaibles are no set, here is what I plan to do:

[webserver]
linuxserv ansible_connection=local ansible_user=aa12 extra-vars=“@/home/root/vars/vars.json”

running the playbook this way:

ansible-playbook deploy.yml

and with vars.json:

{
“ENVT”: “aa12”,
“MODULE”: “tomweb”,
“RELEASE”: “1.2”,
}

Is this possible or do I have to try to load the vars parameter file in the playbook ?

Regards

If the variables are host specific as you first indicate, you can use host variables or group variables
You need to have the specific directory structure :
- hosts (file)
- host_vars/ (directory)
- group_vars/ (directory)

$ cat hosts
[webserver]
linuxserv

$ cat host_vars/linuxserv

extra_vars= inside your hosts file has no meaning, it just gives a path to a variable named ‘extra_vars’.

Ansible can consume the JSON directly, if you don’t want to copy it you can symlink it to host_vars/hostname or group_vars/group_name and Ansible will assign those appropriately.

THank you, my issue is the following: the variables are host/user specific, so I am trying to find a way to set them in an easy way as they will pilote the playbook execution. (if the parameter is tomcat then the tomcat role will be played etc …)
So using a group_var is not easy to manage, I was thinking about setting vars in the hosts in a tab, this way:
[linuxserver]
server1 ansible_user=aa01 vartab=“1;tomcat;release1”

and then in the playbook getting the variables using the vartab[1] …
I am new in ansible, I don’t know if this is possible.

Thanks for your help
Regards

Hello,

I did solve my issue by using an array to pass my parameters in the hosts file:

[deployserver]
linuxserv1 ansible_connection=local ansible_user=aa01 PARAM=“[‘/aa01/dev’, ‘tomweb’, ‘1.2’]”

linuxserv1 ansible_connection=local ansible_user=bb01 PARAM=“[‘/bb01/dev’, ‘query’, ‘15.1’]”

linuxserv2 ansible_connection=local ansible_user=aa01 PARAM=“[‘/aa01/prd’, ‘tcserver’, ‘1.2’]”

and in my playbook:

envt: “{{ PARAM[0]|default (‘fvfv’) }}”
module: “{{ PARAM[1]|default (‘tomcat’) }}”

Thank you,
Regards