How to Pass Variables to Ansible Playbook in command line? - Ansible extra variables
How to pass or override an Ansible Playbook variable from the command line in plaintext, JSON, or YAML. It is very useful to combine some script, automation, or shell variables. For example, the fruit variable is defined as "banana" and changed to "apple" or "raspberry".


Passing Variables to Ansible Playbook: A Quick Guide
In today’s episode of Ansible Pilot, we’ll delve into the practical aspect of passing variables to Ansible Playbooks via the command line. This can be a powerful and flexible way to customize your playbook execution based on dynamic inputs. I’m Luca Berton, and let’s jump right into the world of Ansible extra variables.
Understanding Ansible Extra Variables
Ansible extra variables provide a means to pass values to your playbook from the command line. This flexibility is particularly valuable when you need to integrate Ansible into existing automation scripts or workflows. Extra variables can be specified in various formats, and today, we’ll explore a few options.
Command Line Syntax
The command line parameter for passing extra variables is --extra-vars
, followed by the variable-value pair. Here are some examples:
--extra-vars "fruit=apple"
--extra-vars '{"fruit":"apple"}'
--extra-vars "@file.json"
--extra-vars "@file.yml"
Real-Life Example
Let’s illustrate this concept with a real-life example. Consider the following Ansible Playbook:
- example.yml
---
- name: Extra variable demo
hosts: all
vars:
fruit: "banana"
task:
- name: Print message
ansible.builtin.debug:
msg: "fruit is {{ fruit }}"
In this playbook, we have a variable named fruit
with a default value of “banana.” The playbook then prints a message using the value of this variable.
Executing Without Extra Variables
If we run the playbook without any extra variables, it uses the default value:
$ ansible-playbook -i inventory/demo --extra-vars="fruit=apple" example.yml
PLAY [Extra variable demo] ************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [Print message] *****************************************************************************
ok: [demo.example.com] => {
"msg": "fruit is apple"
}
PLAY RECAP ****************************************************************************************
demo.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
The Best Resources For Ansible
Certifications
Video Course
Printed Book
eBooks
Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Ansible Cookbook: A Comprehensive Guide to Unleashing the Power of Ansible via Best Practices, Troubleshooting, and Linting Rules with Luca Berton
Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
Ansible Automation Platform By Example: A step-by-step guide for the most common user scenarios
Executing With a Plain Extra Variable
Now, let’s provide a plain extra variable:
$ ansible-playbook -i inventory/demo --extra-vars="fruit=apple" example.yml
PLAY [Extra variable demo] ************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [Print message] *****************************************************************************
ok: [demo.example.com] => {
"msg": "fruit is apple"
}
PLAY RECAP ****************************************************************************************
demo.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Executing With a JSON Extra Variable
Lastly, let’s use a JSON-formatted extra variable:
$ ansible-playbook -i inventory/demo --extra-vars='{"fruit":"raspberry"}' example.yml
PLAY [Extra variable demo] ************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [Print message] *****************************************************************************
ok: [demo.example.com] => {
"msg": "fruit is raspberry"
}
PLAY RECAP ****************************************************************************************
demo.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Recap
In summary, passing extra variables to an Ansible Playbook through the command line is a straightforward process. Whether you choose a plain variable, a JSON-formatted one, or even reference a file, Ansible provides the flexibility to cater to your specific needs. This capability opens up endless possibilities for dynamic playbook executions, making your automation workflows even more powerful.
Subscribe to the YouTube channel, Medium, Website, Twitter, and Substack to not miss the next episode of the Ansible Pilot.Academy
Learn the Ansible automation technology with some real-life examples in my
My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate