Get stdout using loop

389 views
Skip to first unread message

Александр Лозовской

unread,
Jun 20, 2014, 11:20:24 PM6/20/14
to ansible...@googlegroups.com
Hi! How i can get stdout using loop (f.e. for with_sequence statement)?

An example of how I want to use it:

Введите код...

  • hosts: 127.0.0.1
    connection: local
    gather_facts: no

    tasks:

    • name: "TEST"
      command: curl -o /dev/null --silent --head --write-out "%{http_code}" http://host"{{ item }}":80
      register: status
      with_sequence: count=4

    • debug: msg="{{ item.stdout }}"


Michael DeHaan

unread,
Jun 22, 2014, 12:08:10 PM6/22/14
to ansible...@googlegroups.com
The above request was registered to a variable called "status", but since you are using with_sequence this is saved in an array.

Take a look at what you have like so:

- debug: var=status

Finally, you'll probably see that you want:

- debug: msg="{{ item.stdout }}
  with_items: status

item doesn't mean anything unless there's a loop in play, so you have to loop over status to use it.






--
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/a6e732c4-0fcb-4bba-9036-674d9fb92b06%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Brian Coca

unread,
Jun 22, 2014, 12:18:58 PM6/22/14
to ansible...@googlegroups.com
status will be a list, so you have 4 stdout​s to look at:

- debug: msg={{item.stdout}}
  with_items: status.results

Александр Лозовской

unread,
Jun 22, 2014, 7:28:24 PM6/22/14
to ansible...@googlegroups.com


воскресенье, 22 июня 2014 г., 20:18:58 UTC+4 пользователь Brian Coca написал:
status will be a list, so you have 4 stdout​s to look at:

- debug: msg={{item.stdout}}
  with_items: status.results

In this case i got all message from  status.results and stdout after it.

I solved this problem through this:

debug: msg="{% for results in status.results %} {{results.cmd.7}} - {{ results.stdout }} {% endfor %}"

In this case, I get what I need, but all 4 stdout output in a single line. How do I put a linebreak after each row (Ansible escapes '\n').

Michael DeHaan

unread,
Jun 23, 2014, 8:09:20 AM6/23/14
to ansible...@googlegroups.com
Do what Brian said.




--
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.

Александр Лозовской

unread,
Jun 23, 2014, 9:25:28 AM6/23/14
to ansible...@googlegroups.com


понедельник, 23 июня 2014 г., 16:09:20 UTC+4 пользователь Michael DeHaan написал:
Do what Brian said.

I've done it.

Playbook:
---
- hosts: 127.0.0.1
  connection: local
  gather_facts: no
  tasks:
  - name: get info
    command: curl -o /dev/null --silent --head --write-out "%{http_code}" "{{item}}"
    register: status
    with_items:

  - debug: var={{item.stdout}}
    with_items: status.results

Results: 

PLAY [127.0.0.1] ************************************************************** 

TASK: [get info] ************************************************************** 
changed: [127.0.0.1] => (item=http://google.com)
changed: [127.0.0.1] => (item=http://ansible.com)

TASK: [debug var={{item.stdout}}] ********************************************* 
ok: [127.0.0.1] => (item={'item': 'http://google.com', u'delta': u'0:00:00.072241', u'cmd': [u'curl', u'-o', u'/dev/null', u'--silent', u'--head', u'--write-out', u'%{http_code}', u'http://google.com'], u'end': u'2014-06-23 17:23:19.005928', u'stderr': u'', u'stdout': u'302', 'invocation': {'module_name': 'command', 'module_args': u'curl -o /dev/null --silent --head --write-out "%{http_code}" "http://google.com"'}, u'changed': True, u'rc': 0, u'start': u'2014-06-23 17:23:18.933687'}) => {
    "302": "302", 
    "item": {
        "changed": true, 
        "cmd": [
            "curl", 
            "-o", 
            "/dev/null", 
            "--silent", 
            "--head", 
            "--write-out", 
            "%{http_code}", 
            "http://google.com"
        ], 
        "delta": "0:00:00.072241", 
        "end": "2014-06-23 17:23:19.005928", 
        "invocation": {
            "module_args": "curl -o /dev/null --silent --head --write-out \"%{http_code}\" \"http://google.com\"", 
            "module_name": "command"
        }, 
        "item": "http://google.com", 
        "rc": 0, 
        "start": "2014-06-23 17:23:18.933687", 
        "stderr": "", 
        "stdout": "302"
    }
}
ok: [127.0.0.1] => (item={'item': 'http://ansible.com', u'delta': u'0:00:00.167076', u'cmd': [u'curl', u'-o', u'/dev/null', u'--silent', u'--head', u'--write-out', u'%{http_code}', u'http://ansible.com'], u'end': u'2014-06-23 17:23:19.212962', u'stderr': u'', u'stdout': u'302', 'invocation': {'module_name': 'command', 'module_args': u'curl -o /dev/null --silent --head --write-out "%{http_code}" "http://ansible.com"'}, u'changed': True, u'rc': 0, u'start': u'2014-06-23 17:23:19.045886'}) => {
    "302": "302", 
    "item": {
        "changed": true, 
        "cmd": [
            "curl", 
            "-o", 
            "/dev/null", 
            "--silent", 
            "--head", 
            "--write-out", 
            "%{http_code}", 
            "http://ansible.com"
        ], 
        "delta": "0:00:00.167076", 
        "end": "2014-06-23 17:23:19.212962", 
        "invocation": {
            "module_args": "curl -o /dev/null --silent --head --write-out \"%{http_code}\" \"http://ansible.com\"", 
            "module_name": "command"
        }, 
        "item": "http://ansible.com", 
        "rc": 0, 
        "start": "2014-06-23 17:23:19.045886", 
        "stderr": "", 
        "stdout": "302"
    }
}

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


Reply all
Reply to author
Forward
0 new messages