From 93bfec95eb453c594cffc68d1cb2cac610f2245a Mon Sep 17 00:00:00 2001 From: ikerbs Date: Tue, 2 Nov 2021 17:14:48 +0300 Subject: [PATCH] add hosts role --- README.md | 18 ++++++++++++++++++ defaults/main.yaml | 1 + meta/main.yml | 7 +++++++ tasks/main.yaml | 16 ++++++++++++++++ 4 files changed, 42 insertions(+) create mode 100644 README.md create mode 100644 defaults/main.yaml create mode 100644 meta/main.yml create mode 100644 tasks/main.yaml diff --git a/README.md b/README.md new file mode 100644 index 0000000..1727009 --- /dev/null +++ b/README.md @@ -0,0 +1,18 @@ +Add records in format ${ansible_host} ${FQDN} ${short} from inventory to /etc/hosts + +Config example: + + pin_hosts: yes + +Inventory example: + + node1.site.local ansible_host=10.2.1.5 + node2.site.local ansible_host=10.2.1.6 + node3.site.local ansible_host=10.2.1.7 + +Result /etc/hosts: + + 10.2.1.5 node1.site.local node1 + 10.2.1.6 node2.site.local node2 + 10.2.1.7 node3.site.local node3 + diff --git a/defaults/main.yaml b/defaults/main.yaml new file mode 100644 index 0000000..7f1def1 --- /dev/null +++ b/defaults/main.yaml @@ -0,0 +1 @@ +pin_hosts: "no" diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..8b761c1 --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,7 @@ +--- +galaxy_info: + description: Hostname + galaxy_tags: + - hostname + +dependencies: [] diff --git a/tasks/main.yaml b/tasks/main.yaml new file mode 100644 index 0000000..63a5fc0 --- /dev/null +++ b/tasks/main.yaml @@ -0,0 +1,16 @@ +--- +- block: + - name: add ip address of all hosts to all hosts + lineinfile: + dest: /etc/hosts + regexp: '.*{{ item }} {{ item.split(".")[0] }}$' + line: "{{ hostvars[item].ansible_host }} {{ item }} {{ item.split('.')[0] }}" + state: present + when: + - hostvars[item].ansible_host is defined + - hostvars[item].pin_hosts is defined + with_items: "{{ groups.all }}" + tags: + - hosts + + become: true