FreeKB - Ansible -e --extra-vars (variables) command line option
Ansible - -e --extra-vars (variables) command line option

The -e or --extra-vars command line option can be used to create variables on the command line, like this.

ansible-playbook playbook.yml --extra-vars "foo=Hello bar=World"

 

Or like using, using the JSON format.

ansible-playbook playbook.yml --extra-vars '{"foo": "Hello", "bar": "World"}'

 

In a playbook, the debug module can be used to print each variable.

---
- hosts: all
  tasks:
    - debug:
        msg: "{{ foo }}"

    - debug:
        msg: "{{ bar }}"

 

Something like this should be returned.

TASK [debug]
ok: [server1.example.com] => {
    "msg": "Hello"
}

TASK [debug]
ok: [server1.example.com] => {
    "msg": "World"
}

 


Array

Here is how you would pass in an array, using the JSON format.

ansible-playbook playbook.yml --extra-vars '{"foo": [Hello, World] }'

 


Booleans

Here is how you would create boolean variables.

ansible-playbook playbook.yml --extra-vars "{foo: true, bar: false}"

 


Precedence

The -e or --extra-vars command line option will take precedence over every other way that a variable can be set. For example, let's say you attempt to update the foo variable to contain a value of "Goodbye" using the vars plugin.

---
- hosts: all
  vars:
    foo: Goodbye
  tasks:
    - debug:
        msg: "{{ foo }}"

 

The foo variable will retain it's value of "Hello" because the -e or --extra-vars command line options take precedence.

TASK [debug]
ok: [server1.example.com] => {
    "msg": "Hello"
}

 

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter a9b2b in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |