Skip to content

Instantly share code, notes, and snippets.

@zeitounator
Last active March 26, 2023 12:11
Show Gist options
  • Save zeitounator/362fcc8c367a2c544ae3150933177909 to your computer and use it in GitHub Desktop.
Save zeitounator/362fcc8c367a2c544ae3150933177909 to your computer and use it in GitHub Desktop.
$ tree
.
├── demo.yml
├── inventories
│   └── demo
│   └── hosts.yml
└── roles
└── copy_files
├── defaults
│   └── main.yml
├── files
│   ├── file10.txt
│   ├── file1.txt
│   ├── file2.txt
│   ├── file3.txt
│   ├── file4.txt
│   ├── file5.txt
│   ├── file6.txt
│   ├── file7.txt
│   ├── file8.txt
│   └── file9.txt
├── tasks
│   └── main.yml
└── vars
└── paths.yml
8 directories, 15 files
$ cat inventories/demo/hosts.yml
all:
vars:
ansible_connection: docker
hosts:
test-target.local:
$ cat roles/copy_files/vars/paths.yml
---
copy_files_paths:
- file1.txt
- file5.txt
- file8.txt
- file10.txt
$ cat roles/copy_files/defaults/main.yml
---
copy_files_destination_path: "/tmp/toto"
$ cat roles/copy_files/tasks/main.yml
---
- name: Make sure destination path exists
ansible.builtin.file:
path: "{{ copy_files_destination_path }}"
state: directory
mode: 0750
- name: Load files to copy from dedicated var file
ansible.builtin.include_vars: paths.yml
- name: Copy needed files to destination
ansible.builtin.copy:
src: "{{ item }}"
dest: "{{ copy_files_destination_path }}"
mode: 0640
loop: "{{ copy_files_paths }}"
$ cat demo.yml
---
- name: Demo playbook for copy_files role usage
hosts: all
gather_facts: false
roles:
- role: copy_files
$ echo spin a docker container to play against
spin a docker container to play against
$ docker run -d --rm --name test-target.local python:latest tail -f /dev/null
18a0009cff023b866411d9e06f7ae26f82ea7f7296c8f0c75b188dfffed0e82b
$ ansible-playbook -i inventories/demo/ demo.yml
PLAY [Demo playbook for copy_files role usage] *****************************************************************************************************************************************************************************************
TASK [copy_files : Make sure destination path exists] **********************************************************************************************************************************************************************************
changed: [test-target.local]
TASK [copy_files : Load files to copy from dedicated var file] *************************************************************************************************************************************************************************
ok: [test-target.local]
TASK [copy_files : Copy needed files to destination] ***********************************************************************************************************************************************************************************
changed: [test-target.local] => (item=file1.txt)
changed: [test-target.local] => (item=file5.txt)
changed: [test-target.local] => (item=file8.txt)
changed: [test-target.local] => (item=file10.txt)
PLAY RECAP *****************************************************************************************************************************************************************************************************************************
test-target.local : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
$ docker exec -it test-target.local ls /tmp/toto
file1.txt file10.txt file5.txt file8.txt
$ docker stop test-target.local
test-target.local
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment