On 27.02.2019 22:53, Jeffrey Agnitsch wrote:
> Hello,
>
> I am trying to use hostvars to get an inventory hostname and a specific
> variable. The goal of this is to get from the app group, get each unique
> servers client_name. The below command works for just the first line as I
> am fairly confident the 0 means first item in the inventory file.
>
> I need to get it to be a per line item of the inventory file so that each
> servers client name is read out. I have tried a couple variations but I
> can't seem to get this correct. Can anyone provide any insight as to what I
> might be doing wrong.
>
> "{{ hostvars[groups['apps'][0]]['client_name'] }}"
>
> inventory file
> [apps]
> server1 instance_user=fondinst client_name=fondprd1 server_ip=
> server2 instance_user=kumcinst client_name=kumcprd1 `server_ip=
> [proxy]
> proxy1
>
> Playbook
>
> * hosts: 127.0.0.1
> connection: local
> gather_facts: no
> * hosts: app
> tasks:
>
> * name: debug
> debug: msg={{ client_name }}
> * hosts: proxy
> tasks:
>
> * name: debug
> debug: msg="{{ hostvars[groups['apps'][0]]['client_name'] }}"
> Summary
> Correct syntax for hostvars calling from a certain group, for an
> inventory_hostname name, getting a variable
> Linux Rhel6
You need to loop over groups['apps'] with with_items.
- debug: msg="{{ item['client_name'] }}"
with_items: "{{ groups['apps'] }}"
--
Kai Stian Olstad
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/e075662b-ef39-dd63-1da5-418b8ee009ba%40olstad.com.
For more options, visit https://groups.google.com/d/optout.