From a9f3b8347fb755013821894b19f606f572f7b749 Mon Sep 17 00:00:00 2001 From: Olli Graf Date: Wed, 16 Jul 2025 13:59:07 +0200 Subject: [PATCH] Teil 11 Loops --- .gitignore | 2 ++ loop.yml | 8 ++++++++ loop_files.yml | 7 +++++++ noloop.yml | 8 ++++++++ roles/loop/tasks/main.yml | 7 +++++++ roles/noloop/tasks/main.yml | 21 +++++++++++++++++++++ roles/subtasks/setuploop.yml | 10 ++++++++++ roles/with_items/tasks/main.yml | 13 +++++++++++++ with_items.yml | 8 ++++++++ 9 files changed, 84 insertions(+) create mode 100644 .gitignore create mode 100644 loop.yml create mode 100644 loop_files.yml create mode 100644 noloop.yml create mode 100644 roles/loop/tasks/main.yml create mode 100644 roles/noloop/tasks/main.yml create mode 100644 roles/subtasks/setuploop.yml create mode 100644 roles/with_items/tasks/main.yml create mode 100644 with_items.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..76e579a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +test + diff --git a/loop.yml b/loop.yml new file mode 100644 index 0000000..2df388c --- /dev/null +++ b/loop.yml @@ -0,0 +1,8 @@ +- hosts: "{{ target }}" + vars_files: + - loop_files.yml + gather_facts: false + remote_user: pi + roles: + - loop + diff --git a/loop_files.yml b/loop_files.yml new file mode 100644 index 0000000..47206fe --- /dev/null +++ b/loop_files.yml @@ -0,0 +1,7 @@ +testfiles: + - test1.txt + - test1.json + - test2.txt + - test3.txt + - test4.dat + diff --git a/noloop.yml b/noloop.yml new file mode 100644 index 0000000..b8433ee --- /dev/null +++ b/noloop.yml @@ -0,0 +1,8 @@ +- hosts: "{{ target }}" + vars_files: + - loop_files.yml + gather_facts: false + remote_user: pi + roles: + - noloop + diff --git a/roles/loop/tasks/main.yml b/roles/loop/tasks/main.yml new file mode 100644 index 0000000..4079fce --- /dev/null +++ b/roles/loop/tasks/main.yml @@ -0,0 +1,7 @@ +- include: ../subtasks/setuploop.yml + +- name: erzeuge Testdateien + ansible.builtin.file: + path: "test/{{ item }}" + state: touch + loop: "{{ testfiles }}" diff --git a/roles/noloop/tasks/main.yml b/roles/noloop/tasks/main.yml new file mode 100644 index 0000000..891a0df --- /dev/null +++ b/roles/noloop/tasks/main.yml @@ -0,0 +1,21 @@ +- include: ../subtasks/setuploop.yml +- name: erzeuge test1.txt + ansible.builtin.file: + path: "test/test1.txt" + state: touch +- name: erzeuge test1.json + ansible.builtin.file: + path: "test/test1.json" + state: touch +- name: erzeuge test2.txt + ansible.builtin.file: + path: "test/test2.txt" + state: touch +- name: erzeuge test3.txt + ansible.builtin.file: + path: "test/test3.txt" + state: touch +- name: erzeuge testr4.dat + ansible.builtin.file: + path: "test/test4.dat" + state: touch diff --git a/roles/subtasks/setuploop.yml b/roles/subtasks/setuploop.yml new file mode 100644 index 0000000..d54b551 --- /dev/null +++ b/roles/subtasks/setuploop.yml @@ -0,0 +1,10 @@ +--- +- name: create test dir + ansible.builtin.file: + path: test + state: directory +- name: delete test files + ansible.builtin.file: + path: "test/*" + state: absent + diff --git a/roles/with_items/tasks/main.yml b/roles/with_items/tasks/main.yml new file mode 100644 index 0000000..1ad637e --- /dev/null +++ b/roles/with_items/tasks/main.yml @@ -0,0 +1,13 @@ +- include: ../subtasks/setuploop.yml + +- name: erzeuge Testdateien + ansible.builtin.file: + path: "test/{{ item }}" + state: touch + with_items: + - test1.txt + - test1.json + - test2.txt + - test3.txt + - test4.dat + diff --git a/with_items.yml b/with_items.yml new file mode 100644 index 0000000..f3c2cb8 --- /dev/null +++ b/with_items.yml @@ -0,0 +1,8 @@ +- hosts: "{{ target }}" + vars_files: + - loop_files.yml + gather_facts: false + remote_user: pi + roles: + - with_items +