Answers: <p>Specifying which node you want to reboot is done using a <a href="https://docs.ansible.com/ansible/latest/inventory_guide/intro_patterns.html" rel="nofollow noreferrer">targetting pattern</a>. This can be a single node or a group of nodes defined in the inventory. In your case, you are targetting only <code>node1</code>.</p>
  <p>You need to execute the Ansible playbook from somewhere. It's not quite clear from your question whether the Slurm head node is also the Ansible controller (the machine which is executing <code>ansible-playbook</code>.</p>
  <p>Let's assume that is the case - that the Ansible controller is also the Slurm head node. As such, your playbook as it currently stands will work.
  Assuming that the head node / Ansible controller has access to the inventory, it targets a single node and reboots it.</p>
  <p>Now, let's consider this case:</p>
  <blockquote>
  <p>... when the playbook is executed outside the head-node...</p>
  </blockquote>
  <p>In this case, the Ansible controller is no longer the head node, but some other machine. This could be <em>any</em> machine, even a machine outside of the Slurm cluster. It could also be one of the worker nodes itself. The only constraints are:</p>
  <ol>
  <li>It must have an environment that can run <code>ansible-playbook</code></li>
  <li>It must have access to the inventory</li>
  <li>It must have permission to connect to the targetted host, and the user connecting must have <code>sudo</code> rights (since you use <code>become</code>).</li>
  <li>And of course, you need some way of triggering it...</li>
  </ol>
  <p>If these conditions are satisfied, you can use the exact same playbook.</p>
  <p>However, since you're only executing a single task, I would suggest not running a playbook at all, but an <a href="https://docs.ansible.com/ansible/latest/command_guide/intro_adhoc.html" rel="nofollow noreferrer">ad-hoc task</a>. The documentation even has <a href="https://docs.ansible.com/ansible/latest/command_guide/intro_adhoc.html#rebooting-servers" rel="nofollow noreferrer">an example for your exact use case</a>:</p>
  <pre class="lang-bash prettyprint-override"><code>$ ansible node1 -u team -b -a &quot;/sbin/reboot&quot;
  </code></pre>
  <p>If you want to use the <code>reboot</code> module:</p>
  <pre class="lang-bash prettyprint-override"><code>ansible node1 -u team -b -m reboot -a &quot;reboot_timeout=300&quot;
  </code></pre>
  <p>If your inventory is in a file, you can pass it with the <code>-i</code> flag.</p>
  
Answers: CC BY-SA 4.0
Answers: 2023-10-23T22:18:15.377Z
Answers: Thank you very much targeting patterns are the answer. I apologise for the question not being clear. Just to clarify, I'm outside the head-node (one ip address) but rebooting a node within the cluster under the head node (second ip address). Is this therefore achieved via `ansible node1 -u team -m reboot -i inventory` where `node1` = name of worker node (targeting pattern), `-i inventory` contains the ip address of the headnode, `-u` = headnode login/full ssh address? Is targeting pattern within the inventory or (if absent) target the node name?
Answers: Terra
Answers: CC BY-SA 4.0
Answers: 2023-10-23T07:31:16.183Z
Answers: 2023-10-23T07:31:16.183Z
Answers: Bruce Becker
Body: <p>I looking to execute a simple reboot via Ansible using its playbook</p>
  <pre><code>- name: Worker-node Reboot
    hosts: node1
    gather_facts: no
    remote_user: team
    become: true
   
    tasks:
      - name: Reboot the machine (Wait for 5 min)
        reboot:
          reboot_timeout: 300
  </code></pre>
  <p>How do I specify reboot for a worker node in Ansible's inventory for a Slurm queue when the playbook is executed outside the head-node?</p>
  
Tags: <ansible><deployment><ansible-inventory>
Title: Ansible inventory for restart on worker node
UserDisplayName: Terra
ThreadText: # Ansible inventory for restart on worker node
  
  (Asked by: username_0 on 2023-10-22)
  
  I looking to execute a simple reboot via Ansible using its playbook
  
  ```
  - name: Worker-node Reboot
    hosts: node1
    gather_facts: no
    remote_user: team
    become: true
   
    tasks:
      - name: Reboot the machine (Wait for 5 min)
        reboot:
          reboot_timeout: 300
  
  ```
  
  How do I specify reboot for a worker node in Ansible's inventory for a Slurm queue when the playbook is executed outside the head-node?
  
  **Tags:** ansible, deployment, ansible-inventory
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-10-23. Score: 0 | Accepted answer
  
  Specifying which node you want to reboot is done using a [targetting pattern](https://docs.ansible.com/ansible/latest/inventory_guide/intro_patterns.html). This can be a single node or a group of nodes defined in the inventory. In your case, you are targetting only `node1`.
  
  You need to execute the Ansible playbook from somewhere. It's not quite clear from your question whether the Slurm head node is also the Ansible controller (the machine which is executing `ansible-playbook`.
  
  Let's assume that is the case - that the Ansible controller is also the Slurm head node. As such, your playbook as it currently stands will work.
  Assuming that the head node / Ansible controller has access to the inventory, it targets a single node and reboots it.
  
  Now, let's consider this case:
  
  > ... when the playbook is executed outside the head-node...
  
  In this case, the Ansible controller is no longer the head node, but some other machine. This could be *any* machine, even a machine outside of the Slurm cluster. It could also be one of the worker nodes itself. The only constraints are:
  
  1. It must have an environment that can run `ansible-playbook`
  2. It must have access to the inventory
  3. It must have permission to connect to the targetted host, and the user connecting must have `sudo` rights (since you use `become`).
  4. And of course, you need some way of triggering it...
  
  If these conditions are satisfied, you can use the exact same playbook.
  
  However, since you're only executing a single task, I would suggest not running a playbook at all, but an [ad-hoc task](https://docs.ansible.com/ansible/latest/command_guide/intro_adhoc.html). The documentation even has [an example for your exact use case](https://docs.ansible.com/ansible/latest/command_guide/intro_adhoc.html#rebooting-servers):
  
  ```bash
  $ ansible node1 -u team -b -a "/sbin/reboot"
  
  ```
  
  If you want to use the `reboot` module:
  
  ```bash
  ansible node1 -u team -b -m reboot -a "reboot_timeout=300"
  
  ```
  
  If your inventory is in a file, you can pass it with the `-i` flag.
  
  > **Comments:**
  > **username_0**: Thank you very much targeting patterns are the answer. I apologise for the question not being clear. Just to clarify, I'm outside the head-node (one ip address) but rebooting a node within the cluster under the head node (second ip address). Is this therefore achieved via `ansible node1 -u team -m reboot -i inventory` where `node1` = name of worker node (targeting pattern), `-i inventory` contains the ip address of the headnode, `-u` = headnode login/full ssh address? Is targeting pattern within the inventory or (if absent) target the node name?  
  
  ---
  
  