avatarLuca Berton

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

6780

Abstract

="hljs-strong"></span><span class="hljs-strong"></span> demo.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0</pre></div><div id="c7ee"><pre>ansible-pilot <span class="hljs-symbol"></span></pre></div><h1 id="8127">execution with a plain extra variable</h1><div id="a889"><pre><span class="hljs-meta prompt_"> </span><span class="language-bash">ansible-playbook -i virtualmachines/demo/inventory --extra-vars=<span class="hljs-string">"fruit=apple"</span> extra-variable/example.yml</span></pre></div><div id="38dc"><pre>PLAY [extra variable demo] <span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span></pre></div><div id="a159"><pre>TASK [Gathering Facts] <span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span> ok: [demo.example.com]</pre></div><div id="0cd1"><pre>TASK [message] <span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span> ok: [demo.example.com] => { <span class="hljs-code"> "msg": "fruit is apple" }</span></pre></div><div id="ec9c"><pre>PLAY RECAP <span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span> demo.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0</pre></div><div id="42bc"><pre>ansible-pilot <span class="hljs-symbol"></span></pre></div><h1 id="2f0d">execution with a JSON extra variable</h1><div id="95e4"><pre> ansible-playbook -i virtualmachines/<span class="hljs-built_in">demo</span>/inventory --extra-vars='{<span class="hljs-string">"fruit"</span>:<span class="hljs-string">"raspberry"</span>}' extra-variable/<span class="hljs-built_in">example</span>.yml</pre></div><div id="61b6"><pre>PLAY [extra variable demo] <span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span></pre></div><div id="ed2e"><pre>TASK [Gathering Facts] <span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span> ok: [demo.example.com]</pre></div><div id="04d7"><pre>TASK [message] <span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span> ok: [demo.example.com] => { <span class="hljs-code"> "msg": "fruit is

Options

raspberry" }</span></pre></div><div id="10e3"><pre>PLAY RECAP <span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span><span class="hljs-strong"></span> demo.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0</pre></div><div id="6f7b"><pre>ansible-pilot <span class="hljs-symbol">$</span></pre></div><div id="47de" class="link-block"> <a href="https://github.com/lucab85/ansible-pilot"> <div> <div> <h2>GitHub - lucab85/ansible-pilot: Ansible Pilot YouTube channel code repository</h2> <div><h3>You can't perform that action at this time. You signed in with another tab or window. You signed out in another tab or…</h3></div> <div><p>github.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*9E79Q07RDlHB254I)"></div> </div> </div> </a> </div><h1 id="5b42">Recap</h1><p id="ae44">Now you know how to pass Extra Variables to Ansible Playbook in the command line.</p><p id="f8da">Subscribe to the<a href="https://www.youtube.com/channel/UC5MNbTYRHSCu9vAki3z9SmA"> YouTube channel</a>,<a href="https://ansiblepilot.medium.com/"> Medium</a>, and<a href="https://www.ansiblepilot.com/"> Website</a> to not miss the next episode of the Ansible Pilot.</p><h2 id="1668">Video Course</h2><ul><li><a href="https://click.linksynergy.com/deeplink?id=euGmLrdj*Ec&amp;mid=39197&amp;murl=https%3A%2F%2Fwww.udemy.com%2Fcourse%2Fansible-by-examples-devops%2F%3FreferralCode%3D8E065F6D6F8622A3DEC8"><b>Learn Ansible Automation in 250+examples & practical lessons: Learn Ansible with some real-life examples of how to use the most common modules and Ansible Playbook</b></a></li></ul><h2 id="a033">Printed Books</h2><figure id="7621"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*eK7ZTcUkiSDTh6S7"><figcaption><a href="https://amzn.to/3XHeDLd">Ansible for VMware by Examples</a></figcaption></figure><figure id="b595"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*ct4RPFWWWeUP1m9S"><figcaption><a href="https://amzn.to/3NLCAMB">Ansible for Kubernetes by Example</a></figcaption></figure><figure id="4719"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*hvcRcNX06JcnqTTK"><figcaption><a href="https://amzn.to/43HEMuL">Hands-on Anasible Automation</a></figcaption></figure><h2 id="223c">eBooks</h2><ul><li><a href="https://leanpub.com/ansiblebyexamples"><b>Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps</b></a></li><li><a href="https://leanpub.com/ansibleforwindowsbyexamples"><b>Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps</b></a></li><li><a href="https://leanpub.com/ansibleforlinuxbyexamples"><b>Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps</b></a></li><li><a href="https://leanpub.com/linuxfileanddirectorybyansibleexamples"><b>Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure</b></a></li><li><a href="https://leanpub.com/ansible-for-kubernetes-by-examples"><b>Ansible For Containers and Kubernetes By Examples: 20+ Automation Examples To Automate Containers, Kubernetes and OpenShift</b></a></li><li><a href="https://leanpub.com/ansibleforsecuritybyexamples"><b>Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure</b></a></li><li><a href="https://leanpub.com/ansible-tips-and-tricks"><b>Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks</b></a></li><li><a href="https://leanpub.com/ansiblelinuxusersandgroupsbyexamples"><b>Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure</b></a></li><li><a href="https://leanpub.com/ansible-for-postgresql-by-examples"><b>Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL Database</b></a></li><li><a href="https://leanpub.com/ansible-for-aws-by-examples"><b>Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure</b></a></li><li><a href="https://leanpub.com/Ansible-Automation-Platform/"><b>Ansible Automation Platform By Example: A step-by-step guide for the most common user scenarios</b></a></li></ul><h1 id="b7e5">Donate</h1><div id="3d03" class="link-block"> <a href="https://patreon.com/lucaberton"> <div> <div> <h2>Luca Berton is creating Software Open Source for Ansible | Patreon</h2> <div><h3>Become a patron of Luca Berton today: Get access to exclusive content and experiences on the world's largest membership…</h3></div> <div><p>patreon.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*kGZYTmgBI_wjNNdZ)"></div> </div> </div> </a> </div><div id="00ff" class="link-block"> <a href="https://github.com/sponsors/lucab85"> <div> <div> <h2>Sponsor @lucab85 on GitHub Sponsors</h2> <div><h3>I am an active Open Source contributor, involved in Ansible communities, though I am all over the place. @lucab85's…</h3></div> <div><p>github.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*6i9ntIUC_WR8P_sI)"></div> </div> </div> </a> </div><figure id="2e92"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*dBTYBsFhQSTdy5WX_OHN9w.png"><figcaption></figcaption></figure></article></body>

How to Pass Variables to Ansible Playbook in the 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.

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, and Website to not miss the next episode of the Ansible Pilot.

Video Course

Printed Books

Ansible for VMware by Examples
Ansible for Kubernetes by Example
Hands-on Anasible Automation

eBooks

Donate

Ansible
Command Line
Code
Variables
DevOps
Recommended from ReadMedium