From bc4d380c063f8e6a35d0ede2c282fd67e0d50df2 Mon Sep 17 00:00:00 2001 From: rblundon Date: Wed, 26 Mar 2025 10:52:39 -0500 Subject: [PATCH 1/4] move pc --- 03-infra01-server/install.yml | 20 ++++++++++ dns-entry.yml | 40 ++++++++++++++++++++ group_vars/all/vars | 6 +++ group_vars/freeipa/vars | 2 +- group_vars/{hub-cluster => hub_cluster}/vars | 2 +- host_vars/pve01/vars | 6 +++ inventory.yml | 7 ++-- proxmox.yml | 2 + site.yml | 14 +++++++ templates/dns.tf.j2 | 11 ++++++ terraform/dns/provider.tf | 32 ++++++++++++++++ 11 files changed, 136 insertions(+), 6 deletions(-) create mode 100644 dns-entry.yml rename group_vars/{hub-cluster => hub_cluster}/vars (87%) create mode 100644 host_vars/pve01/vars create mode 100644 proxmox.yml create mode 100644 site.yml create mode 100644 templates/dns.tf.j2 create mode 100644 terraform/dns/provider.tf diff --git a/03-infra01-server/install.yml b/03-infra01-server/install.yml index 7098b4c..2361270 100644 --- a/03-infra01-server/install.yml +++ b/03-infra01-server/install.yml @@ -2,6 +2,7 @@ - name: Install all software on infra01 hosts: infra01 gather_facts: true + become: true collections: - somaz94.ansible_k8s_iac_tool @@ -19,6 +20,24 @@ state: present become: true + - name: Install dnf-plugins-core + ansible.builtin.package: + name: dnf-plugins-core + state: present + become: true + + - name: Install hashicorp repo + ansible.builtin.package: + name: https://rpm.releases.hashicorp.com/fedora/hashicorp.repo + https://rpm.releases.hashicorp.com/fedora/hashicorp.repo + state: present + + - name: Install terraform + ansible.builtin.package: + name: terraform + state: present + become: true + # - name: Clone Git repository # ansible.builtin.git: # repo: "https://github.com/rblundon/homelab.git" @@ -28,3 +47,4 @@ roles: - install_kustomize + diff --git a/dns-entry.yml b/dns-entry.yml new file mode 100644 index 0000000..3377683 --- /dev/null +++ b/dns-entry.yml @@ -0,0 +1,40 @@ +--- +# file: dns-entry.yml + +- name: Master playbook for home lab + hosts: all + gather_facts: false + tasks: + - name: Create DNS entry for {{ inventory_hostname }} + when: ip_address is defined + delegate_to: "{{ dns_server }}" + block: + - name: Check if terraform directory exists + ansible.builtin.stat: + path: ~/homelab/terraform/dns + register: terraform_dir + + - name: Create Terraform directory for DNS entries + ansible.builtin.file: + path: ~/homelab/terraform/dns + state: directory + mode: '0755' + when: not terraform_dir.stat.exists + + - name: Create Terraform file for host + ansible.builtin.template: + src: templates/dns.tf.j2 + dest: ~/homelab/terraform/dns/{{ inventory_hostname }}.tf + mode: '0644' + + handlers: + - name: Update DNS entries via Terraform + delegate_to: "{{ dns_server }}" + community.general.terraform: + project_path: ~/homelab/terraform/dns/ + variables: + freeipa_server: "{{ dns_server }}" + freeipa_admin: "{{ dns_admin }}" + freeipa_password: "{{ vault_freeipa_password }}" + state: present + force_init: true diff --git a/group_vars/all/vars b/group_vars/all/vars index ed97d53..c91009f 100644 --- a/group_vars/all/vars +++ b/group_vars/all/vars @@ -1 +1,7 @@ --- +# file: group_vars/all + +# DNS variables +dns_server: "infra01.int.mk-labs.cloud" +dns_admin: "admin" +base_domain: "int.mk-labs.cloud" \ No newline at end of file diff --git a/group_vars/freeipa/vars b/group_vars/freeipa/vars index 49b80a1..8a5cebd 100644 --- a/group_vars/freeipa/vars +++ b/group_vars/freeipa/vars @@ -1,3 +1,3 @@ --- -freeipa_user: "admin" +#freeipa_user: "admin" #freeipa_password: "{{ vault_freeipa_password }}" diff --git a/group_vars/hub-cluster/vars b/group_vars/hub_cluster/vars similarity index 87% rename from group_vars/hub-cluster/vars rename to group_vars/hub_cluster/vars index 3c45e44..2928cfd 100644 --- a/group_vars/hub-cluster/vars +++ b/group_vars/hub_cluster/vars @@ -1,6 +1,6 @@ cluster_name: "hub" cluster_version: "4.17.20" -base_domain: "int.mk-labs.cloud" +#base_domain: "int.mk-labs.cloud" worker_node_count: 0 master_node_count: 1 cluster_network: "10.128.0.0/14" diff --git a/host_vars/pve01/vars b/host_vars/pve01/vars new file mode 100644 index 0000000..8159029 --- /dev/null +++ b/host_vars/pve01/vars @@ -0,0 +1,6 @@ +--- +# file: host_vars/pve01/vars +# mac_address: "98:b7:85:1e:c6:f1" +ip_address: "10.1.71.51" +hostname: "pve01.{{ base_domain }}" +# primary_interface: "enp1s0f0" \ No newline at end of file diff --git a/inventory.yml b/inventory.yml index 2122fd7..e568817 100644 --- a/inventory.yml +++ b/inventory.yml @@ -1,8 +1,7 @@ +# file: inventory.yml proxmox: hosts: - pve01: - pve02: - pve03: + pve0[1:3]: freeipa: hosts: @@ -12,6 +11,6 @@ matchbox: hosts: infra01: -hub-cluster: +hub_cluster: hosts: ocp-hub: diff --git a/proxmox.yml b/proxmox.yml new file mode 100644 index 0000000..0f741cf --- /dev/null +++ b/proxmox.yml @@ -0,0 +1,2 @@ +--- +# file: proxmox.yml diff --git a/site.yml b/site.yml new file mode 100644 index 0000000..8d63f8b --- /dev/null +++ b/site.yml @@ -0,0 +1,14 @@ +--- +# file: site.yml + +- name: Master playbook for home lab + hosts: all + gather_facts: true + connection: local + # roles: + # - dns-entry + +# tasks: + +- name: Process DNS entries + ansible.builtin.import_playbook: dns-entry.yml diff --git a/templates/dns.tf.j2 b/templates/dns.tf.j2 new file mode 100644 index 0000000..dd07de8 --- /dev/null +++ b/templates/dns.tf.j2 @@ -0,0 +1,11 @@ +// Terraform DNS entry template + +resource "freeipa_dns_record" "{{ inventory_hostname }}" { + zone_name = "{{ base_domain }}." + name = "{{ inventory_hostname }}" + type = "A" + records = [ + "{{ hostvars[inventory_hostname].ip_address }}", + ] + ttl = 60 +} diff --git a/terraform/dns/provider.tf b/terraform/dns/provider.tf new file mode 100644 index 0000000..13f47d0 --- /dev/null +++ b/terraform/dns/provider.tf @@ -0,0 +1,32 @@ +# Configure the DNS Provider +provider "freeipa" { + host = "infra01.int.mk-labs.cloud" + username = "admin" + password = var.freeipa_password + insecure = true +} + +variable "freeipa_server" { + description = "The server for the FreeIPA provider" + type = string +} + +variable "freeipa_admin" { + description = "The admin username for the FreeIPA provider" + type = string +} + +variable "freeipa_password" { + description = "The password for the FreeIPA provider" + type = string + sensitive = true +} + +terraform { + required_providers { + freeipa = { + source = "rework-space-com/freeipa" + version = "5.0.0" + } + } +} From ddd5bfc3ac57652df1aebdb41bb2e3a3c83e9c3f Mon Sep 17 00:00:00 2001 From: rblundon Date: Wed, 26 Mar 2025 17:11:52 -0500 Subject: [PATCH 2/4] Move PC --- .vault_pass.txt | 1 + 03-infra01-server/install.yml | 17 +++------- frr-openshift.conf | 59 +++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 12 deletions(-) create mode 100644 .vault_pass.txt create mode 100644 frr-openshift.conf diff --git a/.vault_pass.txt b/.vault_pass.txt new file mode 100644 index 0000000..add09e1 --- /dev/null +++ b/.vault_pass.txt @@ -0,0 +1 @@ +H3sVxt9kbuoXGvG diff --git a/03-infra01-server/install.yml b/03-infra01-server/install.yml index 2361270..5b084e3 100644 --- a/03-infra01-server/install.yml +++ b/03-infra01-server/install.yml @@ -26,11 +26,11 @@ state: present become: true - - name: Install hashicorp repo - ansible.builtin.package: - name: https://rpm.releases.hashicorp.com/fedora/hashicorp.repo - https://rpm.releases.hashicorp.com/fedora/hashicorp.repo - state: present + - name: Download terraform + ansible.builtin.get_url: + url: https://releases.hashicorp.com/terraform/terraform_{{ terraform_version }}/terraform_{{ terraform_version }}_{{ ansible_facts.ansible_os_family }}_{{ ansible_facts.ansible_architecture }}.zip + dest: /tmp/terraform.zip + mode: 0755 - name: Install terraform ansible.builtin.package: @@ -38,13 +38,6 @@ state: present become: true - # - name: Clone Git repository - # ansible.builtin.git: - # repo: "https://github.com/rblundon/homelab.git" - # dest: ~ - # version: deploy-hub-cluster #main - # become: true - roles: - install_kustomize diff --git a/frr-openshift.conf b/frr-openshift.conf new file mode 100644 index 0000000..3083b1d --- /dev/null +++ b/frr-openshift.conf @@ -0,0 +1,59 @@ +! +frr version 8.1 +frr defaults traditional +hostname WorldDrive +domainname int.mk-labs.cloud +log syslog informational +service integrated-vtysh-config +! +router bgp 65002 + bgp router-id 10.1.71.1 + bgp log-neighbor-changes + no bgp default ipv4-unicast + neighbor internal peer-group + neighbor internal remote-as 65001 + neighbor external peer-group + neighbor external remote-as 65003 + neighbor 10.1.71.141 peer-group external + neighbor 10.1.71.141 description Compact1 + neighbor 10.1.71.142 peer-group external + neighbor 10.1.71.142 description Compact2 + neighbor 10.1.71.143 peer-group external + neighbor 10.1.71.143 description Compact3 + neighbor 10.1.71.131 peer-group internal + neighbor 10.1.71.131 description MF 3 + neighbor 10.1.71.132 peer-group internal + neighbor 10.1.71.132 description NUC2 + neighbor 10.1.71.133 peer-group internal + neighbor 10.1.71.133 description NUC3 + neighbor 10.1.71.134 peer-group internal + neighbor 10.1.71.134 description MF 3 + neighbor 10.1.71.135 peer-group internal + neighbor 10.1.71.135 description NUC2 + neighbor 10.1.71.136 peer-group internal + neighbor 10.1.71.136 description NUC3 + ! + address-family ipv4 unicast + ! no need to redistribute connected, as we are not advertising our own routes + ! redistribute connected + neighbor external activate + neighbor external soft-reconfiguration inbound + neighbor external route-map allow-external in + neighbor internal activate + neighbor internal soft-reconfiguration inbound + neighbor internal route-map allow-internal in + exit-address-family +exit +! +! allow any advertised routes in this range with up to 32 bits mask length +ip prefix-list external seq 5 permit 10.1.182.0/24 le 32 +ip prefix-list internal seq 5 permit 10.1.82.0/24 le 32 +! +route-map allow-external permit 10 + match ip address prefix-list external +exit +! +route-map allow-internal permit 10 + match ip address prefix-list internal +exit +! \ No newline at end of file From bda585fcd583afa8eee59c8357f425fd4f3d5123 Mon Sep 17 00:00:00 2001 From: rblundon Date: Thu, 27 Mar 2025 00:32:02 -0500 Subject: [PATCH 3/4] goodnight --- .vault_pass.txt | 1 - 03-infra01-server/install.yml | 30 ++++---- dns-entry.yml | 74 ++++++++++--------- homelab/terraform/dns/ocp-hub.tf | 11 +++ homelab/terraform/dns/pve01.tf | 11 +++ host_vars/infra01/vars | 11 +++ site.yml | 17 +++-- .../dns_provider.tf.j2 | 4 +- test.yml | 30 ++++---- 9 files changed, 116 insertions(+), 73 deletions(-) delete mode 100644 .vault_pass.txt create mode 100644 homelab/terraform/dns/ocp-hub.tf create mode 100644 homelab/terraform/dns/pve01.tf create mode 100644 host_vars/infra01/vars rename terraform/dns/provider.tf => templates/dns_provider.tf.j2 (89%) diff --git a/.vault_pass.txt b/.vault_pass.txt deleted file mode 100644 index add09e1..0000000 --- a/.vault_pass.txt +++ /dev/null @@ -1 +0,0 @@ -H3sVxt9kbuoXGvG diff --git a/03-infra01-server/install.yml b/03-infra01-server/install.yml index 5b084e3..d855c15 100644 --- a/03-infra01-server/install.yml +++ b/03-infra01-server/install.yml @@ -4,9 +4,6 @@ gather_facts: true become: true - collections: - - somaz94.ansible_k8s_iac_tool - tasks: # - Update all packages # - Install ansible-core @@ -26,18 +23,21 @@ state: present become: true - - name: Download terraform - ansible.builtin.get_url: - url: https://releases.hashicorp.com/terraform/terraform_{{ terraform_version }}/terraform_{{ terraform_version }}_{{ ansible_facts.ansible_os_family }}_{{ ansible_facts.ansible_architecture }}.zip - dest: /tmp/terraform.zip - mode: 0755 + - name: Register status of /usr/local/bin/terraform + ansible.builtin.stat: + path: /usr/local/bin/terraform + register: terraform_path - - name: Install terraform - ansible.builtin.package: - name: terraform - state: present - become: true + - name: Download and installt erraform version + when: not terraform_path.stat.exists + block: + - name: Download and install terraform + ansible.builtin.unarchive: + src: "https://releases.hashicorp.com/terraform/{{ terraform_version }}/terraform_{{ terraform_version }}_linux_amd64.zip" + dest: "/usr/local/bin/" + remote_src: true + mode: '0755' + exclude: "LICENSE.txt" roles: - - install_kustomize - + - somaz94.ansible_k8s_iac_tool.install_kustomize diff --git a/dns-entry.yml b/dns-entry.yml index 3377683..3d5860c 100644 --- a/dns-entry.yml +++ b/dns-entry.yml @@ -1,40 +1,46 @@ --- # file: dns-entry.yml -- name: Master playbook for home lab - hosts: all - gather_facts: false - tasks: - - name: Create DNS entry for {{ inventory_hostname }} - when: ip_address is defined - delegate_to: "{{ dns_server }}" - block: - - name: Check if terraform directory exists - ansible.builtin.stat: - path: ~/homelab/terraform/dns - register: terraform_dir +- name: Check if terraform directory exists + ansible.builtin.stat: + path: homelab/terraform/dns + register: terraform_dir - - name: Create Terraform directory for DNS entries - ansible.builtin.file: - path: ~/homelab/terraform/dns - state: directory - mode: '0755' - when: not terraform_dir.stat.exists +- name: Create Terraform directory for DNS entries + ansible.builtin.file: + path: homelab/terraform/dns + state: directory + mode: '0755' + when: not terraform_dir.stat.exists - - name: Create Terraform file for host - ansible.builtin.template: - src: templates/dns.tf.j2 - dest: ~/homelab/terraform/dns/{{ inventory_hostname }}.tf - mode: '0644' +- name: "Create/Update Terraform DNS provider" + ansible.builtin.template: + src: templates/dns_provider.tf.j2 + dest: homelab/terraform/dns/provider.tf + mode: '0644' - handlers: - - name: Update DNS entries via Terraform - delegate_to: "{{ dns_server }}" - community.general.terraform: - project_path: ~/homelab/terraform/dns/ - variables: - freeipa_server: "{{ dns_server }}" - freeipa_admin: "{{ dns_admin }}" - freeipa_password: "{{ vault_freeipa_password }}" - state: present - force_init: true +- name: Show all the hosts in the inventory + when: "{{ hostvars[groups['all'][ {{ item }} ]['ip_address'] }}" is defined + ansible.builtin.debug: + msg: "{{ hostvars[item].ip_address }}" + loop: "{{ query('inventory_hostnames', 'all') }}" + +# - name: Loop through all hosts +# block: +# - name: Create Terraform files for all eligible hosts +# when: "{{hostvars["{{ item }}"].ip_address}}" is defined +# ansible.builtin.template: +# src: templates/dns.tf.j2 +# dest: homelab/terraform/dns/{{ item }}.tf +# mode: '0644' +# loop: "{{ query('inventory_hostnames', 'all') }}" + +# - name: Update DNS entries via Terraform +# community.general.terraform: +# project_path: homelab/terraform/dns/ +# variables: +# freeipa_server: "{{ dns_server }}" +# freeipa_admin: "{{ dns_admin }}" +# freeipa_password: "{{ vault_freeipa_password }}" +# state: present +# force_init: true diff --git a/homelab/terraform/dns/ocp-hub.tf b/homelab/terraform/dns/ocp-hub.tf new file mode 100644 index 0000000..5d21a91 --- /dev/null +++ b/homelab/terraform/dns/ocp-hub.tf @@ -0,0 +1,11 @@ +// Terraform DNS entry template + +resource "freeipa_dns_record" "ocp-hub" { + zone_name = "int.mk-labs.cloud." + name = "ocp-hub" + type = "A" + records = [ + "10.1.71.10", + ] + ttl = 60 +} diff --git a/homelab/terraform/dns/pve01.tf b/homelab/terraform/dns/pve01.tf new file mode 100644 index 0000000..1b76744 --- /dev/null +++ b/homelab/terraform/dns/pve01.tf @@ -0,0 +1,11 @@ +// Terraform DNS entry template + +resource "freeipa_dns_record" "pve01" { + zone_name = "int.mk-labs.cloud." + name = "pve01" + type = "A" + records = [ + "10.1.71.51", + ] + ttl = 60 +} diff --git a/host_vars/infra01/vars b/host_vars/infra01/vars new file mode 100644 index 0000000..be95983 --- /dev/null +++ b/host_vars/infra01/vars @@ -0,0 +1,11 @@ +--- +# file: host_vars/infra01/vars + +# Networking +# primary_interface: "enp1s0f0" +# mac_address: "98:b7:85:1e:c6:f1" +#ip_address: 10.1.71.51 +hostname: "infra01.{{ base_domain }}" + +# Software +terraform_version: "1.11.3" diff --git a/site.yml b/site.yml index 8d63f8b..a134ac5 100644 --- a/site.yml +++ b/site.yml @@ -3,12 +3,13 @@ - name: Master playbook for home lab hosts: all - gather_facts: true - connection: local - # roles: - # - dns-entry + gather_facts: false -# tasks: - -- name: Process DNS entries - ansible.builtin.import_playbook: dns-entry.yml + tasks: + - name: Process DNS entries + when: inventory_hostname == "infra01" + ansible.builtin.import_tasks: + file: dns-entry.yml + tags: + - update-dns + - never diff --git a/terraform/dns/provider.tf b/templates/dns_provider.tf.j2 similarity index 89% rename from terraform/dns/provider.tf rename to templates/dns_provider.tf.j2 index 13f47d0..e137942 100644 --- a/terraform/dns/provider.tf +++ b/templates/dns_provider.tf.j2 @@ -1,7 +1,7 @@ # Configure the DNS Provider provider "freeipa" { - host = "infra01.int.mk-labs.cloud" - username = "admin" + host = var.freeipa_server + username = var.freeipa_admin password = var.freeipa_password insecure = true } diff --git a/test.yml b/test.yml index 7c3f184..2dfe616 100644 --- a/test.yml +++ b/test.yml @@ -1,22 +1,26 @@ --- - name: Run all tasks necessary to configure matchbox to install the cluster - hosts: matchbox - connection: local + hosts: infra01 + #connection: local gather_facts: true tasks: - - name: Print vars for debugging - ansible.builtin.debug: - msg: System {{ hostvars['infra01'].matchbox_assets }} / {{ hostvars['ocp-hub'].cluster_name }} + # - name: Print vars for debugging + # ansible.builtin.debug: + # msg: System {{ hostvars['infra01'].matchbox_assets }} / {{ hostvars['ocp-hub'].cluster_name }} - - name: Print vars for debugging 2 - ansible.builtin.debug: - msg: System {{ groups }} + # - name: Print vars for debugging 2 + # ansible.builtin.debug: + # msg: System {{ groups }} - - name: Print vars for debugging 3 - ansible.builtin.debug: - msg: System {{ groups['hub-cluster'][0] }} + # - name: Print vars for debugging 3 + # ansible.builtin.debug: + # msg: System {{ groups['hub-cluster'][0] }} - - name: Print vars for debugging 4 + # - name: Print vars for debugging 4 + # ansible.builtin.debug: + # msg: System {{ hostvars[groups['hub-cluster'][0]].cluster_name }} + + - name: Print all available facts ansible.builtin.debug: - msg: System {{ hostvars[groups['hub-cluster'][0]].cluster_name }} + var: ansible_facts \ No newline at end of file From b8609db119960448ae71726fa6a4e2b89bd7f231 Mon Sep 17 00:00:00 2001 From: rblundon Date: Thu, 27 Mar 2025 17:37:01 -0500 Subject: [PATCH 4/4] working on host loop --- README.md | 2 ++ dns-entry.yml | 18 +++++++++++++----- homelab/terraform/dns/ocp-hub.tf | 11 ----------- homelab/terraform/dns/pve01.tf | 11 ----------- print_ips.yml | 14 ++++++++++++++ test.yml | 16 +++++++++++----- 6 files changed, 40 insertions(+), 32 deletions(-) delete mode 100644 homelab/terraform/dns/ocp-hub.tf delete mode 100644 homelab/terraform/dns/pve01.tf create mode 100644 print_ips.yml diff --git a/README.md b/README.md index 42c5e17..79741cb 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,8 @@ This implementation is built on easily accessible consumer based hardware and wi ## Prerequisites +- Ansible user created +- Ansible configured - [Networking](docs/networks.md) - [Proxmox](docs/proxmox.md) (In my homelab, internal DNS, identity manangement, and ipxe are hosted here.) - Matchbox diff --git a/dns-entry.yml b/dns-entry.yml index 3d5860c..05fe52c 100644 --- a/dns-entry.yml +++ b/dns-entry.yml @@ -19,11 +19,19 @@ dest: homelab/terraform/dns/provider.tf mode: '0644' -- name: Show all the hosts in the inventory - when: "{{ hostvars[groups['all'][ {{ item }} ]['ip_address'] }}" is defined - ansible.builtin.debug: - msg: "{{ hostvars[item].ip_address }}" - loop: "{{ query('inventory_hostnames', 'all') }}" +- name: Loop through all hosts + vars: + my_hosts: "{{ query('inventory_hostnames', 'all') }}" + # ansible.builtin.debug: + # msg: "Host {{ item }} has IP address: {{ hostvars[my_hosts[my_idx]].ip_address | default('No IP address defined') }}" + ansible.builtin.template: + src: templates/dns.tf.j2 + dest: homelab/terraform/dns/{{ item }}.tf + mode: '0644' + when: hostvars[my_hosts[my_idx]].ip_address is defined + loop: "{{ my_hosts }}" + loop_control: + index_var: my_idx # - name: Loop through all hosts # block: diff --git a/homelab/terraform/dns/ocp-hub.tf b/homelab/terraform/dns/ocp-hub.tf deleted file mode 100644 index 5d21a91..0000000 --- a/homelab/terraform/dns/ocp-hub.tf +++ /dev/null @@ -1,11 +0,0 @@ -// Terraform DNS entry template - -resource "freeipa_dns_record" "ocp-hub" { - zone_name = "int.mk-labs.cloud." - name = "ocp-hub" - type = "A" - records = [ - "10.1.71.10", - ] - ttl = 60 -} diff --git a/homelab/terraform/dns/pve01.tf b/homelab/terraform/dns/pve01.tf deleted file mode 100644 index 1b76744..0000000 --- a/homelab/terraform/dns/pve01.tf +++ /dev/null @@ -1,11 +0,0 @@ -// Terraform DNS entry template - -resource "freeipa_dns_record" "pve01" { - zone_name = "int.mk-labs.cloud." - name = "pve01" - type = "A" - records = [ - "10.1.71.51", - ] - ttl = 60 -} diff --git a/print_ips.yml b/print_ips.yml new file mode 100644 index 0000000..1f3921a --- /dev/null +++ b/print_ips.yml @@ -0,0 +1,14 @@ +--- +- name: Print host IP addresses + hosts: infra01 + gather_facts: false + vars: + my_hosts: "{{ query('inventory_hostnames', 'all') }}" + tasks: + - name: Loop through all hosts + ansible.builtin.debug: + msg: "Host {{ item }} has IP address: {{ hostvars[my_hosts[my_idx]].ip_address | default('No IP address defined') }}" + when: hostvars[my_hosts[my_idx]].ip_address is defined + loop: "{{ my_hosts }}" + loop_control: + index_var: my_idx diff --git a/test.yml b/test.yml index 2dfe616..da29d64 100644 --- a/test.yml +++ b/test.yml @@ -1,8 +1,8 @@ --- - name: Run all tasks necessary to configure matchbox to install the cluster - hosts: infra01 + hosts: all #connection: local - gather_facts: true + gather_facts: false tasks: # - name: Print vars for debugging @@ -21,6 +21,12 @@ # ansible.builtin.debug: # msg: System {{ hostvars[groups['hub-cluster'][0]].cluster_name }} - - name: Print all available facts - ansible.builtin.debug: - var: ansible_facts \ No newline at end of file + # - name: Print all available facts + # ansible.builtin.debug: + # var: ansible_facts + + + - name: Print host IP address if defined + debug: + msg: "Host {{ inventory_hostname }} has IP address: {{ hostvars[inventory_hostname].ip_address | default('Not defined') }}" + when: hostvars[inventory_hostname].ip_address is defined \ No newline at end of file