Ansible: Pass Variable To Ansible Playbook-Ansible Extra Vars

Pass Variable To Ansible Playbook-Ansible Extra Vars

Pass variable to ansible playbook in the command line

using ansible ‘– – exra-vars’ tag or ‘-e‘ tag we can pass the ansible variable to ansible playbook in the command line. In this post i will explain how to pass variable to ansible playbook with examples.

Syntax:

ansible-playbook  --exra-vars "var1=value  var2=value .........."

ansible-playbook -e "var1=value  var2=value .........."

we can use any one of the above tags to pass variable to ansible playbook

Ansible Extra Vars Example 1:

[root@ip-10-0-0-20 ~]# cat decodingdevops.yml
---
- hosts: localhost
  gather_facts: no

  tasks:
  - name: pass the ansible varibale to playbook
    debug: 
      msg: "Hi This Is  {{ xyz }}"

in the above playbook ‘xyz’ is a variable, now by using ansible extra vars i will pass the variable to playbook

ansible-playbook decodingdevops.yml   – – extra-vars    “xyz=decodingdevops”   OR

ansible-playbook decodingdevops.yml   -e  “xyz=decodingdevops”

Output:

root@ip-172-31-37-35:~# ansible-playbook decodingdevops.yml -e "xyz=decodingdevops"
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'


PLAY [localhost] ***********************************************************************************************************************************************************

TASK [pass the ansible varibale to playbook] *******************************************************************************************************************************
ok: [localhost] => {
    "msg": "Hi This Is  decodingdevops"
}

PLAY RECAP *****************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Ansible Extra Vars Example 2:

Ansible Pass Multiple Varible in command line

root@ip-172-31-37-35:~# cat decodingdevops.yml
---
- hosts: localhost
  gather_facts: no

  tasks:
  - name: ansible pass multiple varibles in command line
    debug:
      msg: "Hi This Is  {{ abcd }}"

  - name: pass multiple varibales to playbook
    debug:
      msg: "Hi This Is  {{ efgh }}"

in the above playbook ‘abcd’ and ‘efgh’ are two variable now by using command line i will pass the variable values to playbook

ansible-playbook     decodingdevops.yml   – – extra-vars       “abc=decoding         efgh=devops”    OR

ansible-playbook     decodingdevops.yml   -e       “abc=decoding         efgh=devops”

Output:

root@ip-172-31-37-35:~# ansible-playbook decodingdevops.yml -e "abcd=decoding efgh=devops"
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'


PLAY [localhost] ***********************************************************************************************************************************************************

TASK [ansible pass multiple varibles in command line] **********************************************************************************************************************
ok: [localhost] => {
    "msg": "Hi This Is  decoding"
}

TASK [pass multiple varibales to playbook] *********************************************************************************************************************************
ok: [localhost] => {
    "msg": "Hi This Is  devops"
}

PLAY RECAP *****************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

in this way we can pass multiple variables to ansible playbook