skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    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 }}"
    

  2. 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') }}"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search