• 欢迎访问蜷缩的蜗牛博客 蜷缩的蜗牛
  • 微信搜索: 蜷缩的蜗牛 | 联系站长 kbsonlong@qq.com
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

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

Ansible 蜷缩的蜗牛 8个月前 (01-23) 18次浏览 已收录

知识点: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 使用实例

<code class=" hljs coffeescript">Inventory 内容设定 servers 为<span class="hljs-number">2</span>台机器列表
[root<span class="hljs-property">@host31</span> ansible]<span class="hljs-comment"># cat inventory</span>
[servers]
host31
host32
不使用-i 参数提示 hosts list 为空
[root<span class="hljs-property">@host31</span> ansible]<span class="hljs-comment"># ansible servers -m ping</span>
 [WARNING]: provided hosts list <span class="hljs-keyword">is</span> empty, only localhost <span class="hljs-keyword">is</span> available

[root<span class="hljs-property">@host31</span> ansible]<span class="hljs-comment">#</span>
指定-i 之后能够正常动作
[root<span class="hljs-property">@host31</span> ansible]<span class="hljs-comment"># ansible servers -i inventory -m ping</span>
host31 | SUCCESS<span class="hljs-function"> =></span> {
    <span class="hljs-string">"changed"</span>: <span class="hljs-literal">false</span>,
    <span class="hljs-string">"ping"</span>: <span class="hljs-string">"pong"</span>
}
host32 | SUCCESS<span class="hljs-function"> =></span> {
    <span class="hljs-string">"changed"</span>: <span class="hljs-literal">false</span>,
    <span class="hljs-string">"ping"</span>: <span class="hljs-string">"pong"</span>
}
[root<span class="hljs-property">@host31</span> ansible]<span class="hljs-comment">#</span></code>

-k 使用实例

<code class=" hljs ruby">[root<span class="hljs-variable">@host31</span> ~]<span class="hljs-comment"># ansible host31 -k -m command -a "echo hello"</span>
<span class="hljs-constant">SSH</span> <span class="hljs-symbol">password:</span>
虽然设定了 ssh 通路,加上-k 之后依然需要输入密码
host31 | <span class="hljs-constant">SUCCESS</span> | rc=<span class="hljs-number">0</span> >>
hello

[root<span class="hljs-variable">@host31</span> ~]<span class="hljs-comment">#</span></code>

使用-k,实际是使用 sshpass

<code class=" hljs perl">[root<span class="hljs-variable">@host31</span> ~]<span class="hljs-comment"># ansible host31 -k -m command -a "echo hello" -vvv</span>
Using /etc/ansible/ansible.cfg as config file
SSH password:
<host31> ESTABLISH SSH CONNECTION FOR USER: None
<host31> SSH: EXEC sshpass -d12 ssh -C -<span class="hljs-keyword">q</span> -o ControlMaster=auto -o ControlPersist=<span class="hljs-number">60</span><span class="hljs-keyword">s</span> -o ConnectTimeout=<span class="hljs-number">10</span> -o ControlPath=<span class="hljs-regexp">/root/</span>.ansible/cp/ansible-ssh-<span class="hljs-variable">%h</span>-<span class="hljs-variable">%p</span>-<span class="hljs-variable">%r</span> host31 <span class="hljs-string">'/bin/sh -c '</span><span class="hljs-string">"'"</span><span class="hljs-string">'( 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'</span><span class="hljs-string">"'"</span><span class="hljs-string">''</span>
<host31> PUT /tmp/tmp5kG8hJ TO /root/.ansible/tmp/ansible-tmp-<span class="hljs-number">1469849015.27</span>-<span class="hljs-number">12240784319451</span>/command
<host31> SSH: EXEC sshpass -d12 sftp -o BatchMode=<span class="hljs-keyword">no</span> -b - -C -o ControlMaster=auto -o ControlPersist=<span class="hljs-number">60</span><span class="hljs-keyword">s</span> -o ConnectTimeout=<span class="hljs-number">10</span> -o ControlPath=<span class="hljs-regexp">/root/</span>.ansible/cp/ansible-ssh-<span class="hljs-variable">%h</span>-<span class="hljs-variable">%p</span>-<span class="hljs-variable">%r</span> <span class="hljs-string">'[host31]'</span>
<host31> ESTABLISH SSH CONNECTION FOR USER: None
<host31> SSH: EXEC sshpass -d12 ssh -C -<span class="hljs-keyword">q</span> -o ControlMaster=auto -o ControlPersist=<span class="hljs-number">60</span><span class="hljs-keyword">s</span> -o ConnectTimeout=<span class="hljs-number">10</span> -o ControlPath=<span class="hljs-regexp">/root/</span>.ansible/cp/ansible-ssh-<span class="hljs-variable">%h</span>-<span class="hljs-variable">%p</span>-<span class="hljs-variable">%r</span> -tt host31 <span class="hljs-string">'/bin/sh -c '</span><span class="hljs-string">"'"</span><span class="hljs-string">'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'</span><span class="hljs-string">"'"</span><span class="hljs-string">''</span>
host31 | SUCCESS | rc=<span class="hljs-number">0</span> >>
hello

[root<span class="hljs-variable">@host31</span> ~]<span class="hljs-comment">#</span></code>

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

<code class=" hljs perl">[root<span class="hljs-variable">@host31</span> ~]<span class="hljs-comment"># ansible host31 -m command -a "echo hello" -vvv</span>
Using /etc/ansible/ansible.cfg as config file
<host31> ESTABLISH SSH CONNECTION FOR USER: None
<host31> SSH: EXEC ssh -C -<span class="hljs-keyword">q</span> -o ControlMaster=auto -o ControlPersist=<span class="hljs-number">60</span><span class="hljs-keyword">s</span> -o KbdInteractiveAuthentication=<span class="hljs-keyword">no</span> -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=<span class="hljs-keyword">no</span> -o ConnectTimeout=<span class="hljs-number">10</span> -o ControlPath=<span class="hljs-regexp">/root/</span>.ansible/cp/ansible-ssh-<span class="hljs-variable">%h</span>-<span class="hljs-variable">%p</span>-<span class="hljs-variable">%r</span> host31 <span class="hljs-string">'/bin/sh -c '</span><span class="hljs-string">"'"</span><span class="hljs-string">'( 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'</span><span class="hljs-string">"'"</span><span class="hljs-string">''</span>
<host31> PUT /tmp/tmpTvuyFh TO /root/.ansible/tmp/ansible-tmp-<span class="hljs-number">1469849066.0</span>-<span class="hljs-number">90742279645616</span>/command
<host31> SSH: EXEC sftp -b - -C -o ControlMaster=auto -o ControlPersist=<span class="hljs-number">60</span><span class="hljs-keyword">s</span> -o KbdInteractiveAuthentication=<span class="hljs-keyword">no</span> -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=<span class="hljs-keyword">no</span> -o ConnectTimeout=<span class="hljs-number">10</span> -o ControlPath=<span class="hljs-regexp">/root/</span>.ansible/cp/ansible-ssh-<span class="hljs-variable">%h</span>-<span class="hljs-variable">%p</span>-<span class="hljs-variable">%r</span> <span class="hljs-string">'[host31]'</span>
<host31> ESTABLISH SSH CONNECTION FOR USER: None
<host31> SSH: EXEC ssh -C -<span class="hljs-keyword">q</span> -o ControlMaster=auto -o ControlPersist=<span class="hljs-number">60</span><span class="hljs-keyword">s</span> -o KbdInteractiveAuthentication=<span class="hljs-keyword">no</span> -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=<span class="hljs-keyword">no</span> -o ConnectTimeout=<span class="hljs-number">10</span> -o ControlPath=<span class="hljs-regexp">/root/</span>.ansible/cp/ansible-ssh-<span class="hljs-variable">%h</span>-<span class="hljs-variable">%p</span>-<span class="hljs-variable">%r</span> -tt host31 <span class="hljs-string">'/bin/sh -c '</span><span class="hljs-string">"'"</span><span class="hljs-string">'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'</span><span class="hljs-string">"'"</span><span class="hljs-string">''</span>
host31 | SUCCESS | rc=<span class="hljs-number">0</span> >>
hello

[root<span class="hljs-variable">@host31</span> ~]<span class="hljs-comment">#</span></code>

本文转载自 实例学习 ansible 系列(3)Ansible 执行命令常用 Option


蜷缩的蜗牛 , 版权所有丨如未注明 , 均为原创丨 转载请注明实例学习 ansible 系列(3)Ansible 执行命令常用 Option
喜欢 (0)
[]
分享 (0)