Getting string from an XML URL response

707 views
Skip to first unread message

Marc L

unread,
Oct 27, 2017, 12:55:32 PM10/27/17
to Ansible Project
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 '(?<=<passcode>).*?(?=</passcode>)'"
      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'"}




Kai Stian Olstad

unread,
Oct 27, 2017, 2:52:02 PM10/27/17
to ansible...@googlegroups.com
On 27.10.2017 18:55, Marc L wrote:
> 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.

--
Kai Stian Olstad

Marc Lang

unread,
Oct 30, 2017, 11:04:15 AM10/30/17
to ansible...@googlegroups.com
Thanks Kai, that worked. Seems obvious now I look at it, but wasnt at the time!



--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/BGsBZEnT0-c/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/dbfca1ec28fd254e9e15e0b64546ce06%40olstad.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages