From 7a1a4709867e949bd8dd918521fbe9a8bf34b939 Mon Sep 17 00:00:00 2001
From: arcter <varga.mate@kszk.bme.hu>
Date: Tue, 9 Nov 2021 00:18:31 +0100
Subject: [PATCH] Add example playbook

---
 ansible/example-playbook/README.md            |  3 +++
 ansible/example-playbook/exampleplay.yml      | 25 +++++++++++++++++++
 ansible/example-playbook/files/README.md      |  2 ++
 ansible/example-playbook/tasks/README.md      |  5 ++++
 .../example-playbook/tasks/installpackage.yml | 23 +++++++++++++++++
 ansible/example-playbook/templates/README.md  | 15 +++++++++++
 ansible/example-playbook/vars/README.md       | 14 +++++++++++
 ansible/example-playbook/vars/example.yml     |  5 ++++
 ansible/inventory.yaml                        |  1 +
 ansible/playbook-name/README.md               |  3 ---
 10 files changed, 93 insertions(+), 3 deletions(-)
 create mode 100644 ansible/example-playbook/README.md
 create mode 100644 ansible/example-playbook/exampleplay.yml
 create mode 100644 ansible/example-playbook/files/README.md
 create mode 100644 ansible/example-playbook/tasks/README.md
 create mode 100644 ansible/example-playbook/tasks/installpackage.yml
 create mode 100644 ansible/example-playbook/templates/README.md
 create mode 100644 ansible/example-playbook/vars/README.md
 create mode 100644 ansible/example-playbook/vars/example.yml
 delete mode 100644 ansible/playbook-name/README.md

diff --git a/ansible/example-playbook/README.md b/ansible/example-playbook/README.md
new file mode 100644
index 0000000..4324f4b
--- /dev/null
+++ b/ansible/example-playbook/README.md
@@ -0,0 +1,3 @@
+This is an example playbook.
+
+Kubernetes example: https://git.sch.bme.hu/kszk/sysadmin/kubernetes/cluster-setup/-/tree/master/ansible/mgmt
diff --git a/ansible/example-playbook/exampleplay.yml b/ansible/example-playbook/exampleplay.yml
new file mode 100644
index 0000000..8a004ad
--- /dev/null
+++ b/ansible/example-playbook/exampleplay.yml
@@ -0,0 +1,25 @@
+- name: task examples
+  hosts: host1.sch.bme.hu
+  vars_files:
+  - vars/example.yml
+  tasks:
+    - name: Simple task
+      script: "echo hello world!"
+
+    - name: Import task file
+      import_tasks: tasks/installpackage.yml
+
+    - name: Only do task when requirement meet
+      debug:
+        msg: "Random task was executed"
+      when: condition
+
+    - name: use vars
+      debug:
+        msg: "{{ user }}"
+
+    - name: use loop
+      debug:
+        msg: "{{ item }}"
+      loop:
+        "{{ list }}"
diff --git a/ansible/example-playbook/files/README.md b/ansible/example-playbook/files/README.md
new file mode 100644
index 0000000..511e4d6
--- /dev/null
+++ b/ansible/example-playbook/files/README.md
@@ -0,0 +1,2 @@
+You may place your files here that should be copyed to the target machine,
+and doesn't need templating.
diff --git a/ansible/example-playbook/tasks/README.md b/ansible/example-playbook/tasks/README.md
new file mode 100644
index 0000000..1a98e6f
--- /dev/null
+++ b/ansible/example-playbook/tasks/README.md
@@ -0,0 +1,5 @@
+You may place your task files here.
+
+Task files are a list of tasks that can be executed separetly.
+You would use this if you want to group some tasks.
+Ie: you want to install Podman on Ubuntu 20.04
diff --git a/ansible/example-playbook/tasks/installpackage.yml b/ansible/example-playbook/tasks/installpackage.yml
new file mode 100644
index 0000000..bdbeb01
--- /dev/null
+++ b/ansible/example-playbook/tasks/installpackage.yml
@@ -0,0 +1,23 @@
+---
+- name: Add the GPG key for the repository
+  become: true
+  ansible.builtin.apt_key:
+    url: https://urlforgpgkey
+    state: present
+
+- name: Add the repository
+  become: true
+  ansible.builtin.apt_repository:
+    repo: "deb http://hu.archive.ubuntu.com/ubuntu/ focal main restricted" #Example line
+    state: present
+
+- name: "Update apt packages"
+  become: true
+  apt:
+    upgrade: yes
+    update_cache: yes
+
+- name: Install the software
+  apt:
+    name: Name_of_SW
+    state: present
\ No newline at end of file
diff --git a/ansible/example-playbook/templates/README.md b/ansible/example-playbook/templates/README.md
new file mode 100644
index 0000000..64f8104
--- /dev/null
+++ b/ansible/example-playbook/templates/README.md
@@ -0,0 +1,15 @@
+You may place templates here. Templates can use variables that will be replaced by the task later.
+Example for a template:
+```
+This file holds the {{ meaning_of_life }}.
+```
+If you give value for the `meaning_of_life`, then you can copy it by this task:
+```
+- name: Use template
+  vars:
+    meaning_of_life: 42
+  template:
+    src: ../templates/README.md
+    dest: /the/full/path/of/destination
+# You may set other parameters of the target file such as owner or permissins
+```
\ No newline at end of file
diff --git a/ansible/example-playbook/vars/README.md b/ansible/example-playbook/vars/README.md
new file mode 100644
index 0000000..ff37757
--- /dev/null
+++ b/ansible/example-playbook/vars/README.md
@@ -0,0 +1,14 @@
+You may place your var files here.
+Var files can hold variables that can be used by tasks, rules and playes.
+The vars should use the "key: value" form.
+If you use variables that should be encrypted you can do so by ansible-vault
+Encrypt it
+
+```bash
+ansible-vault encrypt file_name
+```
+
+Decrypt it
+```bash
+ansible-vault decrypt file_name
+```
diff --git a/ansible/example-playbook/vars/example.yml b/ansible/example-playbook/vars/example.yml
new file mode 100644
index 0000000..1f56734
--- /dev/null
+++ b/ansible/example-playbook/vars/example.yml
@@ -0,0 +1,5 @@
+list:
+  - one: 1
+  - two: 2
+  - three: 3
+user: {name: user1 , home_address: moon}
\ No newline at end of file
diff --git a/ansible/inventory.yaml b/ansible/inventory.yaml
index f47cfbc..0c2bdd9 100644
--- a/ansible/inventory.yaml
+++ b/ansible/inventory.yaml
@@ -1,4 +1,5 @@
 all:
+  #You should be able to SSH into all of these machines
   hosts:
     # Use OpenSSH config to make it comfortable
     host1.sch.bme.hu: 
diff --git a/ansible/playbook-name/README.md b/ansible/playbook-name/README.md
deleted file mode 100644
index 3ccf8cd..0000000
--- a/ansible/playbook-name/README.md
+++ /dev/null
@@ -1,3 +0,0 @@
-You place here your usual playbook.
-
-Example: https://git.sch.bme.hu/kszk/sysadmin/kubernetes/cluster-setup/-/tree/master/ansible/mgmt
-- 
GitLab