Ansible list variable

Ansible uses variables to manage differences between systems. we can define these variables in our playbooks, in our inventory, in re-usable files or roles, or at the command line. we can also create variables during a playbook run by registering the return value or values of a task as a new variable.


A list variable combines a variable name with multiple values. The multiple values can be stored as an itemized list or in square brackets [], separated with commas.


Let's see an example of an ansible-playbook for list variables.

# cat listvariable.yaml

---

- hosts: all

  vars:

    teams:

      - csk

      - srh

      - mi

      - rcb

  tasks:

  - name: pring list variables

    debug:

      msg="{{ teams[0] }}"


Then run the playbook

# ansible-playbook -i hosts listvariable.yaml

PLAY [all] **************************************************************************************************************************************************


TASK [Gathering Facts] **************************************************************************************************************************************

ok: [192.168.1.2]


TASK [pring list variables] *********************************************************************************************************************************

ok: [192.168.1.2] => {

    "msg": "csk"

}


PLAY RECAP **************************************************************************************************************************************************

192.168.1.2                : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0


Another example of list variable

# cat listvar.yaml

---

- hosts: all

  vars:

    teams:

      - csk

      - srh

      - mi

      - rcb

  tasks:

  - name: pring list variables

    debug:

      msg="{{ item }}"

    with_items:

      - "{{ teams }}"


# ansible-playbook -i hosts listvariable2.yaml

PLAY [all] **************************************************************************************************************************************************


TASK [Gathering Facts] **************************************************************************************************************************************

ok: [192.168.1.2]


TASK [pring list variables] *********************************************************************************************************************************

ok: [192.168.1.2] => (item=csk) => {

    "msg": "csk"

}

ok: [192.168.1.2] => (item=srh) => {

    "msg": "srh"

}

ok: [192.168.1.2] => (item=mi) => {

    "msg": "mi"

}

ok: [192.168.1.2] => (item=rcb) => {

    "msg": "rcb"

}


PLAY RECAP **************************************************************************************************************************************************

192.168.1.2                : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0




Relevant Blogs:

Ansible tower 

Ansible Vault 

Ansible loops  

Aws ec2 volume snapshot

Recent Comments

No comments

Leave a Comment