ansible_shell_executable
to a non-Bourne (sh) compatible shell. By default commands are formatted using
sh
-style syntax. Setting this to
csh
or
fish
will cause commands executed on target systems to follow those shell’s syntax instead.
ansible_python_interpreter
The target host python path. This is useful for systems with more than one Python or not located at
/usr/bin/python such as *BSD, or where
/usr/bin/python is not a 2.X series Python. We do not use the
/usr/bin/env mechanism as that requires the remote user’s path to be set right and also assumes the
python executable is named python, where the executable might be named something like
python2.6.
ansible_*_interpreter
Works for anything such as ruby or perl and works just like
ansible_python_interpreter
. This replaces shebang of modules which will run on that host.
-
ansible_shell_executable
-
This sets the shell the ansible controller will use on the target machine, overrides
executable
in
ansible.cfg
which defaults to
/bin/sh. You should really only change it if is not possible to use
/bin/sh (i.e.
/bin/sh is not installed on the target machine or cannot be run from sudo.).
Examples from an Ansible-INI host file:
Ⅱ. Ansible配置及命令详解
· module_name
Ansible将管理功能分成一个个模块,默认是'command'模块,但是command模块不支持shell变量、管道、配额。所以,执行带有管道的命令,可以使用'shell'模块。
· pattern
如果没有提供'hosts'节点,这是playbook要通信的默认主机组,默认值是对所有主机通信。
1. 指定一组连续的机器:ansible 192.168.1.* -m ping (指定192.168.1/28网段所有机器)
2. 指定一组不相关机器:ansible abcd.com:efgh.com -m ping (同样适用于组连接)
3. 指定在local组,不在web组的机器: local:!web (从左到右依次匹配)
4. 指定在local组,也在web组的机器:local:&web
-a 指定传入模块的参数
-C -D 一起使用,检查hosts规则文件的修改
-l 限制匹配规则的主机数
--list-hosts 显示所有匹配规则的主机
-m -M指定所使用的模块和模块的路径
--syntax-check 检查语法
-v 显示详细日志
Ⅲ. Ansible命令举例
1> 执行第一条ansible命令
# ansible local -m ping #使用ping模块 server | UNREACHABLE! => { "changed": false, "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).\r\n", } client | UNREACHABLE! => { "changed": false, "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).\r\n", }
由于ansible是基于ssh,这里我们先要配置公钥。
# ssh-keygen -t rsa # ssh-copy-id -i root@192.168.137.6 #为ansible管理的主机安装server的公钥
再次运行上一条命令:
# ansible local -m ping server | SUCCESS => { "changed": false, "ping": "pong" } client | SUCCESS => { "changed": false, "ping": "pong" }
2> 其他命令简介
查看local组主机内存使用情况:
# ansible local -a "free -m" server | SUCCESS | rc=0 >> total used free shared buff/cache available Mem: 1496 540 325 10 630 751 Swap: 2047 0 2047 client | SUCCESS | rc=0 >> total used free shared buff/cache available Mem: 1496 453 91 6 951 844 Swap: 2047 3 2044
若要执行带有管道的命令,可使用shell模块:
# ansible local -m shell -a "df -h | grep /home" server | SUCCESS | rc=0 >>
/dev/mapper/cl-home 16G 187M 16G 2% /home client | SUCCESS | rc=0 >>
/dev/mapper/cl-home 16G 187M 16G 2% /home
限定命令只在一台cl