实例学习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
-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]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

这里写图片描述

猜你喜欢

转载自blog.csdn.net/sdfsdfytre/article/details/83578641