如何在 Ansible 中制作幂等 shell [英] How do I make an idempontent shell in Ansible

查看:41
本文介绍了如何在 Ansible 中制作幂等 shell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using Ansible and I have a little problem trying to make idempotent a shell execution. The first I do is install python-apt package because I need it to use the apt module for install others packages. But every time that I run my playbook the shell task always runs and I want to make it idempotent. Here is my shell task:

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

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

And here is the output, always running the above task:

$ 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

You should use the ansible apt module to install python-apt, which will be idempotent out of the box: 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)

(Note using the apt module should automatically install python-apt on the remote host so I'm not sure why you would need to manually install it, see https://github.com/ansible/ansible/issues/4079)

如果由于某种原因你不能使用内置的 apt 模块来安装 python apt,shell 模块提供了 creates参数以帮助使其具有幂等性.

If for some reason you can't use the inbuilt apt module to install python apt, the shell module provides the creates parameter to help make it idempotent.

- 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 模块将不会运行.

What this means is that the shell module will not run if /home/x/output.log already exists.

这篇关于如何在 Ansible 中制作幂等 shell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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