From 7e1348e5395068926b367af3d06d0a1dfeeb5c03 Mon Sep 17 00:00:00 2001 From: L3D Date: Sat, 11 Jun 2022 15:12:13 +0200 Subject: [PATCH 1/6] update submodules --- roles/bat | 2 +- roles/do1jlr.sshd | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/roles/bat b/roles/bat index f49dba9..1b4d972 160000 --- a/roles/bat +++ b/roles/bat @@ -1 +1 @@ -Subproject commit f49dba9f447ae01a73d61bf582b68585e4414d5b +Subproject commit 1b4d9721d1b6a0d1b0786fd5f46409df11b34d07 diff --git a/roles/do1jlr.sshd b/roles/do1jlr.sshd index 878f405..15b7867 160000 --- a/roles/do1jlr.sshd +++ b/roles/do1jlr.sshd @@ -1 +1 @@ -Subproject commit 878f405dfc4056132e445bdd7322029f55ec1e32 +Subproject commit 15b7867a37e4036947de29c727075c4a75859e43 From b44d717ed952a49ff164e250d2744eb540089042 Mon Sep 17 00:00:00 2001 From: L3D Date: Sun, 12 Jun 2022 22:36:35 +0200 Subject: [PATCH 2/6] start adding inventory --- inventory.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100755 inventory.py diff --git a/inventory.py b/inventory.py new file mode 100755 index 0000000..7b51fe4 --- /dev/null +++ b/inventory.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Create a dynamic inventory for this ansible playbook +""" +import socket +import sys + +# create a dict to match hostnames to enviroments +env_dict = { + 'work': + ['workstation.local'], + 'private': + ['dderpy.local', 'foo.bar'] +} + +def fqdn(): + """ + return fully qualified domain name + """ + return socket.getfqdn() + +def env(domain): + """ + map a hostname to a space + """ + for key, values in env_dict.items(): + if domain in values: + return key + sys.exit('{"group": { "hosts": ["example.com"], "vars": {} }, "_meta": { "foo": "bar" }}') + + +def main(): + """ + main funktion + will analyse on which host this script is started + and will print the dynamic inventory to tell ansible + which host_vars and group_vars should be used + """ + host = fqdn() + group = env(host) + print(host + group) +# { +# "group": +# { "hosts": ["127.0.0.1", "::1"], "vars": {} }, +# "_meta": +# { "hostvars": { "192.168.28.71": { "host_specific_var": "bar" }, +# "192.168.28.72": { "host_specific_var": "foo" }} } +# } + +main() From 293cff26497657d933a6572f1e6107bdd778d9f6 Mon Sep 17 00:00:00 2001 From: L3D Date: Sat, 18 Jun 2022 13:16:59 +0200 Subject: [PATCH 3/6] successful return empty inv --- inventory.py | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/inventory.py b/inventory.py index 7b51fe4..609b0e7 100755 --- a/inventory.py +++ b/inventory.py @@ -5,6 +5,7 @@ Create a dynamic inventory for this ansible playbook """ import socket import sys +import json # create a dict to match hostnames to enviroments env_dict = { @@ -27,8 +28,16 @@ def env(domain): for key, values in env_dict.items(): if domain in values: return key - sys.exit('{"group": { "hosts": ["example.com"], "vars": {} }, "_meta": { "foo": "bar" }}') + print(json.dumps(empty_host_list(), sort_keys=True, indent=2)) + sys.exit() +def empty_host_list(): + """ + return empty host list + """ + comment = "No valid host found. returning empty host list!" + return json.loads('{"_meta": {"comment": "' + comment + + '", "hostvars": {}}, "instances": {"hosts": []}}') def main(): """ @@ -40,12 +49,14 @@ def main(): host = fqdn() group = env(host) print(host + group) -# { -# "group": -# { "hosts": ["127.0.0.1", "::1"], "vars": {} }, -# "_meta": -# { "hostvars": { "192.168.28.71": { "host_specific_var": "bar" }, -# "192.168.28.72": { "host_specific_var": "foo" }} } +#{ +# "_meta": { +# "hostvars": { } +# }, +# +# "instances": { +# "hosts": ["10.66.70.33"] +# } # } main() From c23aafa008f00c49a8d17901b61a33d8b635ccd1 Mon Sep 17 00:00:00 2001 From: L3D Date: Sat, 18 Jun 2022 13:45:34 +0200 Subject: [PATCH 4/6] update linting --- inventory.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/inventory.py b/inventory.py index 609b0e7..d119343 100755 --- a/inventory.py +++ b/inventory.py @@ -12,7 +12,7 @@ env_dict = { 'work': ['workstation.local'], 'private': - ['dderpy.local', 'foo.bar'] + ['derpy.local', 'foo.bar'] } def fqdn(): @@ -24,6 +24,7 @@ def fqdn(): def env(domain): """ map a hostname to a space + or print empty list if no one matched and exit """ for key, values in env_dict.items(): if domain in values: @@ -39,6 +40,13 @@ def empty_host_list(): return json.loads('{"_meta": {"comment": "' + comment + '", "hostvars": {}}, "instances": {"hosts": []}}') +def formated_host_group_list(host, group): + """ + build inventory and return it + """ + # pylint: disable=line-too-long + return json.loads('{"_meta": {"hostvars": {}},"' + str(group) + '": {"hosts": ["' + str(host) + '"]},"instances": {"children": ["' + str(group) + '"]}}') + def main(): """ main funktion @@ -48,7 +56,10 @@ def main(): """ host = fqdn() group = env(host) - print(host + group) + print(json.dumps(formated_host_group_list(host, group), sort_keys=True, indent=2)) + + + #{ # "_meta": { # "hostvars": { } From 75609fb2a398d908a4abe94b8f13daad04a0ae63 Mon Sep 17 00:00:00 2001 From: L3D Date: Sat, 18 Jun 2022 14:22:12 +0200 Subject: [PATCH 5/6] automated linting --- .github/workflows/pylint.yml | 15 +++++++++++++++ .github/workflows/yamllint.yaml | 2 -- setup_desk_minni.yml | 4 ++-- setup_l14.yml | 4 ++-- setup_t460p.yml | 2 +- 5 files changed, 20 insertions(+), 7 deletions(-) create mode 100644 .github/workflows/pylint.yml diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml new file mode 100644 index 0000000..876c6ad --- /dev/null +++ b/.github/workflows/pylint.yml @@ -0,0 +1,15 @@ +--- +name: 'Pylint GitHub Actions' + +# yamllint disable-line rule:truthy +on: [push, pull_request] + +jobs: + pylint: + name: 'Pylint' + runs-on: ubuntu-latest + steps: + - name: 'Checkout' + uses: actions/checkout@master + - name: GitHub Action for pylint + uses: cclauss/GitHub-Action-for-pylint@0.7.0 diff --git a/.github/workflows/yamllint.yaml b/.github/workflows/yamllint.yaml index 39c49f8..c4894bd 100644 --- a/.github/workflows/yamllint.yaml +++ b/.github/workflows/yamllint.yaml @@ -18,5 +18,3 @@ jobs: yamllint_config_filepath: './.yamllint' yamllint_strict: false yamllint_comment: true -# env: -# GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN } diff --git a/setup_desk_minni.yml b/setup_desk_minni.yml index 3a573a0..dec07ef 100644 --- a/setup_desk_minni.yml +++ b/setup_desk_minni.yml @@ -21,7 +21,7 @@ hosts: desk_minni.local roles: - {role: akku-warning, tags: akku} - # - {role: pulseaudio, tags: [pulse, audio, pulseaudio]} + # - {role: pulseaudio, tags: [pulse, audio, pulseaudio]} - {role: networkmanager, tags: networkmanager, when: ansible_os_family == 'Archlinux'} - {role: openvpn, tags: ovpn} - {role: nextcloud, tags: nextcloud, when: ansible_os_family == 'Archlinux'} @@ -32,7 +32,7 @@ - {role: ntp, tags: ntp} - {role: xrandr, tags: xrandr} - {role: arch-fonts, tags: fonts} - # - {role: winehq, tags: wine} + # - {role: winehq, tags: wine} - {role: no-sleep, tags: sleep} - {role: do1jlr.avahi_daemon, tags: [avahi_daemon, avahi]} - {role: do1jlr.avahi_client, tags: [avahi_client, mdns]} diff --git a/setup_l14.yml b/setup_l14.yml index e87c712..3fc3975 100644 --- a/setup_l14.yml +++ b/setup_l14.yml @@ -22,7 +22,7 @@ hosts: l14.local roles: - {role: akku-warning, tags: akku} -# - {role: pulseaudio, tags: [pulse, audio, pulseaudio]} + # - {role: pulseaudio, tags: [pulse, audio, pulseaudio]} - {role: networkmanager, tags: networkmanager, when: ansible_os_family == 'Archlinux'} - {role: openvpn, tags: ovpn} - {role: nextcloud, tags: nextcloud, when: ansible_os_family == 'Archlinux'} @@ -33,7 +33,7 @@ - {role: ntp, tags: ntp} - {role: xrandr, tags: xrandr} - {role: arch-fonts, tags: fonts} -# - {role: winehq, tags: wine} + # - {role: winehq, tags: wine} - {role: no-sleep, tags: sleep} - {role: do1jlr.avahi_daemon, tags: [avahi_daemon, avahi]} - {role: do1jlr.avahi_client, tags: [avahi_client, mdns]} diff --git a/setup_t460p.yml b/setup_t460p.yml index 6bf5acf..00f38a8 100644 --- a/setup_t460p.yml +++ b/setup_t460p.yml @@ -21,7 +21,7 @@ hosts: t460p.local roles: - {role: akku-warning, tags: akku} -# - {role: pulseaudio, tags: [pulse, audio, pulseaudio]} + # - {role: pulseaudio, tags: [pulse, audio, pulseaudio]} - {role: networkmanager, tags: networkmanager, when: ansible_os_family == 'Archlinux'} - {role: openvpn, tags: ovpn} - {role: nextcloud, tags: nextcloud, when: ansible_os_family == 'Archlinux'} From 4c11fabaee8eabe8ff8d03f01b131a0775949d06 Mon Sep 17 00:00:00 2001 From: L3D Date: Sat, 18 Jun 2022 14:40:06 +0200 Subject: [PATCH 6/6] Start rewriting README --- README.md | 16 ++++++++++------ ansible.cfg | 2 +- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 90b1e27..f174d10 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,20 @@ Ansible Linux Desktop Setup ========================== -This ansible playbook collection creates [L3D](https://chaos.social/@l3d)s Desktop enviroment. Including window manager and some pre-installed programms like [Firefox](https://www.mozilla.org/de/firefox/new/) and some usefull shell programms. +This ansible playbook collection manages some of my workstations and laptops. Because of this it sometimes contains very specific variables like my username, SSH keys or similar data that may not be the best choice for your system. - ATTENTION +Nevertheless, this ansible playbook is not only publicly available on the internet, but by the MIT license a part of free open-source ansible, which may serve you as inspiration within the framework of the MIT license. + + + Inventory ------------- -Different to my other ansible playbooks: +This is my first ansible with dynamic inventory. The [inventory.py](inventory.py) script looks at which hostname it was lauched on. If the hostname is known, the host is mapped to the group stored for it and a local connection to the host is established. -### THIS PLAYBOOK HAS TO BE EXECUTET AT THE TARGET HOST DIRECTLY! +This has the advantage that different environments are automatically recognized and significantly less danger of accidentally rolling out the ansible with the variables for a completely different host and thus configuring things that were not intended for this device. -*It requires some GUI stuff and I did not find the time to forward X or wayland correctly to make it remotely working. Sorry. Feel free to create a Issue or pull-request* +Obviously, this also means that **this playbook must always be run on the host you want to manage** and this ansible playbook is not meant to be run remotely. - Install tipps: + +Install tipps: ----------------------- ```bash # Clone Git diff --git a/ansible.cfg b/ansible.cfg index 0578235..51c1513 100644 --- a/ansible.cfg +++ b/ansible.cfg @@ -1,5 +1,5 @@ [defaults] -inventory = ./hosts.ini +inventory = ./inventory.py retry_files_enabled = False nocows = True ansible_connection = 'local'