Windows Escape Character Issue

47 views
Skip to first unread message

Joefrey

unread,
May 22, 2023, 9:49:30 PM5/22/23
to Ansible Project
Good day,

I'm utilizing slurp to pull an XML file from a remote Windows host to lookup some values. In the file there are a couple of Windows paths that when grabbed via regex_search are returned with various escape sequences. For example, \a becomes \u0007.

Any ideas how I can resolve the below? I cannot install anything on the remote host, and I cannot pull files locally. This rules out using any of the XML modules.

Example for clarity:

file.xml:

<?xml version="1.0" encoding="utf-8"?>
<ClusterParameters xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   ...
  <SharedPath>\\somewhere.com\adata\aclusters\bytes</SharedPath>
  ...
</ClusterParameters>


Task:
    - name: Create dictionary of values from test.xml
      ansible.builtin.set_fact:
        file_xml_data: '{{ file_xml_data | default({}) | combine({item.key: item.value}) }}'
       loop:
        ...
        - "{ 'key': 'cifs_share', 'value':
          '{{ file_xml | regex_search('<SharedPath>(.+)</', '\\1') | first }}' }"
         ...

Appreciate any ideas you may have.

Thank you.
 

Dick Visser

unread,
May 26, 2023, 1:19:16 PM5/26/23
to ansible...@googlegroups.com
Hii

On Tue, 23 May 2023 at 03:49, Joefrey <joefrey...@gmail.com> wrote:
>
> Good day,
>
> I'm utilizing slurp to pull an XML file from a remote Windows host to lookup some values. In the file there are a couple of Windows paths that when grabbed via regex_search are returned with various escape sequences. For example, \a becomes \u0007.
>
> Any ideas how I can resolve the below? I cannot install anything on the remote host, and I cannot pull files locally. This rules out using any of the XML modules.

What does "cannot pull files locally" mean?
You can't use the XML modules on the control node?

Joefrey Kimmel

unread,
May 26, 2023, 1:40:55 PM5/26/23
to ansible...@googlegroups.com
Correct. The control node is an Ansible Tower server that we have limited access to.

> On May 26, 2023, at 12:19 PM, Dick Visser <dnmv...@gmail.com> wrote:
>
> Hii
> --
> 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/zmEwNI2eJ3I/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAF8BbLYCDLiK5WVAXasoqL5t0LWCKrNGS3uNVzdve%2BCowtkujg%40mail.gmail.com.

Dick Visser

unread,
May 26, 2023, 2:04:03 PM5/26/23
to ansible...@googlegroups.com
On Fri, 26 May 2023 at 19:40, Joefrey Kimmel <joefrey...@gmail.com> wrote:
>
> Correct. The control node is an Ansible Tower server that we have limited access to.

That indeed does not leave much.
I'm not sure but the problem may be that you're using the quoted json
style to define your list of key/values.
If I lay that out as basic yaml (same data structure, but a bit
cleaner to look at), it does seem to work for me:

---
- name: xml slurp and filter
hosts: localhost
connection: local
gather_facts: no
tasks:
- name: Create dictionary of values from test.xml
ansible.builtin.set_fact:
file_xml_data: '{{ file_xml_data | default({}) |
combine({item.key: item.value}) }}'
loop:
- key: cifs_share
value: "{{ file_xml | regex_search('<SharedPath>(.+)</',
'\\1') | first }}"
- debug:
var: file_xml_data

vars:
file_xml: |
<?xml version="1.0" encoding="utf-8"?>
<ClusterParameters xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SharedPath>\\somewhere.com\adata\aclusters\bytes</SharedPath>
</ClusterParameters>


gives:

dick.visser@GA0267 ~$ ansible-playbook work/tasks/xml4.yml
[WARNING]: No inventory was parsed, only implicit localhost is available

PLAY [filter] ******************************************************************

TASK [Create dictionary of values from test.xml] *******************************
ok: [localhost] => (item={'key': 'cifs_share', 'value':
'\\\\somewhere.com\\adata\\aclusters\\bytes'})

TASK [debug] *******************************************************************
ok: [localhost] =>
file_xml_data:
cifs_share: \\somewhere.com\adata\aclusters\bytes

PLAY RECAP *********************************************************************
localhost : ok=2 changed=0 unreachable=0
failed=0 skipped=0 rescued=0 ignored=0

Still, DIY string parsing of XML is never bullet proof. What happens
if things span multiple lines, etc etc.
In short, YMMV.

Joefrey Kimmel

unread,
May 26, 2023, 2:08:52 PM5/26/23
to ansible...@googlegroups.com
Thank you. I’ll give this a whirl next week.

> On May 26, 2023, at 1:04 PM, Dick Visser <dnmv...@gmail.com> wrote:
> --
> 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/zmEwNI2eJ3I/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAF8BbLY3xX9CFPJYBRk_-Bj6fFj0H6hnQU07Q7L8h1AfhuPT5g%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages