1. 常见的使用命令
自己经常使用到的命令有
# 测试配置的机器是否能ping通 ansile test -m ping # 指定hosts,而非使用默认/etc/ansible/hosts配置 ansile test -m ping -i hosts # 指定登录用户 ansible test -m shell -a "sudo jps" -u datard -k # 拷贝命令 ansible test -m copy -a 'src=./jvm.properties dest=/opt/xxx/etc/' # 执行shell命令 ansible test -m shell -a 'cat /opt/xxxx/jvm.properties | grep "max.request.size" ' 1234567891011121314
2. 一些高阶需求
2.1 shell模块,命令中有特殊字符通过shell执行shell命令时,需要通过awk获取某些列的数据并打印。
其中awk '{print $1}'中,$属于特殊字符,需要添加转义字符才能正常运行
ansible test -m shell -a "sudo jps | awk '{print $1}'" 1 2.2 shell模块,命令中需要定义变量 将命令的执行结果赋值给变量,再针对该变量进行后续处理下面命令的意图:获取某个服务的进程pid,然后进入对应的/proc/pid目录获取其工作目录信息(cwd)这时,需要将sudo jps | grep xxServer | awk '{print $1}'的结果保存变量中,变量的定义方式$( ... )同样需要对变量定义中的$添加转义字符
ansible test -m shell -a "sudo ls -l /proc/$(sudo jps | grep xxServer | awk '{print $1}') | grep cwd " 1