此文章记录ansible使用时一些细节上的坑,ansible版本号:2.6.0(持续更新)
1.when做判断register创建的变量时要注意是否为布尔值,用debug输出一下,有时候你写"True"可能会跳过,但是True就可以
2.ansible的收集到的PATH变量需要注意,有时候你用源码安装的软件(比如mysql)本地可以执行命令但是使用ansible报"command not find"的错的时候用debug输出一下PATH对比一下,本地有的ansible可能没有,最好把源码安装的ln -s到/usr/bin下面
3.在ansible中用shell或command无法使用通配符,尽量避免通配符的使用,如果非要使用可以用/bin/bash -c 'command'进行执行
4.在ansible中想要循环执行命令怎么办?可以使用with_item.
---
- hosts: localhost
tasks:
- name: "when test"
command: "{{item}}"
with_items:
- "ls /opt"
- "ls /etc"
- "ls /"
register: a
- debug: var=a
如果以上问题有大佬遇到并有更好的解决办法欢迎指教,谢谢