Getting string from an XML URL response

Hi,

I have a URL that I call with a GET, and that returns an XML response.

I’d like to parse that response (I could use grep), but I’m struggling to get the response content.

`

  • name: Get OHP Group Passcode
    uri:
    url: “{{ node_protocol }}://{{ inventory_hostname }}:/admin/configuration/environment/current”
    headers:
    “accept”: “application/Configuration.6_10+xml”
    “X-Requested-With”: “ansible”
    method: GET
    user: “{{ auth_user }}”
    password: “{{ local_password }}”
    force_basic_auth: yes
    validate_certs: no
    return_content: yes
    register: webpage
    until: webpage.content.find(“passcode”) != -1
    retries: 60
    delay: 10
    delegate_to: localhost
    when: inventory_hostname == groups[“backend_nodes”][0]

`

`

  • name: Output current env details
    debug:
    msg: “{{ webpage.content }}”

  • name: Get the group passcode
    command: “echo {{ webpage.content }} | grep -oP ‘(?<=).*?(?=)’”
    delegate_to: localhost
    register: passcode
    `

I assumed I could just debug out the webpage.content again, but I get an error

TASK [ansible-ohp-win-installer : Output current env details] **********************************************************
fatal: [192.168.50.4]: FAILED! => {“msg”: “The task includes an option with an undefined variable. The error was: ‘dict object’ has no attribute ‘content’\n\nThe error appears to have been in ‘/mnt/c/Work/ansible/roles/ansible-ohp-win-installer/tasks/main.yml’: line 185, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Output current env details\n ^ here\n\nexception type: <class ‘ansible.errors.AnsibleUndefinedVariable’>\nexception: ‘dict object’ has no attribute ‘content’”}

I have a URL that I call with a GET, and that returns an XML response.

I'd like to parse that response (I could use grep), but I'm struggling to
get the response content.

In 2.4 you have the xml module you could use.

- name: Get OHP Group Passcode
      uri:
        url: "{{ node_protocol }}://{{ inventory_hostname
}}:/admin/configuration/environment/current"
        headers:
           "accept": "application/Configuration.6_10+xml"
           "X-Requested-With": "ansible"
        method: GET
        user: "{{ auth_user }}"
        password: "{{ local_password }}"
        force_basic_auth: yes
        validate_certs: no
        return_content: yes
      register: webpage
      until: webpage.content.find("passcode") != -1
      retries: 60
      delay: 10
      delegate_to: localhost
      when: inventory_hostname == groups["backend_nodes"][0]

    - name: Output current env details
      debug:
        msg: "{{ webpage.content }}"

<snip />

I assumed I could just debug out the webpage.content again, but I get an
error

TASK [ansible-ohp-win-installer : Output current env details]
**********************************************************
fatal: [192.168.50.4]: FAILED! => {"msg": "The task includes an option with
an undefined variable. The error was: 'dict object' has no attribute
'content'\n\nThe error appears to have been in
'/mnt/c/Work/ansible/roles/ansible-ohp-win-installer/tasks/main.yml': line
185, column 7, but may\nbe elsewhere in the file depending on the exact
syntax problem.\n\nThe offending line appears to be:\n\n\n - name:
Output current env details\n ^ here\n\nexception type: <class
'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'dict object' has no
attribute 'content'"}

Your host 192.168.50.4 is not groups["backend_nodes"][0]

"Get OHP Group Passcode" run only on one host 'groups["backend_nodes"][0]' because of the when statement.
But your debug task run on all the host in the playbook, but they don't have the variable webpage, so you get the error message on them, so you need to include the when on the debug task also.

Thanks Kai, that worked. Seems obvious now I look at it, but wasnt at the time!