Issue
I am trying to upload multiple .xml
files to a server and my Ansible playbook indicates the path isn't a directory, but the path and files are there.
For each .xml
file I want to loop through and upload each one. The URI method doesn't work due to a known bug, so I reverted back to curl
. The command works from the CLI without any problems.
Relevant Ansible WARNING
message:
[WARNING]: Skipped '/tmp/oscap' path due to this access issue: '/tmp/oscap' is not a directory
I feel like I have refrigerator blindness and am missing something right in front of me. Help is greatly appreciated.
Added CLI output on paths and files for reference:
# ls -ld /tmp/oscap
drwxr-xr-x. 2 root root 42 Sep 17 22:28 /tmp/oscap
# ls -l
total 16120 -rw-r--r--. 1 admin root 16504110 Sep 19 09:07 ans-01.corp.com.xml
---
- name: Upload .xml files to server
hosts: localhost
become: true
vars:
heimdall_hostname: 'http://heimdall.corp.com'
heimdall_port: 42089
evaluation_tags: scap
api_key: >-
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJrZXlJZCI6IjMiLCJjcmVhdGVkQXQiOiIyMDIzLTA5LTIxVDIyOjQ2OjIyLjgyN1oiLCJpYXQiOjE2OTUzMzYzODJ9.RwmNVrpJ34Qm0JjiTU_JVYLDToldoAAajN4S5h-Afhg
tasks:
- name: Get a list of all the .xml files in the file path
ansible.builtin.find:
paths: /tmp/oscap
file_type: file
use_regex: yes
patterns: '*.xml'
register: xml_files
- name: Upload each .xml file to the server
ansible.builtin.shell:
cmd: curl -k -F "data=@{{ item.path }}" \
-F "filename={{ item.path | basename }}" \
-F "public=false" -F "evaluationTags={{ evaluation_tags }}" \
-H "Authorization:Api-Key {{ api_key }}" \
"{{ heimdall_hostname }}:{{ heimdall_port }}/evaluations"
loop: "{{ xml_files.files }}"
Everything is working except I am getting errors on hostname resolution. Here's the error output from the Ansible Controller.
failed: [ans-01.corp.com] (item={'path': '/tmp/oscap/ans-01.corp.com.xml', 'mode': '0644', 'isdir': False, 'ischr': False, 'isblk': False, 'isreg': True, 'isfifo': False, 'islnk': False, 'issock': False, 'uid': 1000, 'gid': 0, 'size': 16504110, 'inode': 2436, 'dev': 64774, 'nlink': 1, 'atime': 1695308203.6200945, 'mtime': 1695128852.3120863, 'ctime': 1695425843.6465063, 'gr_name': 'root', 'pw_name': 'admin', 'wusr': True, 'rusr': True, 'xusr': False, 'wgrp': False, 'rgrp': True, 'xgrp': False, 'woth': False, 'roth': True, 'xoth': False, 'isuid': False, 'isgid': False}) => {
"ansible_loop_var": "item",
"changed": true,
"cmd": "curl -k -F \"data=@/tmp/oscap/ans-01.corp.com.xml\" \\ -F \"filename=ans-01.corp.com.xml\" \\ -F \"public=false\" -F \"evaluationTags=scap\" \\ -H \"Authorization:Api-Key eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJrZXlJZCI6IjMiLCJjcmVhdGVkQXQiOiIyMDIzLTA5LTIxVDIyOjQ2OjIyLjgyN1oiLCJpYXQiOjE2OTUzMzYzODJ9.RwmNVrpJ34Qm0JjiTU_JVYLDToldoAAajN4S5h-Afhg\" \\ \"http://heimdall.corp.com:42089/evaluations\"",
"delta": "0:00:00.011430",
"end": "2023-09-23 16:39:28.283482",
"invocation": {
"module_args": {
"_raw_params": "curl -k -F \"data=@/tmp/oscap/ans-01.corp.com.xml\" \\ -F \"filename=ans-01.corp.com.xml\" \\ -F \"public=false\" -F \"evaluationTags=scap\" \\ -H \"Authorization:Api-Key eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJrZXlJZCI6IjMiLCJjcmVhdGVkQXQiOiIyMDIzLTA5LTIxVDIyOjQ2OjIyLjgyN1oiLCJpYXQiOjE2OTUzMzYzODJ9.RwmNVrpJ34Qm0JjiTU_JVYLDToldoAAajN4S5h-Afhg\" \\ \"http://heimdall.corp.com:42089/evaluations\"",
"_uses_shell": true,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true
}
},
"item": {
"atime": 1695308203.6200945,
"ctime": 1695425843.6465063,
"dev": 64774,
"gid": 0,
"gr_name": "root",
"inode": 2436,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"mode": "0644",
"mtime": 1695128852.3120863,
"nlink": 1,
"path": "/tmp/oscap/ans-01.corp.com.xml",
"pw_name": "admin",
"rgrp": true,
"roth": true,
"rusr": true,
"size": 16504110,
"uid": 1000,
"wgrp": false,
"woth": false,
"wusr": true,
"xgrp": false,
"xoth": false,
"xusr": false
},
"msg": "non-zero return code",
"rc": 3,
"start": "2023-09-23 16:39:28.272052",
"stderr": "curl: (3) URL using bad/illegal format or missing URL\n % Total % Received % Xferd Average Speed Time Time Time Current\n Dload Upload Total Spent Left Speed\n\r 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0curl: (6) Could not resolve host: filename=ans-01.corp.com.xml\ncurl: (3) URL using bad/illegal format or missing URL\n % Total % Received % Xferd Average Speed Time Time Time Current\n Dload Upload Total Spent Left Speed\n\r 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0curl: (6) Could not resolve host: public=false\ncurl: (3) URL using bad/illegal format or missing URL\ncurl: (3) URL using bad/illegal format or missing URL\ncurl: (3) URL using bad/illegal format or missing URL",
"stderr_lines": [
"curl: (3) URL using bad/illegal format or missing URL",
" % Total % Received % Xferd Average Speed Time Time Time Current",
" Dload Upload Total Spent Left Speed",
"",
" 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0curl: (6) Could not resolve host: filename=ans-01.corp.com.xml",
"curl: (3) URL using bad/illegal format or missing URL",
" % Total % Received % Xferd Average Speed Time Time Time Current",
" Dload Upload Total Spent Left Speed",
"",
" 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0curl: (6) Could not resolve host: public=false",
"curl: (3) URL using bad/illegal format or missing URL",
"curl: (3) URL using bad/illegal format or missing URL",
"curl: (3) URL using bad/illegal format or missing URL"
],
"stdout": "",
"stdout_lines": []
}
PLAY RECAP *********************************************************************
ans-01.corp.com : ok=2 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
Solution
I am able to reproduce a [WARNING]
message and Skipped
only if using Parameter: use_regex
set to true
.
If
false
, the patterns are file globs (shell).
Iftrue
, they are python regexes.
false
<- default
A minimal example playbook
---
- hosts: localhost
become: false
gather_facts: false
tasks:
- find:
paths: "/home/{{ ansible_user }}/test"
patterns: '*.xml' # or '*.yml'
register: result
- debug:
msg: "{{ result.files | map(attribute='path') | list | map('basename') }}"
or even
---
- hosts: localhost
become: false
gather_facts: false
tasks:
- find:
paths: "/home/{{ ansible_user }}/test"
file_type: file
patterns: '*.xml' # or '*.yml'
register: result
- debug:
msg: "{{ result.files | map(attribute='path') | list | map('basename') }}"
will result into just the filenames looking for.
If a (Python) Regular Expression is needed use in example
- find:
paths: "/home/{{ ansible_user }}/test"
use_regex: true
patterns: '^[a-z].*xml$' # or '^[a-z].*yml$'
register: result
or as already mentioned within the comments '.*\.xml'
.
Further Documentation and Q&A
- Python - Understanding Regular Expression
re.match
is only useful if the match starts from the beginning of the string, it does not find all matches in a string. - and probably many more ...
Answered By - U880D Answer Checked By - Pedro (WPSolving Volunteer)