Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ansible ignore the run_once configuration on task

I am using Ansible and I want to run a task only once. I follow the documentation about how to configure and run a task only once

- name: apt update
  shell: apt-get update
  run_once: true

But when I run Ansible, it always runs this task. How can I run my task only once.

like image 738
Robert Avatar asked Oct 16 '14 16:10

Robert


People also ask

How do I ignore tasks in Ansible?

Ignoring failed commands By default Ansible stops executing tasks on a host when a task fails on that host. You can use ignore_errors to continue on in spite of the failure. The ignore_errors directive only works when the task is able to run and returns a value of 'failed'.

What is Ansible Run_once?

Ansible run_once parameter is used with a task, which you want to run once on first host. When used, this forces the Ansible controller to attempt execution on first host in the current hosts batch, then the result can be applied to the other remaining hosts in current batch.

How do you stop playbook in Ansible?

The default behavior is to pause with a prompt. You can use ctrl+c if you wish to advance a pause earlier than it is set to expire or if you need to abort a playbook run entirely. To continue early: press ctrl+c and then c . To abort a playbook: press ctrl+c and then a .


1 Answers

The run_once option will run every time your Playbook/tasks runs, but will only run it once during the specific run itself. So every time you run the play, it will run, but only on the first host in the list. If you're looking for a way to only run that command once, period, you'll need to use the creates argument. Using your example, this can be achieved by using the following -

- name: apt update
  shell: apt-get update && touch /root/.aptupdated
  args:
    creates: /root/.aptupdated

In this case the file /root/.aptupdated is created. The task will now check to see if that exists, and if it does it will not run.

On a related note if the task you are trying to run is the apt-get update, you may want to use the native apt module. You can then do something like this -

- name: apt update
  apt: update_cache=yes cache_valid_time=86400

Now this will only run if the cache is older than one day.

like image 80
Charlie O. Avatar answered Oct 11 '22 20:10

Charlie O.