2

I have a playbook, which replace the IP and hostname. The playbook works fine till it reboots the machine, but unable to connect it back as now the IP of that server is changed to something else.

Is there any better way to handle this problem.

---
- hosts: test
  remote_user: root
  vars:
    IP_TO_REPLACE: '192.168.1.15'
    IP_TO_REPLACE_WITH: '192.168.1.16'
    HOSTNAME_TO_REPLACE: 'devops-15'
    HOSTNAME_TO_REPLACE_WITH: 'devops-16'

  tasks:
  - name: modifying the ifcfg-BR0 file 
    replace:
      path: "{{ item.path }}"
      regexp: "{{ item.regexp1 }}"
      replace: "{{ item.replace }}"
      backup: yes
    with_items:
      - { path: '/etc/sysconfig/network/ifcfg-br0', regexp1: "{{ IP_TO_REPLACE }}", replace: "{{ IP_TO_REPLACE_WITH }}" }
      - { path: '/etc/hosts', regexp1: "{{ IP_TO_REPLACE }}", replace: "{{ IP_TO_REPLACE_WITH }}" }
      - { path: '/etc/hosts', regexp1: "{{ HOSTNAME_TO_REPLACE }}", replace: "{{ HOSTNAME_TO_REPLACE_WITH }}" }
      - { path: '/etc/hostname', regexp1: "{{ HOSTNAME_TO_REPLACE }}", replace: "{{ HOSTNAME_TO_REPLACE_WITH }}" }


    register: task_result

  - name: Reboot immediately the server to take latest changes
    shell: "sleep 15 && reboot"
    async: 1
    poll: 0
    when: task_result is changed

  - name: Wait for the reboot to complete if there was a changed
    wait_for:
      port: 22
      host: '{{ (ansible_ssh_host |default(ansible_host))|default(inventory_hostname) }}'
      search_regex: OpenSSH
      delay: 10
      timeout: 90
    when: task_result is changed

  - name: Check the uptime of the server
    shell: "uptime"
    register: uptime_result

  - debug: var=uptime_result

Actual Output

TASK [Reboot immediately the server to take latest changes] ***************************************************************************** fatal: [192.168.1.15]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Shared connection to 192.168.1.15 closed.\r\n", "unreachable": true}

Expected Output

It should connect to the new IP 192.168.1.16

Biks
  • 21
  • 4

1 Answers1

1

My research lead me to the following solution

Inspire by the suggestion of "drenthe73" here:

https://www.reddit.com/r/ansible/comments/n6xfyv/change_current_ip_address_and_reboot_into_the_new/

playbook with 2 plays

---
# play 1
hosts:
  - host_ip_initial

  # ...

  tasks:
    - name: "change ip address"

      # ...
    
    - name: "reboot target"
      ansible.builtin.shell: "reboot"
      async: 1
      poll: 0
    
# play2
gather_facts: no
hosts:
  - host_ip_final

  # ...

  tasks:
    - name: "Wait for the target to reboot before probing it is up and running"
      ansible.builtin.wait_for:
        timeout: 300
      delegate_to: localhost

    - name: "wait for boot up"
      ansible.builtin.wait_for_connection:
        connect_timeout: 5
        sleep: 5
        delay: 30
        timeout: 600

You need to know the final IP address and creates in your inventory the proper homolog entries.

Ansible various way to restart target:

https://www.redhat.com/sysadmin/automate-reboot-ansible

I am a bit late, but it might help someone else...

Richard
  • 721
  • 5
  • 16
  • Then I run in ssh known hosts issues, there is this SO here : https://stackoverflow.com/questions/30226113/ansible-ssh-prompt-known-hosts-issue, I will update the answer with proper solution, ASAP – Richard Feb 13 '22 at 17:24