From ff3bcade1b86d438ca97640fdc63ed62676a3fa4 Mon Sep 17 00:00:00 2001 From: Tim Stallard Date: Sun, 24 May 2020 15:38:47 +0100 Subject: [PATCH] start of server attributes check --- README.md | 1 + playbook.yml | 2 ++ roles/server-attributes/files/attributes.py | 23 +++++++++++++++++++++ roles/server-attributes/files/netbox.pub | 1 + roles/server-attributes/tasks/main.yml | 15 ++++++++++++++ 5 files changed, 42 insertions(+) create mode 100644 roles/server-attributes/files/attributes.py create mode 100644 roles/server-attributes/files/netbox.pub create mode 100644 roles/server-attributes/tasks/main.yml diff --git a/README.md b/README.md index 19904d8..0c7a9ca 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,7 @@ New roles should be developed on branches, and changes rolled out to all servers - `nrpe` - `cron-mail-redirect` - `unattended-upgrades` + - `server-attributes` - `pxe` - `dnsmasq` - `ipxe` diff --git a/playbook.yml b/playbook.yml index a191d45..a74a086 100644 --- a/playbook.yml +++ b/playbook.yml @@ -14,6 +14,8 @@ tags: unattended-upgrades - role: cron-mail-redirect tags: cron-mail-redirect + - role: server-attributes + tags: server-attributes tags: common - hosts: device_roles_Router roles: diff --git a/roles/server-attributes/files/attributes.py b/roles/server-attributes/files/attributes.py new file mode 100644 index 0000000..043d9a5 --- /dev/null +++ b/roles/server-attributes/files/attributes.py @@ -0,0 +1,23 @@ +#!/usr/bin/python3 +import json +import subprocess + +data = {} + +with open("/etc/os-release", "r") as fh: + for line in fh: + k, v = line.split("=", 1) + v = v.strip().strip("\"") + if k == "ID": + data["distro"] = v + elif k == "VERSION_ID": + data["version"] = v + +# some of our servers don't support iproute2 json output +# just send back a None +try: + data["network"] = json.loads(subprocess.check_output(["ip", "-details", "-json", "addr"])) +except: + data["network"] = None + +print(json.dumps(data)) diff --git a/roles/server-attributes/files/netbox.pub b/roles/server-attributes/files/netbox.pub new file mode 100644 index 0000000..ece8a27 --- /dev/null +++ b/roles/server-attributes/files/netbox.pub @@ -0,0 +1 @@ +ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC0pNFstmYWf7cWHRKdZd10W0EHM9Mg+Ab1no61930dK5wbcJ6Axjg+buFpMdv8ghrpFI5lITGlExoGQSWcg27MqgUNPlZddfMK57qOtcj7UumyFBji2cKrr5qnZ7NB5Mp6ZFuCXzGwG7uEPdrnJuVYg+ZE8gzneH3pftx2OFsDw4WXOS5d3plUlGB8tLr8meIfOM+Jb1yQcyT1/nFQHmYs4LmZjFO/I9/VzC1+AaPLaDLVxAf5y3ZxGGZHDzA0iGxhZPFpPh1SGots8V2MwPtcI0Ok+PsoeOPhG8Trv7FCNYDzovWf2auW9iQbiaFNmtTRNAgNA8Yx2ev8vIOAJLan netbox@netbox diff --git a/roles/server-attributes/tasks/main.yml b/roles/server-attributes/tasks/main.yml new file mode 100644 index 0000000..7a1b6a7 --- /dev/null +++ b/roles/server-attributes/tasks/main.yml @@ -0,0 +1,15 @@ +- name: add attributes script dir + file: + path: /opt/sown/server-attributes/ + state: directory +- name: add netbox key + authorized_key: + user: root + state: present + key: "{{ lookup('file', 'netbox.pub') }}" + key_options: 'restrict,command="/opt/sown/server-attributes/attributes.py"' +- name: add attributes script + copy: + src: attributes.py + dest: /opt/sown/server-attributes/attributes.py + mode: "700"