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

281 views
Skip to first unread message

fanvalt

unread,
Apr 18, 2016, 8:32:18 AM4/18/16
to Ansible Project
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

Toshaan Bharvani | VanTosh

unread,
Apr 18, 2016, 8:51:07 AM4/18/16
to ansible...@googlegroups.com
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
---
ansible_connection: local
ansible_user: aa12
ENVT: aa12
MODULE: tomweb
RELEASE: 1.2

$ cat group_vars/webserver
---
webserver: apache

Now you run the playbook with those hosts and the variables will be
usable from each hosts
You can actually have different inventory names and different actual
names using this
You can group hosts and use group_vars directory
The host file is an ini file (or executable), while the host and group
vars file are json/yaml files (that is why they have the three dashes)

For more info look at the documentation, it is very clearly explained :
http://docs.ansible.com/ansible/intro_inventory.html

Brian Coca

unread,
Apr 18, 2016, 7:22:29 PM4/18/16
to ansible...@googlegroups.com
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.


----------
Brian Coca

fanvalt

unread,
Apr 19, 2016, 3:49:13 AM4/19/16
to Ansible Project
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

fanvalt

unread,
Apr 19, 2016, 6:31:21 AM4/19/16
to Ansible Project
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
Reply all
Reply to author
Forward
0 new messages