From 960c02f91445722418b7438eeaa3291a880e2a91 Mon Sep 17 00:00:00 2001 From: Tanguy Herbron Date: Sat, 16 Oct 2021 18:29:06 +0200 Subject: [PATCH] Development of auto-inventory --- setup/inventory_builder.py | 25 +++++++++++++++++++++++++ setup/inventory_builder.sh | 3 +++ setup/testing.yml | 5 +++++ setup/user_setup.yml | 14 ++++++++++++++ tmp_hosts | 1 + 5 files changed, 48 insertions(+) create mode 100644 setup/inventory_builder.py create mode 100644 setup/inventory_builder.sh create mode 100644 setup/testing.yml create mode 100644 setup/user_setup.yml create mode 100644 tmp_hosts diff --git a/setup/inventory_builder.py b/setup/inventory_builder.py new file mode 100644 index 0000000..908b231 --- /dev/null +++ b/setup/inventory_builder.py @@ -0,0 +1,25 @@ +from paramiko import SSHClient,AutoAddPolicy +from getpass import getpass + +address = input("Machine address: ") +username = input("SSH user: ") +password = getpass() + +print("Connecting to", address, "as", username, "...") +client = SSHClient() +client.set_missing_host_key_policy(AutoAddPolicy()) + +client.connect(address, username=username, password=password) +stdin, stdout, stderr = client.exec_command("su") +stdin.write(password + '\n') +stdin.write("touch /home/tanguy/testing\n") +stdin.write("exit\n") +stdin.flush() +print("Return", stdout.read().decode("utf8")) + +ret = stdout.channel.recv_exit_status() + +stdin.close() +stdout.close() +stderr.close() +client.close() diff --git a/setup/inventory_builder.sh b/setup/inventory_builder.sh new file mode 100644 index 0000000..eb7663b --- /dev/null +++ b/setup/inventory_builder.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +echo "" diff --git a/setup/testing.yml b/setup/testing.yml new file mode 100644 index 0000000..778e5a1 --- /dev/null +++ b/setup/testing.yml @@ -0,0 +1,5 @@ +--- +- name: Testing tasks + hosts: all + tasks: + - debug: var=ansible_all_ipv4_addresses diff --git a/setup/user_setup.yml b/setup/user_setup.yml new file mode 100644 index 0000000..867b1b2 --- /dev/null +++ b/setup/user_setup.yml @@ -0,0 +1,14 @@ +--- +- name: Setup ansible configuration for new host + hosts: all + tasks: + - name: Add 'ansible' system user and setup SSH key + become: yes + user: + name: ansible + comment: Automation user for ansible + system: True + create_home: False + generate_ssh_key: yes + ssh_key_bits: 4096 + ssh_key_file: /etc/ansible/.id_rsa diff --git a/tmp_hosts b/tmp_hosts new file mode 100644 index 0000000..1706c4e --- /dev/null +++ b/tmp_hosts @@ -0,0 +1 @@ +10.10.0.23