Ansible 1.4.2:ansible 命令的基本用法

1.4.2:ansible 命令的基本用法

ansible命令用于对特定主机定义和运行单个的Ansible任务。一般的用法为:

ansible <HOST_PATTERN> [OPTIONS] [-m MODULE_NAME] [-a "MODULE_ARGS"]

其中:

  • HOST_PATTER用于指定主机。
  • OPTIONS用于指定其它选项。
  • MODULE_NAME用于指定Ansible模块。
  • MODULE_ARGS是为指定模块传递的参数或命令。
1.4.2.1:ansible 命令常用选项
1.4.2.1.1:–version 显示版本信息

--version用于显示Ansible版本号、配置文件路径、模块查询路径、模块位置,以及Ansbile所调用的可执行环境(Python信息等)。

--version
          show program's version number, config file location, configured module search path, module location, executable location and exit

示例:

[root@ansible ~]# ansible --version
ansible 2.9.18
  config file = /root/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Nov 20 2015, 02:00:19) [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)]
1.4.2.1.2:-m 指定模块

-m用于指定本次任务所调用的Ansible模块名称(ansible-doc -l中所列的模块),如不指定,则为主配置文件ansible.cfg中的module_name选项指定的模块,默认是command模块)

-m 'MODULE_NAME', --module-name 'MODULE_NAME'
          module name to execute (default=command)

示例:

[root@ansible ~]# ansible websrvs -m ping
192.168.1.111 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    }, 
    "changed": false, 
    "ping": "pong"
}
1.4.2.1.3:-a 传递模块参数

-a向指定的模块传递相应的模块参数。

-a 'MODULE_ARGS', --args 'MODULE_ARGS'
          module arguments

示例(为websrvs主机组拷贝test.txt文件):

[root@ansible ~]# ansible websrvs -m copy -a "src=test.txt dest=/root/test.txt"
192.168.1.111 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    }, 
    "changed": true, 
    "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", 
    "dest": "/root/test.txt", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "d41d8cd98f00b204e9800998ecf8427e", 
    "mode": "0644", 
    "owner": "root", 
    "size": 0, 
    "src": "/root/.ansible/tmp/ansible-tmp-1619403777.61-2887-91072838353275/source", 
    "state": "file", 
    "uid": 0
}
1.4.2.1.4:-v/-vv/-vvv/-vvvv 执行过程详细信息
-v, --verbose
          verbose mode (-vvv for more, -vvvv to enable connection debugging)

示例(显示ping模块的详细执行过程):

-v显示详细过程:

[root@ansible ~]# ansible websrvs -m ping -v
Using /root/ansible.cfg as config file
192.168.1.111 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    }, 
    "changed": false, 
    "ping": "pong"
}

-vv显示更详细的过程:

[root@ansible ~]# ansible websrvs -m ping -vv

在这里插入图片描述

-vvv显示更更详细过程:

[root@ansible ~]# ansible websrvs -m ping -vvv

在这里插入图片描述

-vvvv开启debug模式,最详细:

[root@ansible ~]# ansible websrvs -m ping -vvvv

在这里插入图片描述

1.4.2.1.5:–list-hosts 列出主机列表

列出指定的HOST_PATTER中包含的主机,可以简写为--list

--list-hosts
          outputs a list of matching hosts; does not execute anything else

示例(列出websrvs组中包含的主机):

[root@ansible ~]# ansible websrvs --list
  hosts (1):
    192.168.1.111
1.4.2.1.6:-C 检查

加上-C选项后,不做任何实际的操作,只是将预测的任务执行过程打印出来,检查可能出现的错误。

-C, --check
          don't make any changes; instead, try to predict some of the changes that may occur

示例:

[root@ansible ~]# ansible websrvs -C -m copy -a "src=test2.txt dest=/root/test2.txt" 
192.168.1.111 | CHANGED => {
    "changed": true
}
1.4.2.1.7:-T 指定超时时间

指定本次任务执行所建连接的超时时间,单位为s,默认是10s。

-T 'TIMEOUT', --timeout 'TIMEOUT'
          override the connection timeout in seconds (default=10)

示例(指定超时时间为3s):

[root@ansible ~]# ansible websrvs -T 3 -m copy -a "src=test3.txt dest=/root/test3.txt"
192.168.1.111 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    }, 
    "changed": true, 
    "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", 
    "dest": "/root/test3.txt", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "d41d8cd98f00b204e9800998ecf8427e", 
    "mode": "0644", 
    "owner": "root", 
    "size": 0, 
    "src": "/root/.ansible/tmp/ansible-tmp-1619404117.78-2998-73842112532100/source", 
    "state": "file", 
    "uid": 0
}
1.4.2.1.8:-k 询问连接密码

如果已经设置了基于key的验证,在使用-k选项时,将连接验证的方式临时改为密码验证。

-k, --ask-pass
          ask for connection password

示例(指定-k选项,需要输入SSH连接密码才能继续执行任务):

[root@ansible ~]# ansible websrvs -k -m ping 
SSH password: 
192.168.1.111 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    }, 
    "changed": false, 
    "ping": "pong"
}
1.4.2.1.9:-u 指定远程连接目标主机的用户

默认为None。

-u 'REMOTE_USER', --user 'REMOTE_USER'
          connect as this user (default=None)

示例(使用testuser用户连接):

whoami验证当前用户为testuser。

[root@ansible ~]# ansible websrvs -k -u testuser -m shell -a "whoami"
SSH password: 
192.168.1.111 | CHANGED | rc=0 >>
testuser
1.4.2.1.10:-f 并发数量

指定本次任务的并发执行数,如不指定,则使用ansible.cfg中的forks指定的数量,默认为5。

-f 'FORKS', --forks 'FORKS'
          specify number of parallel processes to use (default=5)
1.4.2.1.11:-b 使用sudo

使用-b表示不使用ssh用户,而要使用sudo切换到指定的become-user来执行任务。

-b, --become
          run operations with become (does not imply password prompting)

示例(使用testuser进行ssh连接,并sudo到root用户来执行whoami):

#在目标主机192.168.1.111上添加testuser的sudo权限
root@node111:~# visudo
testuser ALL=(ALL) NOPASSWD: ALL


#验证用户sudo是否成功
[root@ansible ~]# ansible 192.168.1.111 -b -k -u testuser -m shell -a "whoami"   
SSH password: 
192.168.1.111 | CHANGED | rc=0 >>
root
1.4.2.1.12:-K 询问sudo密码
-K, --ask-become-pass
          ask for privilege escalation password

示例(即使testuser在目标主机上的sudo权限设为NOPASSWD,加上-K选项后,在sudo到root时仍然需要输入root用户的密码才能切换):

[root@ansible ~]# ansible 192.168.1.111 -b -k -K -u testuser -m shell -a "whoami"
SSH password: 
#需要输入BECOME password,这里要sudo到root,所以是root用户的密码
BECOME password[defaults to SSH password]: 
192.168.1.111 | CHANGED | rc=0 >>
root
1.4.2.1.13:–become-user 指定sudo目标用户

使用-b进行sudo时,默认是sudo到root用户,而--become-user可以sudo到指定用户。

--become-user 'BECOME_USER'
          run operations as this user (default=root)

示例(使用testuser进行连接,并sudo到testbecome用户执行whoami命令):

#目标主机192.168.1.111添加testbecome用户并设置登录密码
root@node111:~# useradd testbecome
root@node111:~# passwd testbecome


#使用testuser进行连接,并sudo到testbecome用户执行whoami命令,验证已sudo到testbecome用户
[root@ansible ~]# ansible 192.168.1.111 -b -k -u testuser --become-user=testbecome -m shell -a "whoami"     
SSH password: 
192.168.1.111 | CHANGED | rc=0 >>
testbecome
1.4.2.2:ansible 命令的 HOST_PATTERN 匹配方式

HOST_PATTERN用于指定被管理主机,有以下匹配方式。

1.4.2.2.1:“all” 全集(所有主机)

all表示匹配主机清单文件中的所有主机

示例(ping主机清单中的所有主机):

[root@ansible ~]# cat /etc/ansible/hosts
[websrvs]
192.168.1.111
[dbsrvs]
192.168.1.112
[testsrvs]
192.168.1.113

[root@ansible ~]# ansible all -m ping
192.168.1.112 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    }, 
    "changed": false, 
    "ping": "pong"
}
192.168.1.111 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    }, 
    "changed": false, 
    "ping": "pong"
}
192.168.1.113 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    }, 
    "changed": false, 
    "ping": "pong"
}
1.4.2.2.2:“*” 通配符

可以使用*来表示所有字符。

示例(ping主机清单中192.168.1.开头的IP地址):

[root@ansible ~]# ansible 192.168.1.* -m ping
192.168.1.111 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    }, 
    "changed": false, 
    "ping": "pong"
}
192.168.1.113 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    }, 
    "changed": false, 
    "ping": "pong"
}
192.168.1.112 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    }, 
    "changed": false, 
    "ping": "pong"
}
1.4.2.2.3:“:” 并集

使用:取并集,用""将表达式引起来。

示例(ping主机清单中websrvs或testsrvs组中的主机;即取这两个主机组中的并集,重复主机只ping一次):

[root@ansible ~]# cat /etc/ansible/hosts
[websrvs]
192.168.1.111
192.168.1.113
[dbsrvs]
192.168.1.112
[testsrvs]
192.168.1.113

#websrvs和testsrvs的并集为:192.168.1.111、192.168.1.113

[root@ansible ~]# ansible "websrvs:testsrvs" -m ping       
192.168.1.113 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    }, 
    "changed": false, 
    "ping": "pong"
}
192.168.1.111 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    }, 
    "changed": false, 
    "ping": "pong"
}
1.4.2.2.4:“:&” 交集

使用:&取交集,用""将表达式引起来。

示例(ping即在websrvs组,又在testsrvs组中的主机):

[root@ansible ~]# cat /etc/ansible/hosts
[websrvs]
192.168.1.111
192.168.1.113
[dbsrvs]
192.168.1.112
[testsrvs]
192.168.1.113

#websrvs和testsrvs的交集为:192.168.1.113


[root@ansible ~]# ansible "websrvs:&testsrvs" -m ping
192.168.1.113 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    }, 
    "changed": false, 
    "ping": "pong"
}

1.4.2.2.5:“:!” 相对补集

使用:!取两个主机组之间的相对补集,用''将表达式引起来(这里是单引号)

示例(ping在websrvs组,但不在testsrvs组中的主机):

[root@ansible ~]# cat /etc/ansible/hosts
[websrvs]
192.168.1.111
192.168.1.113
[dbsrvs]
192.168.1.112
[testsrvs]
192.168.1.113

#testsrvs对websrvs的相对补集为(属于websrvs但不属于testsrvs):192.168.1.111

[root@ansible ~]# ansible 'websrvs:!testsrvs' -m ping    
192.168.1.111 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    }, 
    "changed": false, 
    "ping": "pong"
}
1.4.2.2.6:“~” 正则表达式

使用~开头表示使用正则表达式匹配,用""将表达式引起来。

示例(ping以srvs结尾的主机组中的主机):

[root@ansible ~]# cat /etc/ansible/hosts
[websrvs]
192.168.1.111
[dbsrvs]
192.168.1.112
[testsrvs]
192.168.1.113
[ftp]
192.168.1.114

#以srvs结尾的主机组当中包含的主机为:192.168.1.111、192.168.1.112、192.168.1.113

[root@ansible ~]# ansible "~.*srvs" -m ping          
192.168.1.111 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    }, 
    "changed": false, 
    "ping": "pong"
}
192.168.1.113 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    }, 
    "changed": false, 
    "ping": "pong"
}
192.168.1.112 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    }, 
    "changed": false, 
    "ping": "pong"
}
1.4.2.3:ansible 命令的执行过程
  1. 加载对应的配置文件ansible.cfg
  2. 加载对应的模块文件;
  3. 根据需要执行的任务生成临时的py文件,并推送到远程主机的执行用户家目录下的临时目录~/.ansible/tmp下;
  4. 执行py文件并返回结果;
  5. 删除临时py文件并关闭连接。

任务执行过程中在目标主机上查看临时py文件的进程:

[root@node203 ~]# ps -ef | grep py

在这里插入图片描述

1.4.2.4:ansible 命令的执行状态

Ansible的执行状态通过返回的执行结果的颜色进行标识,默认的颜色标识如下:

[root@ansible ~]# grep -A 14 '\[colors\]' /etc/ansible/ansible.cfg 
[colors]
#highlight = white
#verbose = blue
#warn = bright purple
#error = red
#debug = dark gray
#deprecate = purple
#skip = cyan
#unreachable = red
#ok = green
#changed = yellow
#diff_add = green
#diff_remove = red
#diff_lines = cyan

常见的执行状态为:

  • 绿色:执行成功但没做更改的操作(changed: false)
  • 黄色:执行成功且做了更改的操作(changed: true)
  • 红色:执行失败
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值