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

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

                       
 

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

Option说明

                                                               
option说明
-v详细信息输出
-i指定inventory的目录,缺省会使用/etc/ansible/hosts
-ffork的进程个数,默认是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等。
-bsu的方式,可以指定用户
-Conly for check

-i使用实例

Inventory内容设定servers为2台机器列表[root@host31 ansible]# cat inventory[servers]host31host32不使用-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 pinghost31 | SUCCESS => {    "changed": false,    "ping": "pong"}host32 | SUCCESS => {    "changed": false,    "ping": "pong"}[root@host31 ansible]#
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

-k使用实例

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

使用-k,实际是使用sshpass

[root@host31 ~]# ansible host31 -k -m command -a "echo hello" -vvvUsing /etc/ansible/ansible.cfg as config fileSSH password:<host31> ESTABLISH SSH CONNECTION FOR USER: None<host31> 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'"'"''<host31> PUT /tmp/tmp5kG8hJ TO /root/.ansible/tmp/ansible-tmp-1469849015.27-12240784319451/command<host31> 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]'<host31> ESTABLISH SSH CONNECTION FOR USER: None<host31> 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 ~]#
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
 

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

[root@host31 ~]# ansible host31 -m command -a "echo hello" -vvvUsing /etc/ansible/ansible.cfg as config file<host31> ESTABLISH SSH CONNECTION FOR USER: None<host31> 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'"'"''<host31> PUT /tmp/tmpTvuyFh TO /root/.ansible/tmp/ansible-tmp-1469849066.0-90742279645616/command<host31> 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]'<host31> ESTABLISH SSH CONNECTION FOR USER: None<host31> 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 ~]#
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值