# Problem with conditional

**URL:** <https://forum.ansible.com/t/problem-with-conditional/16219>\
**Category:** Ansible Project\
**Created:** [September 14, 2014, 6:09pm UTC](https://forum.ansible.com/t/problem-with-conditional/16219 "2014-09-14T18:09:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![oravirt](https://avatars.discourse-cdn.com/v4/letter/o/3e96dc/32.png) [@oravirt](https://forum.ansible.com/u/oravirt)\
**Post date:** [September 14, 2014, 6:09pm UTC](https://forum.ansible.com/t/problem-with-conditional/16219/1 "2014-09-14T18:09:21Z")

</div>

Hi,

I’ve got a dictionary as below. I loop through that dictionary using a value and grep a file searching to see if the value already exists. If it doesnt I want to use that value in the next task (mytask) to do something. If the value exists should just skip that value and move on.  
The grep command can only be 0 or 1 and I can see in the debug that is:  
“stdout”: “0” #\<-- value doesnt exists  
or  
“stdout”: “1” #\<-- value exists

`  
dictname:  
mykey1:  
myvalue: foo  
mykey2:  
myvalue: bar  
mykey3:  
myvalue: blerg  
…

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [September 15, 2014, 9:28pm UTC](https://forum.ansible.com/t/problem-with-conditional/16219/2 "2014-09-15T21:28:50Z")

</div>

Hi Mikael,

Using with\_together as follows works for the example you’ve shown above:

- hosts: localhost  
gather\_facts: no  
vars:  
dictname:  
mykey1:  
myvalue: foo  
mykey2:  
myvalue: bar  
mykey3:  
myvalue: blerg  
tasks:

- name: Check if line exists  
shell: grep {{ item.value.myvalue }} /tmp/test\_foo | wc -l  
with\_dict: dictname  
register: check

- name: Mytask  
shell: echo “the value for this is {{dictname[item.0].myvalue}}”  
when: item.1.stdout != “1”  
with\_together:

- dictname

- check.results

The odd part is having to use the “item.0” as the key to the dictionary, which make sense when you consider that with\_together iterates over the objects together, so for a dictionary the result of that iteration would be the keys rather than the values.

Setting up a test file with simply “blerg” in it, I get the following output when running the above playbook:

# echo blerg \> /tmp/test\_foo

# ansible-playbook -vv test\_mikael\_ml.yml

PLAY [localhost] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK: [Check if line exists] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
\<127.0.0.1\> REMOTE\_MODULE command grep blerg /tmp/test\_foo | wc -l #USE\_SHELL  
changed: [127.0.0.1] =\> (item={‘key’: ‘mykey3’, ‘value’: {‘myvalue’: ‘blerg’}}) =\> {“changed”: true, “cmd”: “grep blerg /tmp/test\_foo | wc -l”, “delta”: “0:00:00.003378”, “end”: “2014-09-15 16:22:12.263595”, “item”: {“key”: “mykey3”, “value”: {“myvalue”: “blerg”}}, “rc”: 0, “start”: “2014-09-15 16:22:12.260217”, “stderr”: “”, “stdout”: “1”, “warnings”: }  
\<127.0.0.1\> REMOTE\_MODULE command grep bar /tmp/test\_foo | wc -l #USE\_SHELL  
changed: [127.0.0.1] =\> (item={‘key’: ‘mykey2’, ‘value’: {‘myvalue’: ‘bar’}}) =\> {“changed”: true, “cmd”: “grep bar /tmp/test\_foo | wc -l”, “delta”: “0:00:00.003305”, “end”: “2014-09-15 16:22:12.334522”, “item”: {“key”: “mykey2”, “value”: {“myvalue”: “bar”}}, “rc”: 0, “start”: “2014-09-15 16:22:12.331217”, “stderr”: “”, “stdout”: “0”, “warnings”: }  
\<127.0.0.1\> REMOTE\_MODULE command grep foo /tmp/test\_foo | wc -l #USE\_SHELL  
changed: [127.0.0.1] =\> (item={‘key’: ‘mykey1’, ‘value’: {‘myvalue’: ‘foo’}}) =\> {“changed”: true, “cmd”: “grep foo /tmp/test\_foo | wc -l”, “delta”: “0:00:00.003385”, “end”: “2014-09-15 16:22:12.406804”, “item”: {“key”: “mykey1”, “value”: {“myvalue”: “foo”}}, “rc”: 0, “start”: “2014-09-15 16:22:12.403419”, “stderr”: “”, “stdout”: “0”, “warnings”: }

TASK: [Mytask] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
skipping: [127.0.0.1] =\> (item=[‘mykey3’, {u’cmd’: u’grep blerg /tmp/test\_foo | wc -l’, u’end’: u’2014-09-15 16:22:12.263595’, u’stderr’: u’‘, u’stdout’: u’1’, u’changed’: True, u’rc’: 0, ‘item’: {‘value’: {‘myvalue’: ‘blerg’}, ‘key’: ‘mykey3’}, u’warnings’: , u’delta’: u’0:00:00.003378’, ‘invocation’: {‘module\_name’: u’shell’, ‘module\_args’: u’grep blerg /tmp/test\_foo | wc -l’}, u’start’: u’2014-09-15 16:22:12.260217’}])  
\<127.0.0.1\> REMOTE\_MODULE command echo “the value for this is bar” #USE\_SHELL  
\<127.0.0.1\> REMOTE\_MODULE command echo “the value for this is foo” #USE\_SHELL

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
127.0.0.1 : ok=2 changed=2 unreachable=0 failed=0

I truncated the output there at the end to save some space, the main point was to show it skipping the “blerg” item in that task.

---

<div class="post-metadata">

**Author:** ![oravirt](https://avatars.discourse-cdn.com/v4/letter/o/3e96dc/32.png) [@oravirt](https://forum.ansible.com/u/oravirt)\
**Post date:** [September 16, 2014, 3:37pm UTC](https://forum.ansible.com/t/problem-with-conditional/16219/3 "2014-09-16T15:37:36Z")

</div>

Hi James,

Awesome! Worked like a charm.

Thank you!  
/Micke
