# unable to fit the piece of code in ansible

**URL:** <https://forum.ansible.com/t/unable-to-fit-the-piece-of-code-in-ansible/27254>\
**Category:** Ansible Project\
**Created:** [April 12, 2018, 2:51am UTC](https://forum.ansible.com/t/unable-to-fit-the-piece-of-code-in-ansible/27254 "2018-04-12T02:51:32Z")\
**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 12, 2018, 2:51am UTC](https://forum.ansible.com/t/unable-to-fit-the-piece-of-code-in-ansible/27254/1 "2018-04-12T02:51:32Z")

</div>

Hi all,

I am a beginner in ansible. If anyone can help me in finding the answer. It would be great. My problem was finding a phrase in ansible which one of the person have helped me. But unfortunately i am not able to fit the piece of code in my playbook.

hostinfo:  
host001’:  
ip: 192.168.43.10  
hostclass: ‘puppet’  
‘host002’:  
ip: 192.168.43.11  
hostclass: ‘ess’  
‘host003’:  
ip: 192.168.43.21  
hostclass: ‘mdb’

the code was::

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

Now i am writing the code as given below. I know in this piece of code i am making mistakes. But since i am a beginner i am not able to understand it. Can anyone help me?

---

<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 12, 2018, 11:58am UTC](https://forum.ansible.com/t/unable-to-fit-the-piece-of-code-in-ansible/27254/2 "2018-04-12T11:58:42Z")

</div>

Any one can suggest any idea please…

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [April 12, 2018, 12:22pm UTC](https://forum.ansible.com/t/unable-to-fit-the-piece-of-code-in-ansible/27254/3 "2018-04-12T12:22:02Z")

</div>

Can I suggest that you describe what you want to achieve. That is, describe the end result that you are seeking.

That is more likely to get a useful response.

Regards, K.

---

<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 12, 2018, 1:46pm UTC](https://forum.ansible.com/t/unable-to-fit-the-piece-of-code-in-ansible/27254/4 "2018-04-12T13:46:57Z")

</div>

Thanks Karl, for your reply. Problem statement ::-

My job is to find out the hostname which belongs to hostclass ess, which in this case the answer is host002.

```auto
hostinfo:
  'host001':
    ip: 192.168.43.10
    hostclass: 'puppet'
  'host002':
    ip: 192.168.43.11
    hostclass: 'ess'
  'host003':
    ip: 192.168.43.21
    hostclass: 'mdb'

```

i got one solution from some friend in google ansible groups only, but i am unable to fit it my ansible playbook.

```auto
- debug:
    msg: "{{ hostinfo|dictsort|selectattr('1.hostclass', 'equalto', 'ess')|first|first }}"

```

The playbook i am writing, which is wrong but i am not able to figure out how to fit the code in my playbook. Can anyone help me in modifying the code of mine.

```auto
---

- hosts: somehost
  gather_facts: no
  vars_files:
    file: sometext.yaml
    name: sometext

  - debug:
        msg: "{{ hostinfo|dictsort|selectattr('1.hostclass', 'equalto', 'ess')|first|first }}

```

---

<div class="post-metadata">

**Author:** ![Biswadip\_Dutta](https://avatars.discourse-cdn.com/v4/letter/b/54ee81/32.png) [@Biswadip\_Dutta](https://forum.ansible.com/u/Biswadip_Dutta)\
**Post date:** [April 13, 2018, 6:09am UTC](https://forum.ansible.com/t/unable-to-fit-the-piece-of-code-in-ansible/27254/5 "2018-04-13T06:09:51Z")

</div>

Can you please let me know which Ansible version you are using and what error you are getting?

---

<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 13, 2018, 2:52pm UTC](https://forum.ansible.com/t/unable-to-fit-the-piece-of-code-in-ansible/27254/6 "2018-04-13T14:52:14Z")

</div>

Ansible:: 2.4 is the version i am using.
