Print Hostvar For All Host Under Group

28 views
Skip to first unread message

Jitender J

unread,
Dec 3, 2021, 3:02:19 AM12/3/21
to Ansible Development
Hi Team,

Need your help got stuck 

I want to use host vars from inventory for all host under a specific group

I tried with the below example but only getting IP when I am passing hostname not getting anything when trying to get ansbile_host variable value for all host under that group with the group name 

 ip: "{{hostvars['test']['ansible_host']}}"   ------Not working 
 ip: "{{hostvars['test1']['ansible_host']}}" -------- Working 


[test]
test1 ansible_host=1.1.1.1
test2 ansible_host=2.2.2.2

james livulpi

unread,
Dec 4, 2021, 6:55:52 PM12/4/21
to Ansible Development
Hello,

This doesn't look like its a supported way, you can use index of one and access but not full group:


- Jamer

Jitender J

unread,
Dec 5, 2021, 10:51:32 PM12/5/21
to james livulpi, Ansible Development
Thanks .....any other way to use hosname wildcard 

 "{{hostvars['*test*']['ansible_host']}}"


--
You received this message because you are subscribed to the Google Groups "Ansible Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-deve...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-devel/81261341-b525-4042-b8ba-a68fd5767a2en%40googlegroups.com.

Todd Lewis

unread,
Dec 6, 2021, 11:24:09 AM12/6/21
to Ansible Development
Is this what you're looking for?
$ cat groupvars-inventory.ini
[test]
test1 ansible_host=1.1.1.1
test2 ansible_host=2.2.2.2

[dev]
dev1 ansible_host=3.3.3.3
dev2 ansible_host=4.4.4.4

$ cat groupvars.yml
---
- name: Demo for accessing inventory group variables
  # ansible-playbook groupvars.yml -i ./groupvars-inventory.ini -v
  hosts: localhost
  tasks:
    - name: Gather the ansible_host values from hosts in the test group
      set_fact:
        test_ips: |
          {%- set ip=[] -%}
          {%- for h in groups['test'] -%}
          {%-   set _ = ip.append(hostvars[h]['ansible_host']) -%}
          {%- endfor %}{{ ip }}

    # TASK [Gather the ansible_host values from hosts in the test group] ****
    # ok: [localhost] => changed=false
    #   ansible_facts:
    #     test_ips:
    #     - 1.1.1.1
    #     - 2.2.2.2



    - name: Same thing but with legit filters in the dev group
      set_fact:
        dev_ips: "{{ groups['dev']|map('extract',hostvars,'ansible_host') }}"

    # TASK [Same thing but with legit filters in the dev group] ****
    # ok: [localhost] => changed=false
    #   ansible_facts:
    #     dev_ips:
    #     - 3.3.3.3
    #     - 4.4.4.4


On Friday, December 3, 2021 at 3:02:19 AM UTC-5 jiten...@zscaler.com wrote:

Jitender J

unread,
Dec 7, 2021, 3:41:05 AM12/7/21
to Todd Lewis, Ansible Development
Thanks @Todd, First solution is working for me.... thank you very much 

--
You received this message because you are subscribed to the Google Groups "Ansible Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-deve...@googlegroups.com.


--
Regards,
Jitender
Reply all
Reply to author
Forward
0 new messages