Variable in hostvars

119 views
Skip to first unread message

Herman Pool

unread,
Dec 23, 2019, 11:38:07 AM12/23/19
to Ansible Project
Hi all,

I am new to ansible.
Why is my playbook not working?
And how can I solve it?

My playbook:

---
- name: print out ip adressess
  hosts: db_servers
  gather_facts: True
  vars:
    - nic: "enp0s8"
    - full_line: ""

  tasks:
  - debug: var=ansible_{{ nic }}.ipv4.address

  - set_fact: full_line="{{ full_line }} {{ hostvars[item].ansible_enp0s8.ipv4.address }}"
    loop: "{{ groups['db_servers'] }}"

  - debug:
      msg: "{{ full_line }}"

  - set_fact: full_line="{{ full_line }} {{ hostvars[item].ansible_{{ nic }}.ipv4.address }}"
    loop: "{{ groups['db_servers'] }}"

  - debug:
      msg: "{{ full_line }}"


The output:


PLAY [print out ip adressess] **********************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************
ok: [dbs11]
ok: [dbs10]
TASK [debug] ***************************************************************************************************************
ok: [dbs10] => {
    "ansible_enp0s8.ipv4.address": "192.168.152.174"
}
ok: [dbs11] => {
    "ansible_enp0s8.ipv4.address": "192.168.152.181"
}
TASK [set_fact] ************************************************************************************************************
ok: [dbs10] => (item=dbs10)
ok: [dbs10] => (item=dbs11)
ok: [dbs11] => (item=dbs10)
ok: [dbs11] => (item=dbs11)
TASK [debug] ***************************************************************************************************************
ok: [dbs10] => {
    "msg": " 192.168.152.174 192.168.152.181"
}
ok: [dbs11] => {
    "msg": " 192.168.152.174 192.168.152.181"
}
TASK [set_fact] ************************************************************************************************************
fatal: [dbs10]: FAILED! => {"msg": "template error while templating string: expected token 'end of print statement', got '{'. String: {{ full_line }} {{ hostvars[item].ansible_{{ nic }}.ipv4.address }}"}
fatal: [dbs11]: FAILED! => {"msg": "template error while templating string: expected token 'end of print statement', got '{'. String: {{ full_line }} {{ hostvars[item].ansible_{{ nic }}.ipv4.address }}"}
PLAY RECAP *****************************************************************************************************************
dbs10                      : ok=4    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
dbs11                      : ok=4    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0


With regards,

Herman


Kai Stian Olstad

unread,
Dec 23, 2019, 12:17:28 PM12/23/19
to ansible...@googlegroups.com
On 23.12.2019 17:38, Herman Pool wrote:
> I am new to ansible.
> Why is my playbook not working?

You have a couple of things you need to change.


> ---
> - name: print out ip adressess
> hosts: db_servers
> gather_facts: True
> vars:
> - nic: "enp0s8"
> - full_line: ""

vars is a dict not a list so you need to remove those dashes in front.


> tasks:
> - debug: var=ansible_{{ nic }}.ipv4.address
>
> - set_fact: full_line="{{ full_line }} {{ hostvars[item].ansible_enp0s8.ipv4.address }}"
> loop: "{{ groups['db_servers'] }}"
>
> - debug:
> msg: "{{ full_line }}"
>
> - set_fact: full_line="{{ full_line }} {{ hostvars[item].ansible_{{ nic }}.ipv4.address }}"
> loop: "{{ groups['db_servers'] }}"

You can't use {{ }} inside {{ }}, so you need to concatenate stings to make it work.

{{ hostvars[item]['ansible_' ~ nic].ipv4.address }}


You could drop the loop by using this

- set_fact:
full_line: "{{ groups['db_servers'] | map('extract', hostvars, ['ansible_' ~ nic, 'ipv4', 'address']) | join(' ') }}"


--
Kai Stian Olstad

Herman Pool

unread,
Dec 23, 2019, 12:54:22 PM12/23/19
to Ansible Project
Hello Kai,

many thnx for the quick respons!
Your solution works fine and it is exactly what I needed.

Happy X-mas to you all.


with regards,

Herman
Reply all
Reply to author
Forward
0 new messages