From b8609db119960448ae71726fa6a4e2b89bd7f231 Mon Sep 17 00:00:00 2001 From: rblundon Date: Thu, 27 Mar 2025 17:37:01 -0500 Subject: [PATCH] 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