I am executing shell commands via Ansible.
Sometimes i don't have the complete foldername. Suppose i have dirname solr4.7.0
.
In shell I can type cd solr*
.
But in ansible I can't do:
chdir=/var/solr*
Is there any workaround?
Wildcard doesn't work in ansible shell command.
Using the --limit parameter of the ansible-playbook command is the easiest option to limit the execution of the code to only one host. The advantage is that you don't need to edit the Ansible Playbook code before executing to only one host.
No. The chdir=
parameter to, e.g., the command
module does not support wildcards.
You could accomplish what you want using a register variable to store the output of the ls
command:
- shell: ls -d solr*
register: dir_name
- command: some_command
args:
chdir: "{{ dir_name.stdout }}"
But this is, frankly, an ugly solution. You're better off just using the actual directory name. If it differs on different hosts, you can use host variables to set it appropriately.
As Larsks wrote the key is to use register
, but the code was not working on my current ansible version. So here is corrected one:
- shell: ls -d solr*
register: dir_name
- command: chdir={{ item }} some_command
with_items: dir_name.stdout_lines
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With