-1

We want to capture variables from Ansible output. We have a bulk dns record and wanted to capture FQDN and IP address from output but were unable to display it. Tasks are below:

    - name: Run DNS Lookup
      command: dig +short "{{ item }}"
      register: dns_output
      loop: "{{ dns_records.stdout_lines }}"

    - name: Display DNS Results
      debug:
        msg: "FQDN: {{ item.item }}, IP Address: {{ item.stdout }}"
      with_items: "{{ dns_output.results }}"

++++++ansible output+++++

changed: [localhost] => (item=www.facebook.com) => {
    "ansible_loop_var": "item",
    "changed": true,
    "cmd": [
        "dig",
        "+short",
        "www.facebook.com"
    ],
    "delta": "0:00:00.028353",
    "end": "2023-07-08 15:44:18.486735",
    "invocation": {
        "module_args": {
            "_raw_params": "dig +short \"www.facebook.com\"",
            "_uses_shell": false,
            "argv": null,
            "chdir": null,
            "creates": null,
            "executable": null,
            "removes": null,
            "stdin": null,
            "stdin_add_newline": true,
            "strip_empty_ends": true
        }
    },
    "item": "www.facebook.com",
    "msg": "",
    "rc": 0,
    "start": "2023-07-08 15:44:18.458382",
    "stderr": "",
    "stderr_lines": [],
    "stdout": "star-mini.c10r.facebook.com.\n157.240.202.35",
    "stdout_lines": [
        "star-mini.c10r.facebook.com.",
        "157.240.202.35"
    ]
}

Wanted to capture FQDN (item) and stdout

www.facebook.com - star-mini.c10r.facebook.com.\n157.240.202.35
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
SJS
  • 27
  • 5
  • What is the expected result? – Vladimir Botka Jul 09 '23 at 02:25
  • I am looking to capture. Item & stdout date. Such as www.facebook.com - star-mini.c10r.facebook.com.\n157.240.202.35 – SJS Jul 09 '23 at 07:35
  • 1
    Right. This is what you've got from *debug*. You've already said it. Still the same, what is the expected result? In other words, what do you what to achieve? Do you want to create variables? For example, `my_fqdn=star-mini.c10r.facebook.com`? (There is no date in stdout. I don't understand how you want to 'capture' the stdout date.) – Vladimir Botka Jul 09 '23 at 07:46

1 Answers1

1

How to capture FQDN and IP address from output?

I understand that you like to access keys and values from Return Values, specifically shell module - Return Values.

To do so you may have a look into the following minimal example playbook.

---
- hosts: localhost
  become: false
  gather_facts: false

  vars:

    results:
      item: www.example.com
      start: "1970-01-01 00:00:00.000000"
      stdout_lines:
        - server.web.example.com
        - 192.0.2.1

  tasks:

  - debug:
      msg: "FQDN {{ results.stdout_lines[0] }} has IP {{results.stdout_lines[1] }}"

  - set_fact:
      date: "{{ results.start }}"
      fqdn: "{{ results.stdout_lines[0] }}"
      ip: "{{ results.stdout_lines[1] }}"

  - name: "{{ results.item }}"
    debug:
      msg:
       - "{{ date }}"
       - "{{ fqdn }}"
       - "{{ ip }}"

which will result into an output of

TASK [debug] **************************************
ok: [localhost] =>
  msg: FQDN server.web.example.com has IP 192.0.2.1

TASK [set_fact] ***********************************
ok: [localhost]

TASK [www.example.com] ****************************
ok: [localhost] =>
  msg:
  - '1970-01-01 00:00:00.000000'
  - server.web.example.com
  - 192.0.2.1

Further Reading

U880D
  • 8,601
  • 6
  • 24
  • 40