# pick the hostname dynamically from facts and match with inventory hostname and if matched then run the command

**URL:** <https://forum.ansible.com/t/pick-the-hostname-dynamically-from-facts-and-match-with-inventory-hostname-and-if-matched-then-run-the-command/26915>\
**Category:** Ansible Project\
**Created:** [March 7, 2018, 5:31am UTC](https://forum.ansible.com/t/pick-the-hostname-dynamically-from-facts-and-match-with-inventory-hostname-and-if-matched-then-run-the-command/26915 "2018-03-07T05:31:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rraka1002](https://avatars.discourse-cdn.com/v4/letter/r/bcef8e/32.png) [@rraka1002](https://forum.ansible.com/u/rraka1002)\
**Post date:** [March 7, 2018, 5:31am UTC](https://forum.ansible.com/t/pick-the-hostname-dynamically-from-facts-and-match-with-inventory-hostname-and-if-matched-then-run-the-command/26915/1 "2018-03-07T05:31:27Z")

</div>

I’m looking for an idea to spill the hostname infromation geting from facts and run the command if the matches the hostname provided in inventory.  
Saying that “myhostname” value should be picked & matched from inventory ones and get the IP of the hostname once matched to complete the command  
My Host inventory:

test-host1  
test-host2

somecommand hostinfo=myhostname:IP\_ADDRESS \<CMD\_ARGS\>  
somecommand hostinfo=test-host1:xx.xx.xx.xx CIC01

Exaplaination:

somecommand = Static Value  
hostinfo = Static Value  
myhostname = Should be the same as taken from inventory  
IP\_ADDRESS = Should be the IP of Hostname taken from invetory  
CMD\_ARGS = Static Value ( like i mentioned CIC01)

- name: Ansible to get the resolv.conf output  
hosts: all  
remote\_user: root  
gather\_facts: True  
tasks:
- name: runing re-image  
shell: somecommand hostinfo=test-host1:xx.xx.xx.xx CIC01

register: named

- name: Storing the remote Hosts Output locally  
lineinfile: create=yes dest=/opt/out.logs line=“{{ named.stdout }}”  
delegate\_to: 127.0.0.1

---

<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 7, 2018, 6:47am UTC](https://forum.ansible.com/t/pick-the-hostname-dynamically-from-facts-and-match-with-inventory-hostname-and-if-matched-then-run-the-command/26915/2 "2018-03-07T06:47:09Z")

</div>

> I'm looking for an idea to spill the hostname infromation geting from facts  
> and run the command if the matches the hostname provided in inventory.  
> Saying that "myhostname" value should be picked & matched from inventory  
> ones and get the IP of the hostname once matched to complete the command

A good start i this  
[https://docs.ansible.com/ansible/latest/playbooks\_variables.html](https://docs.ansible.com/ansible/latest/playbooks_variables.html)

> My Host inventory:
> 
> test-host1  
> test-host2

Try running ansible ad-hoc to see what facts you have available  
ansible test-host1 -m setup  
and/or  
ansible localhost -m setup

> somecommand hostinfo=myhostname:IP\_ADDRESS \<CMD\_ARGS\>  
> somecommand hostinfo=test-host1:xx.xx.xx.xx CIC01
> 
> Exaplaination:
> 
> somecommand = Static Value  
> hostinfo = Static Value  
> myhostname = Should be the same as taken from inventory

{{ inventory\_hostname }}

> IP\_ADDRESS = Should be the IP of Hostname taken from invetory

There are different once, check with the ad-hoc command above.  
One is {{ ansible\_default\_ipv4.address }}

---

<div class="post-metadata">

**Author:** ![rraka1002](https://avatars.discourse-cdn.com/v4/letter/r/bcef8e/32.png) [@rraka1002](https://forum.ansible.com/u/rraka1002)\
**Post date:** [March 7, 2018, 8:00am UTC](https://forum.ansible.com/t/pick-the-hostname-dynamically-from-facts-and-match-with-inventory-hostname-and-if-matched-then-run-the-command/26915/3 "2018-03-07T08:00:11Z")

</div>

Thanks Kai.
