Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mention wildcard in ansible commands

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?

like image 531
user3147180 Avatar asked Mar 18 '14 10:03

user3147180


People also ask

Can you use wildcards in ansible?

Wildcard doesn't work in ansible shell command.

How do I limit ansible hosts?

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.


2 Answers

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.

like image 70
larsks Avatar answered Sep 21 '22 11:09

larsks


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
like image 33
Sasha Avatar answered Sep 21 '22 11:09

Sasha