加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何在Ansible中创建一个idempontent shell

发布时间:2020-12-15 22:19:36 所属栏目:安全 来源:网络整理
导读:我正在使用Ansible,我试图使幂等幂执行 shell时遇到一些问题.我做的第一个是安装 python-apt包,因为我需要它来使用apt模块来安装其他包.但每次我运行我的playbook时,shell任务总是运行,我想让它成为幂等的.这是我的shell任务: - name: install pyton-apt sh
我正在使用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模块提供了创建参数来帮助使它成为幂等的.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读