---
- name: Fix all master references to ch4
  hosts: localhost
  gather_facts: no
  vars:
    files_to_fix:
      - "scripts/diagnose-consul-sync.sh"
      - "scripts/register-traefik-to-all-consul.sh"
      - "deployment/ansible/playbooks/update-nomad-consul-config.yml"
      - "deployment/ansible/templates/nomad-server.hcl.j2"
      - "deployment/ansible/templates/nomad-client.hcl"
      - "deployment/ansible/playbooks/fix-nomad-consul-roles.yml"
      - "deployment/ansible/onecloud1_nomad.hcl"
      - "ansible/templates/consul-client.hcl.j2"
      - "ansible/consul-client-deployment.yml"
      - "ansible/consul-client-simple.yml"
      
  tasks:
    - name: Replace master.tailnet-68f9.ts.net with ch4.tailnet-68f9.ts.net
      replace:
        path: "{{ item }}"
        regexp: 'master\.tailnet-68f9\.ts\.net'
        replace: 'ch4.tailnet-68f9.ts.net'
      loop: "{{ files_to_fix }}"
      when: item is file

    - name: Replace master hostname references
      replace:
        path: "{{ item }}"
        regexp: '\bmaster\b'
        replace: 'ch4'
      loop: "{{ files_to_fix }}"
      when: item is file

    - name: Replace master IP references in comments
      replace:
        path: "{{ item }}"
        regexp: '# master'
        replace: '# ch4'
      loop: "{{ files_to_fix }}"
      when: item is file

    - name: Fix inventory files
      replace:
        path: "{{ item }}"
        regexp: 'master ansible_host=master'
        replace: 'ch4 ansible_host=ch4'
      loop:
        - "deployment/ansible/inventories/production/inventory.ini"
        - "deployment/ansible/inventories/production/csol-consul-nodes.ini"
        - "deployment/ansible/inventories/production/nomad-clients.ini"
        - "deployment/ansible/inventories/production/master-ash3c.ini"
        - "deployment/ansible/inventories/production/consul-nodes.ini"
        - "deployment/ansible/inventories/production/vault.ini"

    - name: Fix IP address references (100.117.106.136 comments)
      replace:
        path: "{{ item }}"
        regexp: '100\.117\.106\.136.*# master'
        replace: '100.117.106.136  # ch4'
      loop: "{{ files_to_fix }}"
      when: item is file