Accurate Solution for Ansible Archive Module

Hi, I have finished Anisble Archive module question and completed it successfully but I feel my solution is not optimized one, as its tasks are repeated. Can some one suggest correct solution.

Here is my solution. I had to repeat it for every host. I think we can use “hosts: all” and dynamically pass the user names as variables. Can some one suggest optimized solution for this.


  • hosts: stapp01
    become: true
    tasks:
    • name: Compress directory .tgz
      archive:
      path: /usr/src/devops/
      dest: /opt/dba/beta.tar.gz
      format: gz
      owner: tony
      group: tony

use ansible variables like this:
group: ‘{{ ansible_user }}’
owner: ‘{{ ansible_user }}’

@nashwan Thanks for your solution.

This is my ansible code

  • hosts: all

    become: yes

    tasks:

    • name: Compress Directory contents

      become: yes

      archive:

      path: /usr/src/dba/

      dest: /opt/dba/ecommerce.tar.gz

      format: gz

      group: ‘{{ ansible_user }}’

      owner: ‘{{ ansible_user }}’

I’m getting below error