Ansible Extra Vars List Array | Ansible Extra Vars File JSON – DecodingDevops

Ansible Extra Vars List Array | Ansible Extra Vars File JSON - DecodingDevops

Using ansible extra vars we can pass the variable list, we can pass variable array and also we can pass extra vars with json file. In this post we are going to see how to pass ansible extra vars list, ansible extra vars in json file and ansible pass variable array.

Syntax:

JSON string format:

ansible-playbook release.yml --extra-vars '{"version":"1.23.45","other_variable":"foo"}'
ansible-playbook arcade.yml --extra-vars '{"pacman":"mrs","ghosts":["inky","pinky","clyde","sue"]}'

using above format we can pass ansible variable as a list or array.

Example:

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

  tasks:
  - name: ansible extra vars list command line
    debug:
      msg: "Hi This Is  {{ abc }}"

  - name: ansible extra vars list
    debug:
      msg: "Hi This Is  {{ xyz[1] }}"

Output:

root@ip-172-31-37-35:~# ansible-playbook list.yml -e '{"abc":"decodingdevops","xyz":["inky","pinky","clyde","sue"]}'


[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'


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

TASK [ansible extra vars list command line] ********************************************************************************************************************************
ok: [localhost] => {
    "msg": "Hi This Is  decodingdevops"
}

TASK [ansible extra vars list] *********************************************************************************************************************************************
ok: [localhost] => {
    "msg": "Hi This Is  pinky"
}

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

Ansible Extra Vars File JSON

define your all variables in one json file and using below command we can pass varibles in json file format.

ansible-playbook release.yml --extra-vars "@some_file.json"

here some_file.json is the file in which we have to place all your variables.