Ansible 2.7 : How to list files from unarchive - ansible

Need to unarchive a .zip file then list the unpacked files. This is to select some pattern of files and use them in rest of the code
My playbook yaml snippet is
- name: uarchive the opar zip
unarchive:
src: "{{opar_download_path}}/opar.zip"
dest: "{{opar_download_path}}"
remote_src: yes
list_files: yes
I am not able to find details of how to use the result from "list_files",ie how to store the list of files to a variable. I was referring below document
https://docs.ansible.com/ansible/latest/modules/unarchive_module.html

The list_files: yes includes additional response attribute called files. For better understanding, try to print the output unarchived_list.files, as shown below:
- name: unarchive the opar zip
unarchive:
src: "{{opar_download_path}}/opar.zip"
dest: "{{opar_download_path}}"
remote_src: yes
list_files: yes
register: unarchived_list
- name: print unarchived folder list of files
debug: msg="{{unarchived_list.files}}"
More information on registering variables can be found here: https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#registering-variables

Related

Need assistance in ansible playbook for compressing files and excluding files based on certain condition

I have created an ansible playbook which finds files older than 7 days ,trying to exclude
files from particular directory or already zipped but not able to exclude.
tasks:
- name : Find files ending with extensions
become: true
find:
path:
- /home/test/replicate/vol/gunnsc01/prod/scoreout
recurse: yes
file_type: any
age: 7d
age_stamp: mtime
excludes: "delete"
register: output
- name: archive the files
become: yes
archive:
remove: yes
path: "{{ item.path }}"
dest: "{{ item.path }}-{{ansible_date_time.date.replace('-','')}}.gz"
format: zip
with_items: "{{ output.files }}"
I am not able to exclude specific directory ,I have tried exclude,excludes,exclude_path,exclude_paths and exclude_patterns
however I am not able to exclude the files under the directory delete

Ansible copy files with wildcard?

In my files directory I have various files, with a similar name structure:
data-example.zip
data-precise.zip
data-arbitrary.zip
data-collected.zip
I would like to transfer all of these files in the /tmp directory of my remote machine using Ansible without specifying each file name explicitly.
In other words I would like to transfer every file that stars with "data-".
What is the correct way to do that? In a similar thread, someone suggested the with_fileglob keyword, - but I couldn't get that to work.
Can someone provide me an example on how to accomplish said task?
Method 1: Find all files, store them in a variable and copy them to destination.
- hosts: lnx
tasks:
- find: paths="/source/path" recurse=yes patterns="data*"
register: file_to_copy
- copy: src={{ item.path }} dest=/dear/dir
owner: root
mode: 0775
with_items: "{{ files_to_copy.files }}"
Use remote_src: yes to copy file in remote machine from one path to another.
Ansible documentation
Method 2: Fileglob
- name: Copy each file over that matches the given pattern
copy:
src: "{{ item }}"
dest: "/etc/fooapp/"
owner: "root"
mode: 0600
with_fileglob:
- "/playbooks/files/fooapp/*"
Ansible documentation
Shortly after posting the question I actually figured it out myself. The with_fileglob keyword is the way to do it.
- name: "Transferring all data files"
copy:
src: "{{ item }}"
dest: /tmp/
with_fileglob: "data-*"

Copy extracted files to another path - Ansible

I am using unarchive module in ansible to extract tar.gz files to a folder. After that, i want to copy the extracted folder to another location. Is there a ansible way of doing it? I tried below , it does not work
- name : Extract Files
unarchive:
src: "/home/app/{{ item }}"
dest: "/home/app/"
mode: 0777
copy: no
with_items:
- "{{ sharef }}"
delegate_to: localhost
register: extractedfiles
- name : Copy Files
copy:
src: "{{extractedfiles.results}}"
dest: "{{ location }}"
delegate_to: localhost
How do i extract the filie names from the previously executed task extractedfiles.results does not work. Any help would be appreciated.

How to copy predetermined multiple files in with_items in single Ansible playbook to diff target destinations?

Plz bear with me I am new to Ansible. I have a tasks/main.yml file like this as part of an effort to enhance. For now, I have to execute a playbook for each file separately to copy A.jar and B.jar one at a time. However I am trying to create an array to contain A and B jar files in advance and process one by one to copy it to two different destination folders in this playbook but struggling with syntax. Hoping to re-use with_items.
- name: Copy
copy:
src: "/somePath/{{ name }}.jar"
dest: "{{ item }}"
remote_src: yes
with_items:
- "/pathTo/foo/"
- "/pathTo/bar/"
# /pathTo/foo
A.jar
B.jar
# /pathTo/bar
A.jar
B.jar
You can use with_nested for looping,
- name: Copy
copy:
src: "/somePath/{{ item[0] }}"
dest: "{{ item[1] }}"
remote_src: yes
with_nested:
- [ "A.jar", "B.jar" ]
- [ "/pathTo/foo/", "/pathTo/bar/" ]
This iterates over each element in each list to copy all your source files (A.jar and B.jar) from the first list to all the destination directories listed in the second list.

Ansible: extract archive to user's dir

I'm trying to build an ansible playbook which extracts an archive (available through http) under a folder to be created in the user's home directory.
So far I've come up with:
- name: Extract archive
unarchive:
creates: wildfly
src: "http://download.jboss.org/wildfly/10.1.0.Final/wildfly-10.1.0.Final.zip"
dest: "{{ ansible_user_dir }}"/wildfly
remote_src: yes
However by running it, I get a syntax error: here is the offending line:
dest: "{{ ansible_user_dir }}"/wildfly
^ here
I've tried also without quotes, but the issue stays the same.
By hardcoding the user's home dir it works- except that no folder "wildfly" is created. Any help ?
Quote the whole expression:
dest: "{{ ansible_user_dir }}/wildfly"

Resources