实例学习ansible系列(3)Ansible执行命令常用Option

知识点:ansible命令执行常用Option
Ansible一般使用playbook来执行,ansible-playbook命令用于此种方式。如果不希望每次执行的时候都写一个playbook的yml文件,作为一个类似ssh延伸扩展功能的ansible还是能起到很多有用的作用的,本文将介绍一些平时用到较多的option。

Option说明

option 说明
-v 详细信息输出
-i 指定inventory的目录,缺省会使用/etc/ansible/hosts
-f fork的进程个数,默认是5
–private-key=xxx 指定ssh连接用的文件
-m 指定module
–module-name 指定module名称
–module-path 指定module的path 默认是/usr/share/ansible
-a 指定module的参数
-k 提示输入password
-K 提示输入sudo密码 与–sudo一起使用
-T 设定连接超时时长
-B 设定后台运行并设定超时时长
-c 设定连接类型 有ssh或者local等。
-b su的方式,可以指定用户
-C only for check

-i使用实例

Inventory内容设定servers为2台机器列表
[root@host31 ansible]# cat inventory
[servers]
host31
host32
不使用-i参数提示hosts list为空
[root@host31 ansible]# ansible servers -m ping
 [WARNING]: provided hosts list is empty, only localhost is available

[root@host31 ansible]#
指定-i之后能够正常动作
[root@host31 ansible]# ansible servers -i inventory -m ping
host31 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
host32 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
[root@host31 ansible]#

-k使用实例

[root@host31 ~]# ansible host31 -k -m command -a "echo hello"
SSH password:
虽然设定了ssh通路,加上-k之后依然需要输入密码
host31 | SUCCESS | rc=0 >>
hello

[root@host31 ~]#

使用-k,实际是使用sshpass

[root@host31 ~]# ansible host31 -k -m command -a "echo hello" -vvv
Using /etc/ansible/ansible.cfg as config file
SSH password:
 ESTABLISH SSH CONNECTION FOR USER: None
 SSH: EXEC sshpass -d12 ssh -C -q -o ControlMaster=auto -o ControlPersist=60s -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r host31 '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1469849015.27-12240784319451 `" && echo ansible-tmp-1469849015.27-12240784319451="` echo $HOME/.ansible/tmp/ansible-tmp-1469849015.27-12240784319451 `" ) && sleep 0'"'"''
 PUT /tmp/tmp5kG8hJ TO /root/.ansible/tmp/ansible-tmp-1469849015.27-12240784319451/command
 SSH: EXEC sshpass -d12 sftp -o BatchMode=no -b - -C -o ControlMaster=auto -o ControlPersist=60s -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r '[host31]'
 ESTABLISH SSH CONNECTION FOR USER: None
 SSH: EXEC sshpass -d12 ssh -C -q -o ControlMaster=auto -o ControlPersist=60s -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r -tt host31 '/bin/sh -c '"'"'LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1469849015.27-12240784319451/command; rm -rf "/root/.ansible/tmp/ansible-tmp-1469849015.27-12240784319451/" > /dev/null 2>&1 && sleep 0'"'"''
host31 | SUCCESS | rc=0 >>
hello

[root@host31 ~]#

不使用-k则是直接使用ssh

[root@host31 ~]# ansible host31 -m command -a "echo hello" -vvv
Using /etc/ansible/ansible.cfg as config file
 ESTABLISH SSH CONNECTION FOR USER: None
 SSH: EXEC ssh -C -q -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r host31 '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1469849066.0-90742279645616 `" && echo ansible-tmp-1469849066.0-90742279645616="` echo $HOME/.ansible/tmp/ansible-tmp-1469849066.0-90742279645616 `" ) && sleep 0'"'"''
 PUT /tmp/tmpTvuyFh TO /root/.ansible/tmp/ansible-tmp-1469849066.0-90742279645616/command
 SSH: EXEC sftp -b - -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r '[host31]'
 ESTABLISH SSH CONNECTION FOR USER: None
 SSH: EXEC ssh -C -q -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r -tt host31 '/bin/sh -c '"'"'LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1469849066.0-90742279645616/command; rm -rf "/root/.ansible/tmp/ansible-tmp-1469849066.0-90742279645616/" > /dev/null 2>&1 && sleep 0'"'"''
host31 | SUCCESS | rc=0 >>
hello

[root@host31 ~]#

你可能感兴趣的:(#,实例学习Ansible系列)