0

I am having around 250 Debian files in a directory /home/emgda/del/ which periodically changes and must be installed by everyday end.

So I am trying to write an Ansible script to loop this directory, hold file names in an array then install all Debian sequentially using command sudo dpkg -i file_name

So far below is the code I have listed out the files in the directory, just need to add command: somehow to execute above command,

---
- hosts: local
  gather_facts: false

  tasks:

  - command: "ls /home/emgda/del/"
    register: dir_out

  - debug: var={{item}}
    with_items: dir_out.stdout_lines

OUTPUT is

PLAY [local] ***********************************************************************************************************

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

TASK [debug] ************************************************************************************************************************
ok: [localhost] => (item=dir_out.stdout_lines) => {
    "dir_out.stdout_lines": [
        "a.deb"
    ],
    "item": "dir_out.stdout_lines"
}

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

Any help will be deeply appreciated.

Ankur Soni
  • 5,725
  • 5
  • 50
  • 81

2 Answers2

0

Q: "I have Debian files in a directory /home/emgda/del/ which periodically changes and must be installed."

A: find the packages and install them in the loop with apt

    - find:
        path: '/home/emgda/del/'
        patterns: '*.deb'
      register: result

    - apt:
        deb: '{{ item.path }}'
      loop: '{{ result.files }}'

It's possible to query plugin fileglob and install the packages in one task

    - apt:
        deb: "{{ item }}"
      loop: "{{ query('fileglob', '/home/emgda/del/*.deb') }}"
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
  • It gives an error `dpkg: error: requested operation requires superuser privilege`. I tried `become` but maybe something is wrong in syntax. – Ankur Soni Mar 05 '20 at 13:05
  • Yes. `sudo dpkg` is not enough privilege to run the `apt` module. See [Privilege escalation must be general](https://docs.ansible.com/ansible/latest/user_guide/become.html#privilege-escalation-must-be-general). Without general escalation privilege proceed with `command: 'sudo dpkg -i {{ item }}'`. – Vladimir Botka Mar 05 '20 at 13:14
0

Well I solved it using below technique.

---
- hosts: local
  gather_facts: false

  tasks:

  - name: Making a list of files
    shell: "ls /home/emgda/packages/"
    register: command_result

  - name: Installing Debian sequentially.
    become: yes
    shell: "dpkg -i /home/emgda/packages/{{item}}"
    with_items: 
     - "{{ command_result.stdout_lines }}"
Ankur Soni
  • 5,725
  • 5
  • 50
  • 81