With Ansible, users have the flexibility to accept external input while executing their Ansible playbooks without changing the Ansible playbook content. This can be done via the ability to pass extra variables to an Ansible playbook. This feature is available when using the Ansible CLI or Ansible Tower.
This article was originally published in Red Hat Sysadmin Blog. Follow Red Hat Sysadmin Blog for more articles.
Why should I pass variables to an Ansible playbook when I can declare every variable and value in the playbook or in variable files? This was a recent question I received last time I explained the concept of using extra vars option in Ansible.
The answer lies becomes explicit when you run into scenarios such as the following:
While these questions only scratch the surface, it becomes clear that when you are looking for flexibility, the Ansible extra variable feature is the best answer for most of these solutions. The following use cases explain how you can use extra variables to add flexibility to your Ansible playbooks.
For example, you create an Ansible playbook that runs on a specific set of hosts and sets those hosts to a group labeled: webgroup. As you complete your Ansible playbook testing, you enable it for production use.
Unfortunately, the operations team or engineers need to run this playbook on a different group of hosts called appgroup. By explicitly hard-coding the hosts group name within the Ansible playbook, you’ve limited its flexibility requiring changes in the Ansible playbook itself.
What is a better method to solve this issue?
The use of the --extra-vars
parameter and modifying the Ansible playbook to take a variable (e.g., nodes) when declaring your hosts. The following example illustrates it:
- hosts: "{{ nodes }}"
vars_files:
- vars/main.yml
roles:
- { role: geerlingguy.apache }
To pass a value to nodes, use the --extra-vars
or -e
option while running the Ansible playbook, as seen below.
# ansible-playbook myplaybook.yaml --extra-vars "nodes=webgroupโ
## Or
# ansible-playbook myplaybook.yaml --extra-vars "nodes=appgroupโ
This ensures you avoid accidental running of the playbook against hardcoded hosts. If the Ansible playbook fails to specify the hosts while running, Ansible will throw an error and stop, saying no value for nodes.
Ansible roles are a collection of templates, files, variables, modules, handlers, and tasks created for the purpose of simplifying the reuse of Ansible code.
Within an Ansible role directory structure, you’ll have two types of variables inside the following directories:
defaults/main.yml
– contains variables for a role that can be customized based on the desired usage of the role.vars
/main.yml
– contains variables for a role that are not intended to be modified.*Due to --extra-vars
having higher precedence than vars/main.yml
variables can be modified using the --extra-vars
parameter. Modifiable variables should reside in defaults/main.yml
You can set the variable values by using --extra-vars
.
For example, using a different port in an Apache installation like below (assuming variables are defined within the defaults/main.yml):
# ansible-playbook deploy-apache.yaml --extra-vars โapache_listen_port=8080โ
If you have multiple values to pass, then use this:
# ansible-playbook deploy-apache.yaml --extra-vars โapache_listen_port=8080 apache_listen_port_ssl=443โ
You need to add quotation marks to string values with spaces to pass them as extra variables:
# ansible-playbook deploy-apache.yaml --extra-vars "apache_ssl_protocol='All -SSLv2 -SSLv3'"
Yes, you can pass the extra variables as JSON format as follows.
# ansible-playbook deploy-apache.yaml --extra-vars "{\"name\":\"John\"}"
If you have many variables to pass, then it won’t be easy to input those via command lines. In such cases, you can pass the variable as a file (JSON or YAML) as shown below.
# ansible-playbook deploy-apache.yaml --extra-vars "@my_variable_file.json"
This is just a glimpse of the different options on how to pass variables to an Ansible playbook. For more ways to pass variables to Ansible playbooks, such as using JSON and external variable files, check out the Ansible documentation site.
This article was originally published in Red Hat Sysadmin Blog. Follow Red Hat Sysadmin Blog for more articles.
Disclaimer:
The views expressed and the content shared in all published articles on this website are solely those of the respective authors, and they do not necessarily reflect the views of the author’s employer or the techbeatly platform. We strive to ensure the accuracy and validity of the content published on our website. However, we cannot guarantee the absolute correctness or completeness of the information provided. It is the responsibility of the readers and users of this website to verify the accuracy and appropriateness of any information or opinions expressed within the articles. If you come across any content that you believe to be incorrect or invalid, please contact us immediately so that we can address the issue promptly.
Gineesh Madapparambath
Gineesh Madapparambath is the founder of techbeatly and he is the co-author of The Kubernetes Bible, Second Edition. and the author of ๐๐ป๐๐ถ๐ฏ๐น๐ฒ ๐ณ๐ผ๐ฟ ๐ฅ๐ฒ๐ฎ๐น-๐๐ถ๐ณ๐ฒ ๐๐๐๐ผ๐บ๐ฎ๐๐ถ๐ผ๐ป.
He has worked as a Systems Engineer, Automation Specialist, and content author. His primary focus is on Ansible Automation, Containerisation (OpenShift & Kubernetes), and Infrastructure as Code (Terraform).
(aka Gini Gangadharan - iamgini.com)
This site uses Akismet to reduce spam. Learn how your comment data is processed.2 Responses
Leave a Reply Cancel reply
ansible-playbook deploy-apache.yaml –extra-vars “@my_variable_file.json”
please can you show one example Json file, I tried its not working for me.
Can you share the file you have tried ?
Also check the documentation: https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#json-string-format
If you are facing issues, please feel to join and ask question in group t.me/techbeatly
Thank you.