# finding a phrase through ansible script

**URL:** <https://forum.ansible.com/t/finding-a-phrase-through-ansible-script/27250>\
**Category:** Ansible Project\
**Created:** [April 11, 2018, 3:24pm UTC](https://forum.ansible.com/t/finding-a-phrase-through-ansible-script/27250 "2018-04-11T15:24:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bishwajit\_Samanta](https://avatars.discourse-cdn.com/v4/letter/b/8e7dd6/32.png) [@Bishwajit\_Samanta](https://forum.ansible.com/u/Bishwajit_Samanta)\
**Post date:** [April 11, 2018, 3:24pm UTC](https://forum.ansible.com/t/finding-a-phrase-through-ansible-script/27250/1 "2018-04-11T15:24:15Z")

</div>

i am having a problem in finding solution. I have a piece of file which contains data in below format (json type with quotes)::-

```auto
hostinfo:
  'host001':
    ip: x.x.x.x
    hostclass: 'puppet'
  'host002':
    ip: y.y.y.y
    hostclass: 'ess'
  'host003':
    ip: z.z.z.z
    hostclass: 'mdb'

```

Now my task is to find out the hostname of the system belong to hostclass ‘ess’ , which in this case the answer in host002. I have to write an ansible script to find out the result. Can anyone help me in finding out, or any idea to approach for the problem.

Issue Type:: Finding a phrase through ansible  
Ansible Version:: Ansible 2.4.0  
Component Name:: It looks similar to json type file.

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [April 11, 2018, 3:31pm UTC](https://forum.ansible.com/t/finding-a-phrase-through-ansible-script/27250/2 "2018-04-11T15:31:03Z")

</div>

The following task will find that:

- debug:  
msg: “{{ hostinfo|dictsort|selectattr(‘1.hostclass’, ‘equalto’, ‘ess’)|first|first }}”

That takes the `hostinfo` dict, turns it into a list of tuples, selects where `hostclass` equals `ess`, gets the first result (there should only be one here), and then gets the first element.

If you don’t have jinja2 version with the `equalto` test, you can replace that with `search` or `match`

It outputs:

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: “host002”  
}

---

<div class="post-metadata">

**Author:** ![Bishwajit\_Samanta](https://avatars.discourse-cdn.com/v4/letter/b/8e7dd6/32.png) [@Bishwajit\_Samanta](https://forum.ansible.com/u/Bishwajit_Samanta)\
**Post date:** [April 11, 2018, 4:06pm UTC](https://forum.ansible.com/t/finding-a-phrase-through-ansible-script/27250/3 "2018-04-11T16:06:13Z")

</div>

I am glad you replied so fast, but sorry to say i am not an ansible expert . I am just a beginner . I just typed the code. Can you help me in understanding where i am not correct.

---

<div class="post-metadata">

**Author:** ![Bishwajit\_Samanta](https://avatars.discourse-cdn.com/v4/letter/b/8e7dd6/32.png) [@Bishwajit\_Samanta](https://forum.ansible.com/u/Bishwajit_Samanta)\
**Post date:** [April 11, 2018, 4:47pm UTC](https://forum.ansible.com/t/finding-a-phrase-through-ansible-script/27250/4 "2018-04-11T16:47:55Z")

</div>

@Matt Martz : I am sorry to ask silly questions, regarding ansible as by seeing the small piece of code, i can understand you must be an ansible expert. If possible you can help me in my problem explanation a bit.

And any such links for learning you want to refer, please refer me. As i googled selectattr but first|first parameter which you have passed i am not getting any good document. If you can help me in referring such, it would be of very nice help.

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [April 11, 2018, 4:55pm UTC](https://forum.ansible.com/t/finding-a-phrase-through-ansible-script/27250/5 "2018-04-11T16:55:54Z")

</div>

Templating in Ansible is handled by Jinja2. You can find documentation on Jinja2 at [http://jinja.pocoo.org/docs/dev/templates/](http://jinja.pocoo.org/docs/dev/templates/)

For the `first` filter specifically: [http://jinja.pocoo.org/docs/dev/templates/#first](http://jinja.pocoo.org/docs/dev/templates/#first)

---

<div class="post-metadata">

**Author:** ![Bishwajit\_Samanta](https://avatars.discourse-cdn.com/v4/letter/b/8e7dd6/32.png) [@Bishwajit\_Samanta](https://forum.ansible.com/u/Bishwajit_Samanta)\
**Post date:** [April 11, 2018, 5:35pm UTC](https://forum.ansible.com/t/finding-a-phrase-through-ansible-script/27250/6 "2018-04-11T17:35:06Z")

</div>

Hi, thanks for sharing the docs link. In the meanwhile i am not able to go in the right direction of solving the problem. so far what i have done is ::

1. converted the text file to json through python.
2. write some ansible codes, which in return it is dumping the contents but i am not able to get the thing i want.
