Problem with conditional

255 views
Skip to first unread message

Mikael Sandström

unread,
Sep 14, 2014, 2:09:21 PM9/14/14
to ansible...@googlegroups.com

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
       
...
       
       
---
 
- name: Check if line exists
    shell
: grep {{ item.value.myvalue }} /some/file |wc -l
    with_dict
: dictname
   
register: check

 
- debug: var=check.stdout
    with_items
: check.results


 
- name: Mytask
   
template: src=asdad.j2 dest=/tmp/asdads .......
    with_dict
: dictname
   
when: check.stdout != "1"



I'm not sure how to reference the result from the check in the 'when' in mytask. If I try the above I get this:

error while evaluating conditional: check.stdout != "1"

Any ideas?

Thanks!
/Micke





James Cammarata

unread,
Sep 15, 2014, 5:29:01 PM9/15/14
to ansible...@googlegroups.com
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.



--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/2fd9b8f8-83dc-410d-bc94-6833c9b32b48%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mikael Sandström

unread,
Sep 16, 2014, 11:37:36 AM9/16/14
to ansible...@googlegroups.com
Hi James,

Awesome! Worked like a charm.

Thank you!
/Micke
Reply all
Reply to author
Forward
0 new messages