# Hostvars to pull specific variables

**URL:** <https://forum.ansible.com/t/hostvars-to-pull-specific-variables/29795>\
**Category:** Ansible Project\
**Created:** [February 27, 2019, 9:53pm UTC](https://forum.ansible.com/t/hostvars-to-pull-specific-variables/29795 "2019-02-27T21:53:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeffrey\_Agnitsch](https://avatars.discourse-cdn.com/v4/letter/j/b487fb/32.png) [@Jeffrey\_Agnitsch](https://forum.ansible.com/u/Jeffrey_Agnitsch)\
**Post date:** [February 27, 2019, 9:53pm UTC](https://forum.ansible.com/t/hostvars-to-pull-specific-variables/29795/1 "2019-02-27T21:53:56Z")

</div>

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

Ansible version 2.4.4.4

component name:  
Ansible

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 27, 2019, 10:42pm UTC](https://forum.ansible.com/t/hostvars-to-pull-specific-variables/29795/2 "2019-02-27T22:42:58Z")

</div>

You need to loop over groups['apps'] with with\_items.

&nbsp;&nbsp;- debug: msg="{{ item['client\_name'] }}"  
&nbsp;&nbsp;&nbsp;&nbsp;with\_items: "{{ groups['apps'] }}"

---

<div class="post-metadata">

**Author:** ![Jeffrey\_Agnitsch](https://avatars.discourse-cdn.com/v4/letter/j/b487fb/32.png) [@Jeffrey\_Agnitsch](https://forum.ansible.com/u/Jeffrey_Agnitsch)\
**Post date:** [February 28, 2019, 1:35pm UTC](https://forum.ansible.com/t/hostvars-to-pull-specific-variables/29795/3 "2019-02-28T13:35:14Z")

</div>

> When running the command it stated there was an undefined variable I made the below changes to the playbook

=\> {“msg”: "The task includes an option with an undefined variable. The error was: ‘ansible.utils.unsafe\_proxy.AnsibleUnsafeText object’ has no attribute ‘client\_name’\n\nThe error appears to have been in ‘/etc/ansible/testing/prod\_inov2.yml’

inventory file  
[apps]  
server1 instance\_user=fondinst client\_name=fondprd1 server\_ip=  
server2 instance\_user=kumcinst client\_name=kumcprd1 `server\_ip=  
[proxy]  
proxy1

- hosts: 127.0.0.1  
connection: local  
gather\_facts: no

- hosts: apps  
tasks:

- name: debug  
debug: msg={{ client\_name }}

- hosts: proxy  
tasks:

- debug: msg=“{{ item[‘client\_name’] }}”  
with\_items: “{{ groups[‘apps’] }}”

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 28, 2019, 7:24pm UTC](https://forum.ansible.com/t/hostvars-to-pull-specific-variables/29795/4 "2019-02-28T19:24:26Z")

</div>

Sorry about that it should of course be

&nbsp;&nbsp;- debug: msg="{{ hostvars[item]['client\_name'] }}"  
&nbsp;&nbsp;&nbsp;&nbsp;with\_items: "{{ groups['apps'] }}"

---

<div class="post-metadata">

**Author:** ![Jeffrey\_Agnitsch](https://avatars.discourse-cdn.com/v4/letter/j/b487fb/32.png) [@Jeffrey\_Agnitsch](https://forum.ansible.com/u/Jeffrey_Agnitsch)\
**Post date:** [March 1, 2019, 2:32pm UTC](https://forum.ansible.com/t/hostvars-to-pull-specific-variables/29795/5 "2019-03-01T14:32:16Z")

</div>

> Yeah that worked for a debugging scenario so awesome and thanks for that. Might you have any idea how to use those called variables to edit text files? Namely here is the bigger playbook I am trying within the proxy role. I am trying to have it change the name of the .conf file as well as manipulate text within the prod\_proxy\_conf.j2. It keeps stating that there is an undefined variable however any thoughts?

proxy role main.yml

- name: " Proxy | Setup Proxy"  
template:  
src: /etc/ansible/files/proxy\_files/prod\_proxy\_conf.j2  
dest: /etc/httpd/conf.d/ “{{ hostvars[item][‘client\_name’] }}”.conf  
with\_items: “{{ groups[‘app’] }}”

- name: Proxy | Restart Proxy  
command: apachectl graceful

- name: Clone template and customize  
delegate\_to: 127.0.0.1  
vmware\_guest:  
hostname: “{{ vcenter\_hostname }}”  
username: “{{ vcenter\_username }}”  
password: “{{ vcenter\_password }}”  
folder: “NECO-App”  
validate\_certs: “false”  
datacenter: “{{ vcenter\_datacenter }}”  
cluster: “{{ vcenter\_cluster }}”  
state: “poweredon”  
name: “{{ inventory\_hostname }}”  
template: “{{ app\_vsphere\_template }}”  
networks:

- name: “{{ app\_network }}”  
ip: “{{ server\_ip }}”  
netmask: “{{ servers\_netmask }}”  
gateway: “{{ app\_gateway }}”  
domain: “{{ app\_domain }}”  
dns\_servers:

- “{{ dns\_lookup }}”  
wait\_for\_ip\_address: yes

- name: add to in memory inventory  
add\_host:  
hostname: “{{ item }}”  
groups: new  
with\_items: “{{ groups.app }}”  
tasks:  
roles:

- { role: common }

- { role: create\_instance }

- { role: setup\_tpms }

- { role: upgrade\_jboss }

- hosts: proxy  
tasks:  
roles:

- { role: proxy }

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 1, 2019, 2:45pm UTC](https://forum.ansible.com/t/hostvars-to-pull-specific-variables/29795/6 "2019-03-01T14:45:23Z")

</div>

> > Yeah that worked for a debugging scenario so awesome and thanks for that.  
> > Might you have any idea how to use those called variables to edit text  
> > files? Namely here is the bigger playbook I am trying within the proxy  
> > role. I am trying to have it change the name of the .conf file as well as  
> > manipulate text within the prod\_proxy\_conf.j2. It keeps stating that there  
> > is an undefined variable however any thoughts?

Without the actual output it's impossible to help.

> proxy role main.yml  
> - name: " Proxy | Setup Proxy"  
> &nbsp;&nbsp;&nbsp;template:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: /etc/ansible/files/proxy\_files/prod\_proxy\_conf.j2  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /etc/httpd/conf.d/ "{{ hostvars[item]['client\_name'] }}".conf

You can't use double quotes in dest, unless you filename actually contains quotes.  
&nbsp;&nbsp;dest: /etc/httpd/conf.d/{{ hostvars[item]['client\_name'] }}.conf
