迭代结果结果

时间:2018-11-26 17:50:47

标签: ansible

当我在Ansible中使用以下内容时,

 with_items:
        - "{{ result1.results }}"

这是结果

"stderr": "",
"stderr_lines": [],
"stdout": "Total Cases : 139, Total Success Cases : 137, Total Failure Cases : 2, Success Percent : 98.56%, ,",
"stdout_lines": ["Total Cases : 139, Total Success Cases : 137, Total Failure Cases : 2, Success Percent : 98.56%, ,"]

再次如何迭代"stdout_lines"

1 个答案:

答案 0 :(得分:0)

如果要迭代列表,请使用loop或with_items构造。

这是我创建的一个简单角色,用于显示ps

---
- name: Getting ps output
  shell: ps
  register: o
- name: Debug
  debug: var=o
- name: Display stdout of ps
  shell: echo "{{ item }}"
  loop: "{{ o.stdout_lines }}"
...

我使用剧本来扮演角色:

---
- hosts: localhost
  roles:
    - ps
...

以下是命令和输出:

ansible-playbook ./testps.yml

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

TASK [Gathering Facts] *********************************************************
ok: [localhost]

TASK [ps : Getting ps output] **************************************************
changed: [localhost]

TASK [ps : Debug] **************************************************************
ok: [localhost] => {
    "o": {
        "changed": true,
        "cmd": "ps",
        "delta": "0:00:00.014101",
        "end": "2018-11-26 13:28:54.858550",
        "failed": false,
        "rc": 0,
        "start": "2018-11-26 13:28:54.844449",
        "stderr": "",
        "stderr_lines": [],
        "stdout": "  PID TTY          TIME CMD\n  300 pts/0    00:00:00 sh\n  301 pts/0    00:00:00 python\n  302 pts/0    00:00:00 python\n  303 pts/0    00:00:00 sh\n  318 pts/0    00:00:00 ps\n 5229 pts/0    00:00:00 bash\n 5576 pts/0    00:00:00 bash\n32689 pts/0    00:00:00 ansible-playboo\n32752 pts/0    00:00:00 ansible-playboo\n32767 pts/0    00:00:00 sh",
        "stdout_lines": [
            "  PID TTY          TIME CMD",
            "  300 pts/0    00:00:00 sh",
            "  301 pts/0    00:00:00 python",
            "  302 pts/0    00:00:00 python",
            "  303 pts/0    00:00:00 sh",
            "  318 pts/0    00:00:00 ps",
            " 5229 pts/0    00:00:00 bash",
            " 5576 pts/0    00:00:00 bash",
            "32689 pts/0    00:00:00 ansible-playboo",
            "32752 pts/0    00:00:00 ansible-playboo",
            "32767 pts/0    00:00:00 sh"
        ]
    }
}

TASK [ps : Display stdout of ps] ***********************************************
changed: [localhost] => (item=  PID TTY          TIME CMD)
changed: [localhost] => (item=  300 pts/0    00:00:00 sh)
changed: [localhost] => (item=  301 pts/0    00:00:00 python)
changed: [localhost] => (item=  302 pts/0    00:00:00 python)
changed: [localhost] => (item=  303 pts/0    00:00:00 sh)
changed: [localhost] => (item=  318 pts/0    00:00:00 ps)
changed: [localhost] => (item= 5229 pts/0    00:00:00 bash)
changed: [localhost] => (item= 5576 pts/0    00:00:00 bash)
changed: [localhost] => (item=32689 pts/0    00:00:00 ansible-playboo)
changed: [localhost] => (item=32752 pts/0    00:00:00 ansible-playboo)
changed: [localhost] => (item=32767 pts/0    00:00:00 sh)

PLAY RECAP *********************************************************************
localhost                  : ok=4    changed=2    unreachable=0    failed=0

ansible文档提供了有关使用循环的更多信息。

希望这会有所帮助。