# need help in checking if a variable string is in an item string

**URL:** <https://forum.ansible.com/t/need-help-in-checking-if-a-variable-string-is-in-an-item-string/21757>\
**Category:** Ansible Project\
**Created:** [June 13, 2016, 5:50pm UTC](https://forum.ansible.com/t/need-help-in-checking-if-a-variable-string-is-in-an-item-string/21757 "2016-06-13T17:50:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jyanga](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jyanga/32/8108_2.png) [@jyanga](https://forum.ansible.com/u/jyanga)\
**Post date:** [June 13, 2016, 5:50pm UTC](https://forum.ansible.com/t/need-help-in-checking-if-a-variable-string-is-in-an-item-string/21757/1 "2016-06-13T17:50:10Z")

</div>

Here is what I have.

vars/main.yml  
host\_entries:

- { srv\_loc: ‘ca’, ip\_addr: ‘10.1.1.1’, host\_aliases: ‘[ca-srv-01.example.com](http://ca-srv-01.example.com) ca-srv-01’ }
- { srv\_loc: ‘ny’, ip\_addr: ‘10.2.1.1’, host\_aliases: ‘[ny-srv-01.example.com](http://ny-srv-01.example.com) ny-srv-01’ }
- { srv\_loc: ‘co’, ip\_addr: ‘10.3.1.1’, host\_aliases: ‘[co-srv-01.example.com](http://co-srv-01.example.com) co-srv-01’ }

tasks/main.yml

---

<div class="post-metadata">

**Author:** ![jyanga](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jyanga/32/8108_2.png) [@jyanga](https://forum.ansible.com/u/jyanga)\
**Post date:** [June 14, 2016, 2:26pm UTC](https://forum.ansible.com/t/need-help-in-checking-if-a-variable-string-is-in-an-item-string/21757/2 "2016-06-14T14:26:22Z")

</div>

I apologize for not providing the info below.

version: 2.0.2.0

event:  
skipping: [ca-server1] =\> (item={u’host\_aliases’: u’[ca-srv-01.example.com](http://ca-srv-01.example.com/) ca-srv-01’, u’ip\_addr’: u’10.1.1.1’, u’srv\_loc’: u’ca’}) =\> {“changed”: false, “item”: {“host\_aliases”: “[ca-srv-01.example.com](http://ca-srv-01.example.com/) ca-srv-01”, “ip\_addr”: “10.1.1.1”, “srv\_loc”: “ca”}, “skip\_reason”: “Conditional check failed”, “skipped”: true}

Regards,  
j

---

<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:** [June 14, 2016, 3:44pm UTC](https://forum.ansible.com/t/need-help-in-checking-if-a-variable-string-is-in-an-item-string/21757/3 "2016-06-14T15:44:55Z")

</div>

> vars/main.yml  
> host\_entries:

Here you have host\_entries

> &nbsp;&nbsp;- { srv\_loc: 'ca', ip\_addr: '10.1.1.1', host\_aliases:  
> &nbsp;&nbsp;'ca-srv-01.example.com ca-srv-01' }  
> &nbsp;&nbsp;- { srv\_loc: 'ny', ip\_addr: '10.2.1.1', host\_aliases:  
> &nbsp;&nbsp;'ny-srv-01.example.com ny-srv-01' }  
> &nbsp;&nbsp;- { srv\_loc: 'co', ip\_addr: '10.3.1.1', host\_aliases:  
> &nbsp;&nbsp;'co-srv-01.example.com co-srv-01' }
> 
> tasks/main.yml  
> ---
> 
> &nbsp;&nbsp;- name: checking location of server  
> &nbsp;&nbsp;&nbsp;&nbsp;raw: 'echo "{{ ansible\_hostname }}" | cut -f1 -d"-"'  
> &nbsp;&nbsp;&nbsp;&nbsp;register: actual\_loc
> 
> &nbsp;&nbsp;- name: adding entries in the hosts file  
> &nbsp;&nbsp;&nbsp;&nbsp;lineinfile:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;backup: yes  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;create: yes  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /etc/hosts  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;group: root  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line: "{{item.ip\_addr}} {{item.host\_aliases}}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mode: 0644  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;owner: root  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: present  
> &nbsp;&nbsp;&nbsp;&nbsp;when: ( actual\_loc.stdout\_lines == item.srv\_loc )  
> &nbsp;&nbsp;&nbsp;&nbsp;with\_items: "{{ hosts\_entries }}"

But here you have hosts\_entries, so one of them is a typo.

You could lose the raw by doing so in the when  
when: ansible\_hostname.split('-')[0] == item.srv\_loc

---

<div class="post-metadata">

**Author:** ![jyanga](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jyanga/32/8108_2.png) [@jyanga](https://forum.ansible.com/u/jyanga)\
**Post date:** [June 14, 2016, 5:53pm UTC](https://forum.ansible.com/t/need-help-in-checking-if-a-variable-string-is-in-an-item-string/21757/4 "2016-06-14T17:53:45Z")

</div>

Thanks, Kai.

You hit the nail on the head. If there is a doc on how to do this string manipulation, please share with me the URL.

Regards,  
j

---

<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:** [June 14, 2016, 8:09pm UTC](https://forum.ansible.com/t/need-help-in-checking-if-a-variable-string-is-in-an-item-string/21757/5 "2016-06-14T20:09:52Z")

</div>

I have just picked it up, but I guess it's a Python string method  
[https://docs.python.org/2.7/library/stdtypes.html?highlight=split#str.split](https://docs.python.org/2.7/library/stdtypes.html?highlight=split#str.split)

Maybe all the Python string methods work, hopefully someone else can shed some light on that.

---

<div class="post-metadata">

**Author:** ![jyanga](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jyanga/32/8108_2.png) [@jyanga](https://forum.ansible.com/u/jyanga)\
**Post date:** [June 14, 2016, 10:12pm UTC](https://forum.ansible.com/t/need-help-in-checking-if-a-variable-string-is-in-an-item-string/21757/6 "2016-06-14T22:12:11Z")

</div>

Kai,

Thanks. You have been very helpful.

Regards,  
j
