Ansible Pilot

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”.

January 15, 2022
Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons

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?

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

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 $

code with ❤️ in GitHub

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

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons
Follow me

Subscribe not to miss any new releases