# run task  only if changed

**URL:** <https://forum.ansible.com/t/run-task-only-if-changed/27720>\
**Category:** Ansible Project\
**Tags:** rhel\
**Created:** [June 6, 2018, 2:24pm UTC](https://forum.ansible.com/t/run-task-only-if-changed/27720 "2018-06-06T14:24:45Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cava\_cavamagie](https://avatars.discourse-cdn.com/v4/letter/c/e68b1a/32.png) [@cava\_cavamagie](https://forum.ansible.com/u/cava_cavamagie)\
**Post date:** [June 6, 2018, 2:24pm UTC](https://forum.ansible.com/t/run-task-only-if-changed/27720/1 "2018-06-06T14:24:45Z")

</div>

i have to run a playbook that can set the repository rh for a lot o machine

- name: RH Enable repositories  
rhsm\_repository:  
name: ‘{{ item.name }}’  
state: ‘{{ item.state }}’  
with\_items: ‘{{ IBM\_l6\_R\_RH7\_rhsm\_repository }}’  
when: ansible\_distribution\_major\_version == “7”  
vars:  
IBM\_l6\_R\_RH7\_rhsm\_repository: \>  
{{ IBM\_l6\_R\_RH7\_rhsm\_repository\_default +  
IBM\_l6\_R\_RH7\_rhsm\_repository\_host +  
IBM\_l6\_R\_RH7\_rhsm\_repository\_group }}  
register: result

this i i the main.yml in the default

IBM\_l6\_R\_RH7\_rhsm\_repository\_default:

- name: rhel-7-server-extras-rpms  
state: enabled
- name: rhel-7-server-optional-rpms  
state: enabled
- name: rhel-7-server-rpms  
state: enabled
- name: rhel-7-server-supplementary-rpms  
state: enabled
- name: rhel-7-server-thirdparty-oracle-java-rpms  
state: enabled  
IBM\_l6\_R\_RH7\_rhsm\_repository\_host:   
IBM\_l6\_R\_RH7\_rhsm\_repository\_group: 

all works but take a very long time to verify

how i can make the task if i want to run the task if only the variabiles is changed? with fact? this task is a part of a playbook who run several time in a day  
someone can help me??

---

<div class="post-metadata">

**Author:** ![Tchia04](https://avatars.discourse-cdn.com/v4/letter/t/fbc32d/32.png) [@Tchia04](https://forum.ansible.com/u/Tchia04)\
**Post date:** [June 7, 2018, 5:44pm UTC](https://forum.ansible.com/t/run-task-only-if-changed/27720/2 "2018-06-07T17:44:17Z")

</div>

In general, you can add “when: results.changed” to a task to run only if the previous task has changed.

You can use “changed\_when:” to customize the output if the default output from task is not what you want

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [June 7, 2018, 5:49pm UTC](https://forum.ansible.com/t/run-task-only-if-changed/27720/3 "2018-06-07T17:49:43Z")

</div>

There’s a lot of stuff in the AWS modules that “changes” for no apparent reason, and where no actual change has occurred. I have given up relying on it, and explicitly test items I think may have changed.

It would be really nice if all the AWS modules could accurately tell whether something had actually changed. ec2\_group is especially bad.

Regards, K.

---

<div class="post-metadata">

**Author:** ![cava\_cavamagie](https://avatars.discourse-cdn.com/v4/letter/c/e68b1a/32.png) [@cava\_cavamagie](https://forum.ansible.com/u/cava_cavamagie)\
**Post date:** [June 8, 2018, 1:45pm UTC](https://forum.ansible.com/t/run-task-only-if-changed/27720/4 "2018-06-08T13:45:40Z")

</div>

because the main idea is to write the result in a fact  
but the problem is that i want to maintain the fact between varios run

example i set the first time a value example an ntp  
and set a fact ntp\_set: true  
and every run if i read the ntp\_set = ture it escape the task

but i lost the value of the fact between the run

---

<div class="post-metadata">

**Author:** ![Tchia04](https://avatars.discourse-cdn.com/v4/letter/t/fbc32d/32.png) [@Tchia04](https://forum.ansible.com/u/Tchia04)\
**Post date:** [June 8, 2018, 8:28pm UTC](https://forum.ansible.com/t/run-task-only-if-changed/27720/5 "2018-06-08T20:28:20Z")

</div>

Maybe you can try using the creates parameters in the shell module ([https://docs.ansible.com/ansible/latest/modules/shell\_module.html](https://docs.ansible.com/ansible/latest/modules/shell_module.html))

creates - a filename, when it already exists, this step will not be run.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [June 9, 2018, 6:26am UTC](https://forum.ansible.com/t/run-task-only-if-changed/27720/6 "2018-06-09T06:26:48Z")

</div>

Sounds like you are looking for local facts  
[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_variables.html#local-facts-facts-d](https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#local-facts-facts-d)

---

<div class="post-metadata">

**Author:** ![cava\_cavamagie](https://avatars.discourse-cdn.com/v4/letter/c/e68b1a/32.png) [@cava\_cavamagie](https://forum.ansible.com/u/cava_cavamagie)\
**Post date:** [July 6, 2018, 2:46pm UTC](https://forum.ansible.com/t/run-task-only-if-changed/27720/7 "2018-07-06T14:46:30Z")

</div>

thank you  
usin a file stat?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 6, 2018, 7:34pm UTC](https://forum.ansible.com/t/run-task-only-if-changed/27720/8 "2018-07-06T19:34:12Z")

</div>

I don't understand you question.

You seed that you needed to store fact between runs, so if that means between different or the same playbook run the most logic way to do that is with custom facts.  
It you read the link you see that custom facts is a json, ini or a executable returning json that set the variables/facts you need.

---

<div class="post-metadata">

**Author:** ![cava\_cavamagie](https://avatars.discourse-cdn.com/v4/letter/c/e68b1a/32.png) [@cava\_cavamagie](https://forum.ansible.com/u/cava_cavamagie)\
**Post date:** [July 6, 2018, 7:53pm UTC](https://forum.ansible.com/t/run-task-only-if-changed/27720/9 "2018-07-06T19:53:28Z")

</div>

Yes Yes your solution is the corretto way to achieve my objective  
Thnak you
