Ansible displays the contents of a variable

If I have two tasks

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

- name: Display variable
  debug: var=variable

Result:

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"
        }
    ]
}

Then how can I access only the stdout part of the variable. Please note that I just need the stdout part of this variable ie 11.11.4.74 and 11.11.4.138 (preferably in a loop)

+4
source share
1 answer

You can either access it individually

{{ variable[0].stdout }}

and {{variable [1] .stdout}}

OR use a loop

  - debug: var=item.stdout
    with_items: variable
+3
source

All Articles