Skip to content

Automatically pull web IP

---
- name: Get IP Address
  hosts: web_web
  gather_facts: yes

  tasks:
    - name: Get private IP from web_web
      command: hostname -I
      register: WebPrivateIP

    - name: Filter IP
      set_fact:
        filtered_ip: "{{ WebPrivateIP.stdout.split() | ipaddr('10.10.1.0/24') | first }}"
    - name: Print IP
      debug:
        msg: "Web private IP in 10.10.1.0/24: {{ filtered_ip }}"
      delegate_to: "{{ item }}"
      delegate_facts: true
      loop: "{{ groups['web_lb'] }}"

- name: Update Caddyfile on lb server
  hosts: web_lb
  gather_facts: yes
  vars:
    app_domain: "app.lanlab.xyz"
    app_port: 80  # Replace with the actual port
    health_path: "/"  # Replace with the actual health path

  tasks:
    - name: Get filtered IPs from web_web
      set_fact:
        web_web_filtered_ips: "{{ groups['web_web'] | map('extract', hostvars, 'filtered_ip') | list }}"

    - name: Configure Caddyfile
      template:
        src: caddyfile.j2
        dest: /etc/caddy/Caddyfile
#      notify: Reload Caddy Service
      become: yes

    - name: Reload Caddy Service
      systemd:
        name: caddy
        state: reloaded
      become: yes

This playbook demonstrates two essential tasks in managing a web server environment:

  • Retrieving the private IP address of web servers within the web_web group.
  • Updating the Caddyfile on load balancers with filtered IP addresses for efficient routing. Playbook Breakdown:

  • Get IP Address and Filter Play Name: Get IP Address

Hosts: web_web

Gather Facts: Yes

Tasks:

Get private IP:

- name: Get private IP from web_web
  command: hostname -I
  register: WebPrivateIP
Filter IP:

- name: Filter IP
  set_fact:
    filtered_ip: "{{ WebPrivateIP.stdout.split() | ipaddr('10.10.1.0/24') | first }}"
Print IP:
- name: Print IP
  debug:
    msg: "Web private IP in 10.10.1.0/24: {{ filtered_ip }}"
  delegate_to: "{{ item }}"
  delegate_facts: true
  loop: "{{ groups['web_lb'] }}"
2. Update Caddyfile on Load Balancers Play Name: Update Caddyfile on lb server

Hosts: web_lb Gather Facts: Yes

Variables:

app_domain: "app.lanlab.xyz"  # Replace with your actual domain name
app_port: 80                   # Replace with your actual application port
health_path: "/"               # Replace with your actual health check path

Tasks:

Get filtered IPs:

- name: Get filtered IPs from web_web
  set_fact:
    web_web_filtered_ips: "{{ groups['web_web'] | map('extract', hostvars, 'filtered_ip') | list }}"

Configure Caddyfile:

- name: Configure Caddyfile
  template:
    src: caddyfile.j2
    dest: /etc/caddy/Caddyfile
    become: yes
Reload Caddy Service:
- name: Reload Caddy Service
  systemd:
    name: caddy
    state: reloaded
    become: yes

Additional Notes:

Replace placeholders like app_domain, app_port, and health_path with your specific values. Ensure the template file caddyfile.j2 exists with the appropriate Caddyfile configuration and template variables. Key Points:

Use gather_facts: yes in both plays to ensure consistent data access within the playbook. Leverage set_fact to store intermediate results for reusability. The delegate_to and delegate_facts options in the Print IP task allow the task to run on each load balancer (web_lb refers to a group of load balancers).