4

我正在使用 Ansible,但在尝试使 shell 执行幂等时遇到了一点问题。我首先要做的是安装python-apt 包,因为我需要它来使用 apt 模块来安装其他包。但是每次我运行我的剧本时,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 
4

1 回答 1

11

您应该使用 ansibleapt模块来安装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

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

于 2014-07-04T15:29:03.307 回答