我正在使用 ansible 部署到服务器。
即使已经安装了正确的版本,apt 阶段(它是 Ubuntu)也需要很长时间才能运行(我猜它只是运行并看到不需要安装任何东西)
示例命令:
- name: set up apt packages
action: apt pkg=nginx=1.4.6-1ubuntu3.3 state=present update_cache=yes
Run Code Online (Sandbox Code Playgroud)
(我有很多,所以需要相当长的时间)
有没有办法 ansible 可以“找出”已经安装了 pkg 并更快地运行/跳过此命令?
如果您设置update_cache=yes
Ansible 将apt-get update
在每次运行时运行。
在操作之前运行 apt-get update 等价物。可以作为包安装的一部分或作为单独的步骤运行。
通过删除update_cache=yes
任务应该运行得更快,因为 Ansible 不需要等待apt
更新其存储库。
另一种选择是使用stat
模块注册一些由包创建的文件或路径。像这样的东西:
- stat: path=/etc/nginx/nginx.conf
register: st
- name: set up apt packages
action: apt pkg=nginx=1.4.6-1ubuntu3.3 state=present update_cache=yes
when: not st.stat.exists
Run Code Online (Sandbox Code Playgroud)
有关更多示例,请参阅Ansible 文档。但我建议只删除update_cache=yes
参数。