2016
November
Status

more
more
Status

Ansible : easy loops and unpacking files

Something that has always bugged me in ansible is a simple idea of loops … i.e – being able to loop through a directory structure for all of the files in a specific dir, and unzip them if they don’t exist at the destination…. thanks to some help on the #ansible IRC today I’ve figured out a quite elegant and simple solution for it…

 

---
# Check if Ansible will do loops properly
- name: Testing loops
  hosts: localhost
  connection: local

  tasks:

    - name: Check if directory eists
      stat: path="{{ some_path }}"
      register: tars_available
      ignore_errors: True

    - name: Find the list of tars that need extracting
      find: paths="{{ some_path }}" patterns="*.tgz" file_type=file
      register: tars_list
      when: tars_available.stat.exists

    - name: setting fact for the path names for the apps in the list
      set_fact:
        tars: "{{ tars_list.files| map(attribute='path') | list }}"
      when: tars_available.stat.exists

    - debug: var=tars

    - name: extract each of the apps in the tars_list to the correct apps dir
      unarchive: src={{ item }} dest={{ some_destination }}
      with_items: "{{ tars }}"
      when: tars is defined

 

more