Ansible显示变量的内容

时间:2014-08-26 09:39:57

标签: ansible ansible-playbook

如果我有这两项任务

- name: Replace ServerIP in config_file on OTHER NODES
  set_fact:
    variable: "{{hostvars.localhost.new_ips.results}}"

- name: Display variable
  debug: var=variable

结果是:

TASK: [Display variable] ********************************************************* 
ok: [vm2] => {
    "variable": [
        {
            "changed": true, 
            "cmd": "echo \"11.11.4.74\"", 
            "delta": "0:00:00.002244", 
            "end": "2014-08-26 02:34:22.880447", 
            "invocation": {
                "module_args": "echo \"11.11.4.74\"", 
                "module_name": "shell"
            }, 
            "item": "74", 
            "rc": 0, 
            "start": "2014-08-26 02:34:22.878203", 
            "stderr": "", 
            "stdout": "11.11.4.74"
        }, 
        {
            "changed": true, 
            "cmd": "echo \"11.11.4.138\"", 
            "delta": "0:00:00.002156", 
            "end": "2014-08-26 02:34:22.958337", 
            "invocation": {
                "module_args": "echo \"11.11.4.138\"", 
                "module_name": "shell"
            }, 
            "item": "138", 
            "rc": 0, 
            "start": "2014-08-26 02:34:22.956181", 
            "stderr": "", 
            "stdout": "11.11.4.138"
        }
    ]
}
ok: [vm1] => {
    "variable": [
        {
            "changed": true, 
            "cmd": "echo \"11.11.4.74\"", 
            "delta": "0:00:00.002244", 
            "end": "2014-08-26 02:34:22.880447", 
            "invocation": {
                "module_args": "echo \"11.11.4.74\"", 
                "module_name": "shell"
            }, 
            "item": "74", 
            "rc": 0, 
            "start": "2014-08-26 02:34:22.878203", 
            "stderr": "", 
            "stdout": "11.11.4.74"
        }, 
        {
            "changed": true, 
            "cmd": "echo \"11.11.4.138\"", 
            "delta": "0:00:00.002156", 
            "end": "2014-08-26 02:34:22.958337", 
            "invocation": {
                "module_args": "echo \"11.11.4.138\"", 
                "module_name": "shell"
            }, 
            "item": "138", 
            "rc": 0, 
            "start": "2014-08-26 02:34:22.956181", 
            "stderr": "", 
            "stdout": "11.11.4.138"
        }
    ]
}

然后我怎样才能访问变量的stdout部分。请注意,我只需要这个变量的标准输出部分,即11.11.4.74和11.11.4.138(最好是循环)

1 个答案:

答案 0 :(得分:3)

您可以单独访问

{{ variable[0].stdout }}

和     {{variable [1] .stdout}}

或使用循环

  - debug: var=item.stdout
    with_items: variable
相关问题