Ansible配置文件与命令行模式使用案例一
目录
Ansible配置文件与命令行模式使用案例(一)
1 默认配置文件解析
/etc/ansible/ansible.cfg文件是Ansible的全局配置文件,用于定义Ansible的默认行为和设置,可以自定义Ansible的运行方式,比如否需要输入密码、是否开启sudo认证、action_plugins插件的位置、hosts主机组的位置、是否开启log功能、默认端口、key文件位置等。
sudo_user: 默认是root,用于指定sudo使用的用户,示例:sudo_user = deploy
ask_sudo_pass: 默认True,是否需要输入sudo密码,示例:ask_sudo_pass = False
ask_pass: 默认True,是否需要输入SSH密码,示例:ask_pass = False
remote_port: 默认22,SSH端口,通常不修改,示例:remote_port = 2222
module_lang: 默认C,模块通信语言,示例:module_lang = en_US.UTF-8
host_key_checking: False跳过SSH主机密钥检查,但需注意安全,示例:host_key_checking = False
timeout: 默认10秒连接超时,示例:timeout = 30
module_name: 默认command模块,示例:module_name = shell
nocolor: 设置为1关闭颜色输出,示例:nocolor = 1
private_key_file: 指定SSH密钥路径,示例:private_key_file = /home/user/.ssh/id_rsa
2 命令行模式常用选项
命令行模式 : 是指ansible临时执行的一条或多条命令,并且不需要保存命令,用于快速执行单条或简单任务,无需编写 Playbook,适合临时性操作
- 适用场景 :快速部署、状态检查等简单任务;复杂任务推荐使用playbook
- 模块依赖 :所有操作需通过预定义模块实现,Ansible自带常用模块(如 command、shell、file 等)
命令行模式常用选项:
- -m module:指定要执行的模块名称,不指定-m选项,默认是command模块
- -a module_args:指定执行模块对应的参数选项
- -k:提示输入ssh登录密码而不是基于密钥验证
- -K:用于输入执行su或sudo操作时需要的认证密码
- -b:提升权限(默认sudo切换至root)
- –become-method:指定提升权限的方法,常用的有sudo和su,默认是sudo
- –become-user:指定执行sudo或su命令时要切换用户,默认是root
- -B SECONDS:后台运行超时时间
- -C:用于测试
- -f FORKS,:设置ansible并行的任务数,默认值是5
- -i Invenory: 指定主机清单文件的路径,默认为/etc/ansible/hosts
命令行模式命令格式:
ansible 主机或组 -m 模块名 -a ‘模块参数’ ansible参数
说明:
- 主机和组:/etc/ansible/hosts文件进行指定内容
- 模块名:不指定时,默认使用command模块,具体可通过ansible-doc -l查看目前已安装的模块
- 模块参数:可以使用“ansible-doc 模块名”命令进行查看用法及后面的具体参数
- ansible参数:比如是否需要输入密码、是否sudo等等
3 命令行常用模块使用案例
3.1 command模块
creates:
- 作用 :当指定的文件或路径存在时,跳过命令执行;否则执行命令
- 适用场景 :避免重复操作(如生成文件后无需重复执行)
示例:ansible web -m command -a "touch /tmp/foo.txt creates=/tmp/tmp.txt"
[root@node2 apache-playbook]# ansible web -m command -a "touch /tmp/foo.txt creates=/tmp/tmp.txt"
192.168.10.32 | SUCCESS | rc=0 >>
skipped, since /tmp/tmp.txt exists
[root@node2 apache-playbook]#
chdir:
- 作用 :在执行命令前切换到指定目录
- 适用场景 :需在特定目录下执行命令(如运行脚本或编译代码)
示例:ansible web -m command -a "cat tmp.txt chdir=/tmp"
[root@node2 apache-playbook]# ansible web -m command -a "cat tmp.txt chdir=/tmp"
192.168.10.32 | CHANGED | rc=0 >>
[root@node2 apache-playbook]#
removes:
- 作用 :当指定的文件或路径存在时,执行命令;否则跳过
- 适用场景 :清理操作或基于文件存在的条件触发任务
示例:ansible web -m command -a "rm -f /tmp/*.txt removes=/tmp/cleanup.flag"
[root@node2 apache-playbook]# ansible web -m command -a "rm -f /tmp/*.txt removes=/tmp/cleanup.flag"
192.168.10.32 | SUCCESS | rc=0 >>
skipped, since /tmp/cleanup.flag does not exist
[root@node2 apache-playbook]#
注意事项:
- 路径问题 :creates和removes的路径需为绝对路径,或相对于chdir指定的目录
- 与Shell模块的区别 :command模块不支持管道符 (|) 或重定向 (>),需用shell模块替代
示例:ansible web -m command -a "cat /tmp/tmp.txt chdir=/tmp creates=/tmp/tmp.txt"
[root@node2 apache-playbook]# ansible web -m command -a "cat /tmp/tmp.txt chdir=/tmp creates=/tmp/tmp.txt"
192.168.10.32 | SUCCESS | rc=0 >>
skipped, since /tmp/tmp.txt exists
[root@node2 apache-playbook]#
[root@node2 apache-playbook]# ansible web -m shell -a "echo 'test' > /tmp/log.txt"
192.168.10.32 | CHANGED | rc=0 >>
[root@node2 apache-playbook]#
3.2 shell模块
核心特性: 通过/bin/sh执行命令,支持管道符 (|)、重定向 (>)、变量替换等Shell特性
与command模块区别:
- command:直接执行命令(不通过 Shell),不支持特殊符号
- shell:通过Shell解释器执行,支持复杂命令组合
示例1:执行命令并过滤结果
[root@node2 apache-playbook]# ansible web -m shell -a 'ps -ef | grep [s]shd'
192.168.10.32 | CHANGED | rc=0 >>
root 1295 1 0 Mar07 ? 00:00:00 /usr/sbin/sshd -D
root 72786 1295 8 22:26 ? 00:00:00 sshd: root@pts/0
[root@node2 apache-playbook]#
示例2:执行远程脚本并重定向输出
[root@node2 apache-playbook]# ansible web -m shell -a 'sh /tmp/test.sh >/tmp/test.log' -b
192.168.10.32 | CHANGED | rc=0 >>
[root@node2 apache-playbook]#
3.3 raw模块
核心特性: 直接通过SSH发送命令到远程主机,无需依赖远程Python环境
适用场景:
- 远程主机未安装Python
- Python版本过低
- 需要初始化环境(如安装Python或依赖包)
示例1:检查SSH服务进程
[root@node2 apache-playbook]# ansible web -m raw -a "ps -ef | grep sshd | awk '{print \$2}'"
192.168.10.32 | CHANGED | rc=0 >>
1295
72977
73201
73205
73220
Shared connection to 192.168.10.32 closed.
[root@node2 apache-playbook]#
示例2:安装Python环境
[root@node2 apache-playbook]# ansible web -m raw -a "yum -y install python2"
192.168.10.32 | CHANGED | rc=0 >>
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Package python-2.7.5-89.el7.x86_64 already installed and latest version
Nothing to do
Shared connection to 192.168.10.32 closed.
[root@node2 apache-playbook]#
3.4 script模块
核心特性: 将控制机(管理端)的Shell脚本传输到远程主机并执行,执行完毕后自动删除脚本
- 无需远程Python环境 :与raw模块类似,适用于远程主机无Python或版本过低的场景
- 原子操作 :自动完成上传 → 执行 →清理的全过程
示例:执行本地脚本
[root@node2 apache-playbook]# ansible web -m script -a "/tmp/test.sh"
192.168.10.32 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.10.32 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.10.32 closed."
],
"stdout": "uid=0(root) gid=0(root) groups=0(root)\r\n",
"stdout_lines": [
"uid=0(root) gid=0(root) groups=0(root)"
]
}
[root@node2 apache-playbook]#