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.