monk
monk

Reputation: 2115

How to reboot a dynamically calculated host in ansible

How to reboot a host using ansible, this host is not the remote host stored in the inventory file. The host IP is getting returned from a script called get_active_host.sh. I tried to reboot the active host from the script itself however , playbook execution failed even after using wait_for_connection . the host which should be rebooted is stored in {{ active_host_location }}

---
# tasks file for GET_ACTIVE_HOST
 - name: GET ACTIVE HOST LOCATION
   script: get_active_host.sh
   args:
     executable: bash
   register: active_host_location
   async: 0
   poll: 0
   become: true

 - name: Wait for server to restart
   local_action:
     module: wait_for
       host={{ active_host_location }}
       port=22
       delay=1
       timeout=300



[my current machine] --->[ansible_host]---get_active_host.sh-->[active_host]
   1.2.3.4                   1.2.3.5                             1.2.3.6

I need to reboot 1.2.3.6 which is dynamically calculated during the play from the script. Is it possible to achieve it and how ?

script output:

./get_active_host.sh
1.2.3.6

Upvotes: 0

Views: 687

Answers (2)

ilias-sp
ilias-sp

Reputation: 6685

The IP returned from the script is saved in the structure of active_host_location variable, and since the output is a single line you can access the IP by active_host_location.stdout.

Since the script returns IP and not hostname, i think you cant populate in advance all these possible results in your inventory, for ansible to be able to connect with provisioned user/pass and run the reboot tasks. So, i would try to do the restart task with a locally run ssh command, that connects via ssh to the target machine, and runs the reboot.

To connect to the machine, you can:

  1. exchange SSH heys
  2. install sshpass in your localhost.

if you follow 1, the shell task would look like:

  - name: Run restart command
    shell: "ssh {{ remote_user }}{{ active_host_location.stdout }} 'sudo reboot'"
    delegate_to: localhost
    register: reboot_result

  - name: print result
    debug:
      var: reboot_result

if you want to use sshpass approach, the command task could be:

  - name: Run restart command
    shell: "sshpass -p \"{{ remote_pass }}\" ssh {{ remote_user }}@{{ active_host_location.stdout }} 'sudo reboot'"
    delegate_to: localhost
    register: reboot_result

  - name: print result
    debug:
      var: reboot_result

these assume this user can "sudo without requiring to submit password".

hope these help

Upvotes: 1

Derrick Xu
Derrick Xu

Reputation: 61

If the variable you need is being outputted from the script already, you should be pretty close. When you register the output of a task, it dumps a whole bunch of stuff into a results object that you then need to dig into to grab the actual variable you need. Use the debug module on active_host_location to figure out the exact hierarchy, but what you want is probably something like, {{ active_host_location.results.stdout }}.

Upvotes: 1

Related Questions