My server has the below path where the hostname i.e "myhost1" folder can either be in upper case or lower case as shown in example below.
/app/cell/myhost1_sales075/nodes/Node009/servers/server.xml
or the path could be in upper case hostname like below
/app/cell/MYHOST1_sales075/nodes/Node009/servers/server.xml
Below is my playbook which checks if the path exists else it will fail.
Note: ansible_hostname may either return myhost1 (lowercase) or MYHOST1 (uppercase)
- name: check if Path is valid
shell: "ls -l /app/cell/{{ ansible_hostname }}*/nodes/*Node*/servers/server.xml"
ignore_errors: yes
register: shorewall
- fail:
msg: Path or server name is invalid. Kindly enter correct PATH. Exiting Now...
when: shorewall.rc != 0
The problem I face is that my check is case sensitive when I want it to be case-insensitive. Thus my playbook should not fail if hostname is either upper or lowercase along with the wildcard "*"
Can you please let me know what changes I need to do in my playbook for the Path check to be case-insensitive ?
Note: this is only answering your direct question. Meanwhile you should consider reviewing your current script and use the stat
module to check for file presence instead of ignoring errors on ls
launched in a shell
The solution below will check both path, then extract all the rc
fields in your results
registered var with json_query
and check if at least one of them returned 0
Prereq: pip install jmespath
on the controller machine (required by json_query
filter).
---
- name: Verify upper/lower path with ls in shell (bad)
hosts: localhost
gather_facts: false
tasks:
- name: Check my path
shell: "ls -l /tmp/{{ item }}"
register: my_path
ignore_errors: yes
loop:
- "{{ inventory_hostname }}"
- "{{ inventory_hostname | upper }}"
- name: Fail if path does not exists
fail:
msg: path does not exists
when: 0 not in (my_path | json_query('results[].rc[]'))
Update
Using json_query
might have been an overkill here. You can get the same result with core filters:
when: 0 not in (my_path.results | map(attribute="rc'))