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. Very useful to combine some script, automation, or shell variables. Example with fruit variable defined as “banana” and changed to “apple” or “raspberry”.


How to pass Pass Variables value to Ansible Playbook in command line? I’m going to show you a live demo with some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot.
How to pass Extra Variables to Ansible Playbook?
--extra-vars "fruit=apple"
--extra-vars '{"fruit":"apple"}'
--extra-vars "@file.json"
--extra-vars "@file.yml"
Today we’re talking about Ansible extra variables.
The easiest way to pass Pass Variables value to Ansible Playbook in the command line is using the extra variables parameter of the “ansible-playbook” command.
This is very useful to combine your Ansible Playbook with some pre-existent automation or script.
Let me clarify that this is specific for variables, there is another way to look for environment variables
The command line parameter is the --extra-vars "variable=value"
and allows you to pass some value from the terminal to the playbook.
You could specify also the parameter in JSON format or include a JSON or a YAML file.
The Best Resources For Ansible
Video Course
Printed Book
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- 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 Containers and Kubernetes By Examples: 20+ Automation Examples To Automate Containers, Kubernetes and OpenShift
- 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
demo
Let’s jump in a real-life example How to pass extra Variables to Ansible Playbook in the command line.
code
---
- name: extra variable demo
hosts: all
vars:
fruit: "banana"
task:
- name: print message
ansible.builtin.debug:
msg: "fruit is {{ fruit }}"
execution without extra variables
$ ansible-playbook --extra-vars="fruit=apple" -i virtualmachines/demo/inventory extra-variable/example.yml
PLAY [extra variable demo] ************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [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
ansible-pilot $
execution with a plain extra variable
$ ansible-playbook -i virtualmachines/demo/inventory --extra-vars="fruit=apple" extra-variable/example.yml
PLAY [extra variable demo] ************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [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
ansible-pilot $
execution with a JSON extra variable
$ ansible-playbook -i virtualmachines/demo/inventory --extra-vars='{"fruit":"raspberry"}' extra-variable/example.yml
PLAY [extra variable demo] ************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [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
ansible-pilot $
Recap
Now you know how to pass Extra Variables to Ansible Playbook in the command line. 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