McFly
McFly

Reputation: 25

How to untar multi-part tar.gz files via Ansible playbook?

I have following two files in my home directory in Red Hat Linux 7.9:

mydir.tar.gz.aa
mydir.tar.gz.ab

I am running the following command to extract the contents from these files :

cat mydir.tar.gz.* | tar xzf -

the command does the job and extracts the contents.

The multipart tar was created via the following command:

tar czf - * | split --bytes=5MB - mydir.tar.gz.

I need to untar the files via the Ansible playbook.yml. I tried the following pieces of code:

---
- name: untar the multipart tar file
  hosts: localhost
  tasks:
  - name: untar
    ansible.builtin.unarchive:
      src: /home/McFly/mydir.tar.gz.a*
      dest: /home/McFly/
    become: yes
    become_user: root
...
---
- name: untar the multipart tar file
  hosts: localhost
  tasks:
  - name: untar
    ansible.builtin.unarchive:
      src: /home/McFly/mydir.tar.gz.aa
      dest: /home/McFly/
    become: yes
    become_user: root
...
---
- name: untar the multipart tar file
  hosts: localhost
  tasks:
  - name: untar
    ansible.builtin.shell: cat mydir.tar.gz.* | tar xzf -
    args:
      chdir: /home/McFly/
      executable: /bin/bash
    become: yes
    become_user: root
...

None of the above worked.

How can I untar the multipart tar.gz files via the Ansible playbook?

Upvotes: 0

Views: 2064

Answers (1)

McFly
McFly

Reputation: 25

So I posted three pieces of code above and the following piece of code worked for me:

---
- name: untar the multipart tar file
  hosts: localhost
  tasks:
  - name: untar
    ansible.builtin.shell: "cat mydir.tar.gz.* | tar xzf -"
    args:
      chdir: /home/McFly/
      executable: /bin/bash
    become: yes
    become_user: root
...

The code is almost the same except for the part:

"cat mydir.tar.gz.* | tar xzf -"

Previously there were no double quotes, wrapping the whole command in double quotes did the job.

What happening was: the dash (-) at the end of the shell command was not following the yml syntax rules. Escaping that specific dash by wrapping the whole command inside the double quotes made it work fine.

Upvotes: 0

Related Questions