# Setting a variable just for a task

**URL:** <https://forum.ansible.com/t/setting-a-variable-just-for-a-task/17685>\
**Category:** Ansible Project\
**Created:** [March 5, 2015, 9:25pm UTC](https://forum.ansible.com/t/setting-a-variable-just-for-a-task/17685 "2015-03-05T21:25:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jim\_McMahon](https://avatars.discourse-cdn.com/v4/letter/j/e8c25b/32.png) [@Jim\_McMahon](https://forum.ansible.com/u/Jim_McMahon)\
**Post date:** [March 5, 2015, 9:25pm UTC](https://forum.ansible.com/t/setting-a-variable-just-for-a-task/17685/1 "2015-03-05T21:25:49Z")

</div>

Is there a way to set a variable just for a task? Say the task uses the template module and I want to set a variable just for that task that is not specific to the host I’m on.

---

<div class="post-metadata">

**Author:** ![Mark\_McCoy](https://avatars.discourse-cdn.com/v4/letter/m/ebca7d/32.png) [@Mark\_McCoy](https://forum.ansible.com/u/Mark_McCoy)\
**Post date:** [March 5, 2015, 10:47pm UTC](https://forum.ansible.com/t/setting-a-variable-just-for-a-task/17685/2 "2015-03-05T22:47:12Z")

</div>

You should be able to put this variable in the rolename/defaults/main.yml file. This way there is a default value and if you do need to override it for a particular host or group you can do that easily.

---

<div class="post-metadata">

**Author:** ![Dag\_Wieers](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@Dag\_Wieers](https://forum.ansible.com/u/Dag_Wieers)\
**Post date:** [March 5, 2015, 10:57pm UTC](https://forum.ansible.com/t/setting-a-variable-just-for-a-task/17685/3 "2015-03-05T22:57:18Z")

</div>

You can define a variable (conditionally) inside the template, or use set\_fact in the previous task, or include\_vars in a previous task.

It depends on what you would like to achieve, and why.

---

<div class="post-metadata">

**Author:** ![Jim\_McMahon](https://avatars.discourse-cdn.com/v4/letter/j/e8c25b/32.png) [@Jim\_McMahon](https://forum.ansible.com/u/Jim_McMahon)\
**Post date:** [March 9, 2015, 1:46am UTC](https://forum.ansible.com/t/setting-a-variable-just-for-a-task/17685/4 "2015-03-09T01:46:19Z")

</div>

I thought about using the set\_fact in the previous task. It just seems more natural to set it in the same task, but that’s not possible.

Thanks,

Jim
