ansible 忽略任务上的 run_once 配置 [英] ansible ignore the run_once configuration on task

查看:23
本文介绍了ansible 忽略任务上的 run_once 配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Ansible,并且我只想运行一次任务.我遵循有关如何配置和仅运行一次任务

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

但是当我运行 Ansible 时,它​​总是运行这个任务.我怎样才能只运行我的任务一次.

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

推荐答案

run_once 选项将在您的 Playbook/tasks 每次运行时运行,但只会在特定运行期间运行一次.因此,每次运行该剧时,它都会运行,但仅在列表中的第一个主机上运行.如果您正在寻找一种仅运行该命令一次的方法,则需要使用 created 参数.使用您的示例,这可以通过使用以下内容来实现 -

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

在这种情况下,会创建文件/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.

在相关说明中,如果您尝试运行的任务是 apt-get 更新,您可能需要使用本机 apt 模块.然后你可以做这样的事情 -

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.

这篇关于ansible 忽略任务上的 run_once 配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆