问题描述
我正在使用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中制作半透明的外壳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!