Khan
Khan

Reputation: 474

Ansible display the contents of a variable

If I have these 2 task

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

- name: Display variable
  debug: var=variable

The result of which are:

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 the stdout part of variable only. Please note, I just require the stdout part of this variable i.e 11.11.4.74 and 11.11.4.138 (in a loop preferably)

Upvotes: 4

Views: 14251

Answers (1)

Ramesh Raithatha
Ramesh Raithatha

Reputation: 544

You can either access it individually

{{ variable[0].stdout }}

and {{ variable[1].stdout }}

OR use loop

  - debug: var=item.stdout
    with_items: variable

Upvotes: 3

Related Questions