Ansible host_vars and group_vars and usage options |
The command-line variable will be the highest priority.
To test the variable precedence, top priority extra vars
ansible -i "node1," all \ -e "var1=VybhavaTechnologies" \ -m debug -a 'msg={{ var1 }}'
Here -i use the target host and the group name to define an extra variable that will have the highest priority. Using debug module we can get the var1 value using jinja format to print.
Let's see the following example:
Create the inventory.yml file in "dev" directory. inventory.yml
Rule 1: When you create a sub-directory inside group_vars must be name of the group name, inside that you can define variables.yml file.
Rule 2: Defining the host_vars should contain sub-directories only with hostname(node1) where you can have variables.yml file.
Create the inventory.yml file in "dev" directory. inventory.yml
all: children: common: children: webserver: hosts: node[1:2]: vars: var1: "webserver" dbserver: hosts: localhost: var1: "node3" vars: var1: "VybhavaTechnologies"
Ansible host_vars and group_vars |
Now run the following command to know how the var1 will work at each group level and host level from the inventory variables.
Let's use clean inventory file now, that is remove all the variables values
inventory.yml
ansible -i inventory.yml all -m debug -a 'msg={{ var1 }}' #Try 2 ansible -i inventory.yml webserver -m debug -a 'msg={{ var1 }}' ansible -i inventory.yml dbserver -m debug -a 'msg={{ var1 }}'The output of the above execution is :
Ansible variable precedence test using inventory variables |
all: children: common: children: webserver: hosts: node[1:2]: dbserver: hosts: localhost:Now let us use the group_vars and host_vars for the same purpose testing var1 value!
mkdir group_vars hosts_vars mkdir group_vars/all vim group_vars/all/variables.yml var1: "I'm in group_vars all" tree ansible -i inventory.yml all -m debug -a 'msg={{ var1 }}' #try dbserver target ansible -i inventory.yml dbserver -m debug -a 'msg={{ var1 }}'The variable defined under the group_vars directory where you can use 'all' folder to use the variable in all playbooks and roles. if you want to use a specific variables for a specific group you need to define them separately, for db, web groups you may need some variable value will be executed from the dedicated folder
group_varrs example in ansible |
That concludes the group_vars will be defined for all as shown above. but we can override them with host_vars, let's see how it is possible now.
specific group variable overrides all group |
Let's quickly check how the host_vars are defined and have their precedence with the following experiment
Here we have tested with 'var1', but in real-time project different variables are required. There could be multiple variables need to be defined at each level which will play importance as per the project requirements. For example developers want notifications for webserver group, you can define 'email_to' variable with developers direct group. similar DBA team want to see mails notification when something happen on dbservers.
all: children: common: children: webserver: hosts: node[1:2]: vars: email_to: "webdeveloper@vybhavatechnologies.com" dbserver: hosts: localhost: email_to: "dba@vybhavatechnologies.com" vars: email_to: "allteams@VybhavaTechnologies"Execution will be as follows:
#try with all: ansible -i inventory.yml all -m debug -a 'msg={{ email_to }}' #try with dbserver: ansible -i inventory.yml dbserver -m debug -a 'msg={{ email_to }}'
Rule 2: Defining the host_vars should contain sub-directories only with hostname(node1) where you can have variables.yml file.
Let's examine the host_vars/node1/variables.yml file content with variable 'var1' is defined then it will be overrides the group_vars-> var1 value.
2022
Here is my new learning about host_vars and group_vars experiment hope you enjoyed
--- # Filename: varexample.yaml # Extra Varialble targets [optional]: the hosts that will be tested; if nothing specific boxes are set, then localhost will be targeted - name: varibles in group_vars and host_vars hosts: "{{ targets | default('localhost') }}" gather_facts: false tasks: - debug: msg: - "var1= {{ var1 }} var1 type: {{ var1 |type_debug }}" - "email_to= {{ email_to }} email_to type: {{ email_to |type_debug }}"Here are multiple usecases for the same playbook executions outputs
# default all groups ansible-playbook varexample.yaml # specific target as node1 ansible-playbook varexample.yaml -e targets=node1 # Specific target as webserver group ansible-playbook varexample.yaml -e targets=webserver
group_vars and host_var examples in a Ansible Playbook |
Courtesy by: Maheshwari
Every experiment is useful, every moment you spend for learning new things is valuable, Keep LEARNING
Keep Sharing!
H A P P Y A U TO M A T I ON !! Enjoy with Ansible!!
References:
No comments:
Post a Comment