From e731e539fa51b0546566e6ea54af881bfaee8140 Mon Sep 17 00:00:00 2001
From: Freezed <2160318-free_zed@users.noreply.gitlab.com>
Date: Tue, 4 Jan 2022 00:14:18 +0100
Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20a=20basic=20tasks:=20host=5Fi?=
 =?UTF-8?q?nfo=20&=20whoami?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .gitignore       |  1 +
 Makefile         | 12 ++++++++++++
 README.md        | 26 +++++++++++++++++++++++++-
 host_info.yml    |  8 ++++++++
 inventory.sample | 16 ++++++++++++++++
 whoami.yml       | 28 ++++++++++++++++++++++++++++
 6 files changed, 90 insertions(+), 1 deletion(-)
 create mode 100644 Makefile
 create mode 100644 host_info.yml
 create mode 100644 inventory.sample
 create mode 100644 whoami.yml

diff --git a/.gitignore b/.gitignore
index a8b42eb..21516bb 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
 *.retry
+inventory
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..009213e
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,12 @@
+EDITOR = geany
+
+clean:
+	# Remove files not in source control
+	find . -type f -name "*.retry" -delete
+	find . -type f -name "*.orig" -delete
+
+open_all:
+	${EDITOR} .gitignore inventory Makefile host_info.yml README.md whoami.yml
+
+inventory_generation:
+	cp inventory.sample inventory && ${EDITOR} inventory
diff --git a/README.md b/README.md
index 3320145..fff4416 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,28 @@
 forga Ansible base playbook
 ===========================
 
-Setup a basic [Debian](https://www.debian.org) (and Ubuntu) config, suitable for server and workstation
+
+💡 Idea
+-------
+
+Get info and set basic config for _Debian_ and _Ubuntu_ distrib.
+
+Suitable for server and workstation.
+
+
+✨ Features
+-----------
+
+|   playbook                        |   return                                          |
+|-----------------------------------|---------------------------------------------------|
+| [`host_info.yml`](host_info.yml)  |   distribution full name & version                |
+| [`whoami.yml`](whoami.yml)        |   `ansible_user` & `become_user` (`sudo` method)  |
+
+
+🚀 Quickstart
+-------------
+
+1. Setup your `inventory` file from [`inventory.sample`](inventory.sample) :
+    - `make inventory_generation`
+1. Run `host_info` playbook to `<group_foo>` & `<group_bar>` :
+    - `ansible-playbook host_info.yml -i inventory -e host_list=<group_foo>:<group_bar>`
diff --git a/host_info.yml b/host_info.yml
new file mode 100644
index 0000000..e46c177
--- /dev/null
+++ b/host_info.yml
@@ -0,0 +1,8 @@
+---
+- hosts: "{{ host_list }}"
+  remote_user: "{{ my_user }}"
+
+  tasks:
+    - name: Host Info
+      debug:
+        msg: "{{ ansible_hostname }} ({{ ansible_distribution }} {{ ansible_distribution_release }} v{{ ansible_distribution_version }})"
diff --git a/inventory.sample b/inventory.sample
new file mode 100644
index 0000000..267a16a
--- /dev/null
+++ b/inventory.sample
@@ -0,0 +1,16 @@
+[physical]
+192.168.1.1
+192.168.1.11
+
+[virtual]
+192.168.1.2
+192.168.1.22
+
+[server]
+192.168.1.1
+192.168.1.2
+
+[workstation]
+localhost
+192.168.1.11
+192.168.1.22
diff --git a/whoami.yml b/whoami.yml
new file mode 100644
index 0000000..7be5227
--- /dev/null
+++ b/whoami.yml
@@ -0,0 +1,28 @@
+---
+- hosts: "{{ host_list }}"
+  become_method: su
+  remote_user: root
+  become_user: "{{ my_user }}"
+
+  tasks:
+    - name: Who is «remote_user»
+      become: no
+      ansible.builtin.script:
+        cmd: /usr/bin/whoami
+      register: who_is_remote_user
+      changed_when: 0
+
+    - name: Who is «become_user»
+      become: yes
+      ansible.builtin.script:
+        cmd: /usr/bin/whoami
+      register: who_is_become_user
+      changed_when: 0
+
+    - name: Who is «remote_user»?
+      ansible.builtin.debug:
+        msg: "I am «{{ who_is_remote_user.stdout_lines }}»"
+
+    - name: Who is «become_user»?
+      ansible.builtin.debug:
+        msg: "I am {{ who_is_become_user.stdout_lines | flatten | first }}"
-- 
GitLab