ansible - 如何在 Ansible 中制作幂等 shell

标签 ansible

我正在使用 Ansible,在尝试使 shell 执行幂等时遇到一些问题。我要做的第一件事是安装 python-apt 包 因为我需要它来使用 apt 模块来安装其他包。但每次我运行我的 playbook 时,shell 任务总是会运行,我想让它幂等。这是我的 shell 任务:

- name: install pyton-apt
  shell: apt-get install -y python-apt

这是始终运行上述任务的输出:

$ ansible-playbook -i hosts site.yml 

PLAY [docker] ***************************************************************** 

GATHERING FACTS *************************************************************** 
ok: [10.0.3.240]

TASK: [docker | install pyton-apt] ******************************************** 
changed: [10.0.3.240]

TASK: [docker | install unzip] ************************************************ 
ok: [10.0.3.240]

PLAY RECAP ******************************************************************** 
10.0.3.240                 : ok=3    changed=1    unreachable=0    failed=0 

最佳答案

您应该使用 ansible apt 模块来安装 python-apt,它将是开箱即用的幂等:http://docs.ansible.com/apt_module.html

例如

- name: install python-apt
  apt: name=python-apt state=present

(请注意,使用 apt 模块应自动在远程主机上安装 python-apt,因此我不确定为什么您需要手动安装它,请参阅 https://github.com/ansible/ansible/issues/4079 )

编辑:如果由于某种原因您无法使用内置的apt模块来安装python apt,shell模块提供creates参数以帮助使其幂等。

- name: install python-apt
  shell: apt-get install -y python-apt >> /home/x/output.log creates=/home/x/output.log

这意味着如果 /home/x/output.log 已经存在,shell 模块将不会运行。

关于ansible - 如何在 Ansible 中制作幂等 shell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24576315/

相关文章:

Ansible - 如何只保留 3 个版本

ansible - 使用ansible创建多个目录

ansible - 强制在所有主机上进行事实收集

ansible - 无法在 Ansible 中检索保管库 secret

安塞 bool /金贾 : Conditional port publishing in Docker/Podman

ansible - 如何在执行结束时总结 Ansible Tower 中失败的断言?

ansible - 使用 molecule 和 gilt 测试依赖的 Ansible 角色,零文档

json - 如何从 ansible 中的模板编写漂亮的 JSON 文件?

python - 使用 jinja2 计算集合差异(在 ansible 中)

ansible - 删除目录中包含特定名称的所有文件