Ansible 2.13 issue with lists in host_vars

Viewed 36

Trying to automate some per-host configuration, which requires a list of items (databases to be created per database host in this case).

The inventory file is at ./inventory/hosts:

[dbservers]
intweb

Which has no group_vars associated, and the following host_vars located at ./inventory/host_vars/intweb.yml:

---
testvalue: test
anothertestvalue:
  - value1
  - value2
  - value3
yetanothervalue: test

When running the following task in a play:

[...other lines...]

- name: Test list variables
  debug:
    msg: "List item is {{ item }}"
  with_items: "{{ anothertestvalue }}"

- name: Test list variable as string
  debug:
    msg: "{{ anothertestvalue }}"

- name: Test simple variables
  debug:
    msg: "{{ testvalue }}"

When running the playbook via

> ansible-playbook plays/default.yml --limit dbservers --start-at-task="Test list variables"

the relevant output is the following (which seems to ignore the list completely):

[...]

TASK [mariadb : Test list variables] ****************************************************

TASK [mariadb : Test list variable as string] *******************************************
ok: [intweb] => {
    "msg": []
}

TASK [mariadb : Test simple variables] **************************************************
ok: [intweb] => {
    "msg": "test"
}

What is the correct syntax to have a list such as the above defind in host_vars?

1 Answers

Given the tree

shell> tree .
.
├── ansible.cfg
├── inventory
│   ├── hosts
│   └── host_vars
│       └── intweb.yml
└── plays
    └── default.yml

The playbook

shell> cat plays/default.yml 
---
- hosts: all
  gather_facts: false
  tasks:
    - name: Test list variables
      debug:
        msg: "List item is {{ item }}"
      with_items: "{{ anothertestvalue }}"

    - name: Test list variable as string
      debug:
        msg: "{{ anothertestvalue }}"

    - name: Test simple variables
      debug:
        msg: "{{ testvalue }}"

works as expected when the path to the inventory is added to the command line

shell> ansible-playbook plays/default.yml -i inventory/hosts --limit dbservers --start-at-task="Test list variables"

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

TASK [Test list variables] *******************************************************************
ok: [intweb] => (item=value1) => 
  msg: List item is value1
ok: [intweb] => (item=value2) => 
  msg: List item is value2
ok: [intweb] => (item=value3) => 
  msg: List item is value3

TASK [Test list variable as string] **********************************************************
ok: [intweb] => 
  msg:
  - value1
  - value2
  - value3

TASK [Test simple variables] *****************************************************************
ok: [intweb] => 
  msg: test

PLAY RECAP ***********************************************************************************
intweb: ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
Related