Home > Net >  Filter out content from Ansible output
Filter out content from Ansible output

Time:05-19

I want to filter out ip address alone from the ansible output. When I tried to filter out with the awk command, it failed. Please see my code, output, and required output.

    - name: Gather all VMs from a specific folder
      community.vmware.vmware_vm_info:
        hostname: hostname_local
        username: vmwarelogin
        password: passwordvmware
        folder: "/VMFS/"
        validate_certs: False
      delegate_to: localhost
      register: vm_info
    - debug:
        var: ip
      vars:
        ip: "{{ vm_info.virtual_machines|
                selectattr('guest_name', 'eq', 'My-Machine')|
                map(attribute='ip_address')|first }}"
      register: ip
    - name: add ip
      shell: echo "{{ip}}"| awk '{print $2}'

Output after running the above code

{'ip': '192.168.1.32', 'failed': False, 'changed': False}

Expected output is

192.168.1.32

Any help would be appreciated to use this IP address as a variable for other places in the same playbook

CodePudding user response:

If I understand what you want to convey using YAML tag you are implying

{'ip': '192.168.1.32', 'failed': False, 'changed': False}

should be treated as YAML file. If this is case you should if allowed use YAML parser, if you want parser which can be used as part of pipelined command then I suggest trying yq. If you are forced to use AWK for this then be warned that it is best suited for working with entities which belong to Chomsky Type-3 while YAML is not Chomsky Type-3 contraption. Anyway it might suffice for your case. I would propose following heurestic: grab 1st thing which looks like IP address in decimal notation, this could be done following way in GNU AWK let say you are using standard input to deliver

{'ip': '192.168.1.32', 'failed': False, 'changed': False}

then

awk 'BEGIN{FPAT="[0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}"}{print $1}'

output

192.168.1.32

Explanation: I use field pattern (FPAT) to inform GNU AWK that field is 1 to 3 digits followed by literal dot followed by 1 to 3 digits followed by dot followed by 1 to 3 digits followed by dot followed by 1 to 3 digits. Note that this does consider solely IP addresses in decimal notation and could also give false positives (e.g. it will find 999.999.999.999) but hopefully your input is not such malicious. I print 1st such field found for each line.

(tested in gawk 4.2.1)

CodePudding user response:

Regarding how to

... use this IP address as a variable for other places in the same playbook?

Reviewing the provided example it looks that you can simplify your case as follow

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

  vars:
    IP: "192.168.2.1"

  tasks:

  - name: Show vars
    debug:
      var: IP
    register: result

  - name: Show result
    debug:
      var: result

  - name: Show IP
    debug:
      msg: "{{ result.IP }}"

just by using finally echo {{ ip.ip }} in your case.

I want to filter out IP address alone from the Ansible output.

There is no need for awk at all as the content is already there direct accessible.

Expected output is ...

The result would be as required

TASK [Show vars] ***
ok: [localhost] =>
  IP: 192.168.2.1

TASK [Show result] ***
ok: [localhost] =>
  result:
    IP: 192.168.2.1
    changed: false
    failed: false

TASK [Show IP] ***
ok: [localhost] =>
  msg: 192.168.2.1

Further Documentation

  • Related