run task only if changed

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??

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

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.

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

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

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

Sounds like you are looking for local facts
https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#local-facts-facts-d

thank you
usin a file stat?

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.

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