TieDad
TieDad

Reputation: 9889

Ansible: how to get unarchived path?

I'm writing an Ansible script to setup a zookeeper cluster. After extract zookeeper tar ball:

unarchive: src={{tmp_dir}}/zookeeper.tar.gz dest=/opt/zookeeper copy=no

I get a zookeeper directory that contains version number:

[root@sc-rdops-vm09-dhcp-2-154 conf]# ls /opt/zookeeper
zookeeper-3.4.8

To proceed, I have to know name of the extracted sub-directory. For example, I need to copy a configure to /opt/zookeeper/zookeeper-3.4.8/conf.

How to get zookeeper-3.4.8 with Ansible statement?

Upvotes: 1

Views: 481

Answers (2)

Raul Hugo
Raul Hugo

Reputation: 1136

Try with this:

➜  ~ cat become.yml
---
- hosts: localhost
  user: vagrant
  tasks:
      - shell: ls /opt/zookeeper
        register: path

      - debug: var=path.stdout


➜  ~ ansible-playbook -i hosts-slaves  become.yml
 [WARNING]: provided hosts list is empty, only localhost is available


PLAY ***************************************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [command] *****************************************************************
changed: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] => {
    "path.stdout": "zookeeper-3.4.8"
}

PLAY RECAP *********************************************************************
localhost                  : ok=3    changed=1    unreachable=0    failed=0

Then you could use {{ path.stdout }} to set the name of the path, in the next tasks.

Upvotes: 1

Daniel Stefaniuk
Daniel Stefaniuk

Reputation: 5764

Alternatively, before you proceed with the next steps you could rename this directory

  - name: Rename directory
    shell: mv `ls -d -1 zookeeper*` zookeeper

The above shell command substitution (inside backticks) should work.

Upvotes: 1

Related Questions