Skip to content

Instantly share code, notes, and snippets.

@grodrigo
Created August 20, 2019 04:09
Show Gist options
  • Save grodrigo/10d66c73c4755279d77e242754443889 to your computer and use it in GitHub Desktop.
Save grodrigo/10d66c73c4755279d77e242754443889 to your computer and use it in GitHub Desktop.
ansible playbook examples
---
- hosts: localhost
tasks:
- name: Create tmp folder path
file:
state: directory
path: /tmp/myfiles
- name: Ansible create multiple files example
file:
path: "/tmp/myfiles/{{ item }}"
state: touch
mode: 0644
#added in 2.7
# modification_time: "201812271410.15"
with_items:
- int1.txt
- int2.txt
- int3.txt
- int4.txt
- .hidden.txt
- copy:
src: /tmp/myfiles/int1.txt
dest: /tmp/myfiles/new.txt
- name: create an old file
shell: "touch -d '2 days ago' /tmp/myfiles/old1_$(date --date='2 days ago' +%F_%T).txt"
- name: create a second old file with creates statement
command: touch -d "3 days ago" /tmp/myfiles/old2.txt creates=/tmp/myfiles/old2.txt
- name: synchronize files
synchronize:
src: /tmp/myfiles/
dest: /tmp/myfiles2/
- name: Recursively find /tmp/myfiles files older than 2 days
find:
paths: /tmp/myfiles
age: 2d
recurse: yes
register: old_files
- copy:
src: "{{ item.path }}"
dest: /tmp/myfiles/copied_{{ item.path | basename }}
with_items: "{{ old_files.files }}"
- name: Find file wildcard
find:
paths: /tmp/myfiles
patterns: "^int.*.txt"
use_regex: true
register: wildcard_files_to_delete
- name: Remove file wildcard
file:
path: "{{ item.path }}"
state: absent
with_items: "{{ wildcard_files_to_delete.files }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment