ansibleipmitool

Ansible execute command locally and then on remote server


I am trying to start a server using ansible shell module with ipmitools and then do configuration change on that server once its up.

Server with ansible installed also has ipmitools.

On server with ansible i need to execute ipmitools to start target server and then execute playbooks on it.

Is there a way to execute local ipmi commands on server running ansible to start target server through ansible and then execute all playbooks over ssh on target server.


Solution

  • You can run any command locally by providing the delegate_to parameter.

    - shell: ipmitools ...
      delegate_to: localhost
    

    If ansible complains about connecting to localhost via ssh, you need to add an entry in your inventory like this:

    localhost              ansible_connection=local
    

    or in host_vars/localhost:

    ansible_connection: local
    

    See behavioral parameters.

    Next, you're going to need to wait until the server is booted and accessible though ssh. Here is an article from Ansible covering this topic and this is the task they have listed:

    - name: Wait for Server to Restart
      local_action:
        wait_for
        host={{ inventory_hostname }}
        port=22
        delay=15
        timeout=300
      sudo: false
    

    If that doesn't work (since it is an older article and I think I previously had issues with this solution) you can look into the answers of this SO question.