# set host variable within a playbook?

**URL:** <https://forum.ansible.com/t/set-host-variable-within-a-playbook/34235>\
**Category:** Ansible Project\
**Created:** [November 24, 2020, 8:09pm UTC](https://forum.ansible.com/t/set-host-variable-within-a-playbook/34235 "2020-11-24T20:09:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chad\_Campbell](https://avatars.discourse-cdn.com/v4/letter/c/c2a13f/32.png) [@Chad\_Campbell](https://forum.ansible.com/u/Chad_Campbell)\
**Post date:** [November 24, 2020, 8:09pm UTC](https://forum.ansible.com/t/set-host-variable-within-a-playbook/34235/1 "2020-11-24T20:09:51Z")

</div>

I’m trying to automate our monthly patch process for CentOS servers. We receive a list of servers from our Linux team each month that says which servers need to be patched and/or rebooted. I’m wanting to have needs\_patch and needs\_reboot variables attached to each host. So, each month we would have a playbook reset all of those to false, and then using the list from the Linux team, another playbook would set the necessary ones to true. Then the actual patch/reboot playbook would do the work based on the true variables and set the variables back to false upon success. I’m having trouble determining if this is possible, and if so, what the correct way is of doing this in the YAML. If anyone has suggestions or perhaps a better way of achieving the same result, that would be greatly appreciated.

Thanks!

Chad

---

<div class="post-metadata">

**Author:** ![Roberto\_J\_R\_Paz](https://avatars.discourse-cdn.com/v4/letter/r/d2c977/32.png) [@Roberto\_J\_R\_Paz](https://forum.ansible.com/u/Roberto_J_R_Paz)\
**Post date:** [November 24, 2020, 8:46pm UTC](https://forum.ansible.com/t/set-host-variable-within-a-playbook/34235/2 "2020-11-24T20:46:02Z")

</div>

You can provide custom variables to the inventory file:

[server]  
host1 needs\_patch=yes needs\_reboot=yes  
host2 needs\_patch=yes needs\_reboot=no  
…

and then use this variables in the task to do each task or skip it on every server.

[https://docs.ansible.com/ansible/latest/user\_guide/intro\_inventory.html](https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html)

---

<div class="post-metadata">

**Author:** ![Chad\_Campbell](https://avatars.discourse-cdn.com/v4/letter/c/c2a13f/32.png) [@Chad\_Campbell](https://forum.ansible.com/u/Chad_Campbell)\
**Post date:** [November 24, 2020, 8:56pm UTC](https://forum.ansible.com/t/set-host-variable-within-a-playbook/34235/3 "2020-11-24T20:56:40Z")

</div>

Update - here’s what I’ve tried so far. It runs successfully, but the facts don’t persist after the playbook is done. I want them to persist so that we can re-run the patch job as needed until all of them are successful without manually going and selecting hosts.

- hosts: “{{ env|default(‘all’) }}”  
gather\_facts: no  
become: no  
tasks:
- set\_fact:  
needs\_patch: False  
needs\_reboot: False  
cacheable: yes

---

<div class="post-metadata">

**Author:** ![Roberto\_J\_R\_Paz](https://avatars.discourse-cdn.com/v4/letter/r/d2c977/32.png) [@Roberto\_J\_R\_Paz](https://forum.ansible.com/u/Roberto_J_R_Paz)\
**Post date:** [November 24, 2020, 9:16pm UTC](https://forum.ansible.com/t/set-host-variable-within-a-playbook/34235/4 "2020-11-24T21:16:10Z")

</div>

I think it has to do with variable precedence.

Use a diferent variable name. set value as default value.  
Overwrite it with specific value for each host and use that one to decide if the task is going to be done or skipped.
