一. Ansible 介绍
Ansible是一个配置管理系统configuration management system, python 语言是运维人员必须会的语言, ansible 是一个基于python 开发的自动化运维工具, 其功能实现基于ssh远程链接服务, ansible 能够实现批量系统配置,批量软件部署,批量文件拷贝,批量运行命令等功能, 除了ansible以外,还有saltstack 等批量管理软件.html
Ansible能作什么?
ansible能够帮助运维人员完成一些批量任务,或者完成一些须要常常重复的工做。
好比:同时在100台服务器上安装nginx服务,并在安装后启动服务。
好比:将某个文件一次性拷贝到100台服务器上。
好比:每当有新服务器加入工做环境时,运维人员都要为新服务器部署某个服务,也就是说运维人员须要常常重复的完成相同的工做。
这些场景中运维人员均可以使用到ansible。node
Ansible软件特色?
ansible不须要单独安装客户端,SSH至关于ansible客户端。
ansible不须要启动任何服务,仅需安装对应工具便可。
ansible依赖大量的python模块来实现批量管理。
ansible配置文件/etc/ansible/ansible.cfg。python
Ansible是一种agentless(基于ssh),可实现批量配置、命令执行和控制,基于Python实现的自动化运维工具。Ansible的两个特性:
- 模块化:经过调用相关模块,完成指定任务,且支持任何语言编写的自定义模块;
- playbook:剧本,可根据须要一次执行完剧本中的全部任务或某些任务;linux
Ansible 架构nginx
- 链接插件(connectior plugins) 用于链接主机 用来链接被管理端。
- 核心模块(core modules) 链接主机实现操做, 它依赖于具体的模块来作具体的事情。
- 自定义模块(custom modules) 根据本身的需求编写具体的模块。
- 插件(plugins) 完成模块功能的补充。
- 剧本(playbooks)ansible的配置文件,将多个任务定义在剧本中,由ansible自动执行。
- 主机清单(host inventory)定义ansible须要操做主机的范围。git
最重要的一点: ansible是模块化的, 它全部的操做都依赖于模块, 不须要单独安装客户端(no agents),基于系统自带的sshd服务,sshd就至关于ansible的客户端, 不须要服务端(no sever),须要依靠大量的模块实现批量管理, 配置文件 /etc/ansible/ansible.cfg (前期不用配置)。github
二. Ansible命令使用
语法格式:
ansible <pattern_goes_here> -m <module_name> -a <arguments>
也就是:
ansible 匹配模式 -m 模块 -a '须要执行的内容'web
解释说明:
匹配模式:即哪些机器生效 (能够是某一台, 或某一组, 或all) , 默认模块为command , 执行常规的shell命令. 正则表达式
-m name, --module-name=name: 指定执行使用的模块。
-u username, --user=username: 指定远程主机以username运行命令。
-s, --sudo: 至关于linux系统下的sudo命令。
-usudo_username, --sudo-user=sudo_username: 使用sudo, 至关于linux系统下的sudo命令。
-C, --check: 只检查不实际执行。
-e, 即extra_vars: 引用外部参数。
-i, 即inventory: 指定仓库列表, 默认/etc/ansible/hosts。
--list-host: 列出执行主机列。 docker
三. Ansible经常使用模块
ping 模块: 检查指定节点机器是否还能连通,用法很简单,不涉及参数,主机若是在线,则回复pong 。
raw 模块: 执行原始的命令,而不是经过模块子系统。
yum 模块: RedHat和CentOS的软件包安装和管理工具。
apt 模块: Ubuntu/Debian的软件包安装和管理工具。
pip 模块 : 用于管理Python库依赖项,为了使用pip模块,必须提供参数name或者requirements。
synchronize 模块: 使用rsync同步文件,将主控方目录推送到指定节点的目录下。
template 模块: 基于模板方式生成一个文件复制到远程主机(template使用Jinjia2格式做为文件模版,进行文档内变量的替换的模块。
copy 模块: 在远程主机执行复制操做文件。
user 模块 与 group 模块: user模块是请求的是useradd, userdel, usermod三个指令,goup模块请求的是groupadd, groupdel, groupmod 三个指令。
service 模块: 用于管理远程主机的服务。
get_url 模块: 该模块主要用于从http、ftp、https服务器上下载文件(相似于wget)。
fetch 模块: 它用于从远程机器获取文件,并将其本地存储在由主机名组织的文件树中。
file 模块: 主要用于远程主机上的文件操做。
lineinfile 模块: 远程主机上的文件编辑模块
unarchive模块: 用于解压文件。
command模块 和 shell模块: 用于在各被管理节点运行指定的命令. shell和command的区别:shell模块能够特殊字符,而command是不支持
hostname模块: 修改远程主机名的模块。
script模块: 在远程主机上执行主控端的脚本,至关于scp+shell组合。
stat模块: 获取远程文件的状态信息,包括atime,ctime,mtime,md5,uid,gid等信息。
cron模块: 远程主机crontab配置。
mount模块: 挂载文件系统。
find模块: 帮助在被管理主机中查找符合条件的文件,就像 find 命令同样。
selinux模块:远程管理受控节点的selinux的模块
四. Ansible 自动化运维操做记录
1) 实验环境准备
ip地址 主机名 角色 系统版本 172.16.60.211 ansible-server 主控节点 centos7.5 172.16.60.212 ansible-node01 受控节点1 centos7.5 172.16.60.213 ansible-node02 受控节点2 centos7.5 172.16.60.214 ansible-node03 受控节点3 centos7.5 四个节点各自设置主机名 [root@ansible-server ~]# hostnamectl set-hostname ansible-server [root@ansible-node01 ~]# hostnamectl set-hostname ansible-node01 [root@ansible-node02 ~]# hostnamectl set-hostname ansible-node02 [root@ansible-node03 ~]# hostnamectl set-hostname ansible-node03 设置主控节点到受控节点的ssh无密码信任关系 (ansible应用环境下, 主控节点必需要设置ssh无密码跳转到受控节点的信任关系) 添加主控节点到受控节点的认证! 首先主控节点必需要生成公私密钥对, 不然不能进行免密信任关系设置!! [root@ansible-server ~]# ssh-keygen -t rsa #一直回车便可 [root@ansible-server ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub -p22 root@172.16.60.212 #回车, 输入远程登陆的密码便可 [root@ansible-server ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub -p22 root@172.16.60.213 [root@ansible-server ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub -p22 root@172.16.60.214 若是受控节点数量比较多的话, 可使用expect进行远程ssh链接的免密信任关系, 具体可参考: https://www.cnblogs.com/kevingrace/p/5900303.html
2) Ansible安装部署
Ansible有两种安装方式: yum 和 pip 安装方式 Ansible在epel的yum中有提供,因此配置好epel源,直接使用yum命令安装便可. CentOS系统, 可使用默认的yum源直接安装ansible [root@ansible-server ~]# yum install -y ansible 配置文件目录:/etc/ansible/ 执行文件目录:/usr/bin/ Lib库依赖目录:/usr/lib/pythonX.X/site-packages/ansible/ Help文档目录:/usr/share/doc/ansible-X.X.X/ Man文档目录:/usr/share/man/man1/ ================================================ pip安装方式 # yum -y install python-pip python-devel # pip install ansible ================================================ ansible程序文件 /usr/bin/ansible: 命令行工具 ansible命令通用格式: ansible <host-pattern> [options] [-m module_name] [-a args] /usr/bin/ansible-doc: 帮助文档 /usr/bin/ansible-playbook: 剧本执行工具 /etc/ansible/ansible.cfg: 主配置文件 /etc/ansible/hosts: 管理的主机清单 /etc/ansible/roles: 角色存放处 查看ansible命令帮助 [root@ansible-server ~]# ansible -h 查看支持的模块 [root@ansible-server ~]# ansible-doc -l [root@ansible-server ~]# ansible-doc -l|grep copy #查看copy模块 查看ansible的支持的模块个数 [root@ansible-server ~]# ansible-doc -l |wc -l 2080 查看ansible版本 [root@ansible-server ~]# ansible --version ansible 2.7.8 config file = /etc/ansible/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, Apr 11 2018, 07:36:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]
3) Ansible清单管理
inventory文件一般用于定义要管理主机的认证信息, 例如ssh登陆用户名、密码以及key相关信息。默认inventory文件为/etc/hostsweb-nodes, ansible模块操做命令中能够省去默认inventory (便可以省去"-i /etc/hostsweb-nodes"), 若是inventory文件定义了其余路径,则在ansible模块操做命令中不能省去, 要加上"-i inventory路径".
如何配置Inventory文件?
主控节点:
- 支持主机名通配以及正则表达式,例如web[1:3].oldboy.com;
- 支持基于非标准的ssh端口,例如web1.oldboy.com:6666;
- 支持指定变量,可对个别主机的特殊配置,如登录用户,密码等;
受控节点
- 支持嵌套组,例如[game:children],那么在game模块下面的组都会被game所包含;
- 支持指定变量,例如[game:vars]在下面指定变量;
ansible的inventory清单文件(/etc/ansible/hosts)中配置说明:
[ ] 中的名字表明组名
主机(hosts)部分可使用域名、主机名、IP地址表示, 通常此类配置中多使用IP地址;
组名下的主机地址就是ansible能够管理的地址;
下面是/etc/ansible/hosts 文件中清单的配置示例:
[root@ansible-server ~]# cd /etc/ansible/ [root@ansible-server ansible]# cp hosts hosts.bak [root@ansible-server ansible]# vim hosts #以下, 这里设置了两组管理清单 ........... #可使用受控节点的ip, 下面表示test-hosts组包括172.16.60.212, 172,16,60,213, 172.16.60.214 (若是ssh端口不是22, 好比是22222, 能够配置为"172.16.60.212:22222") [test-hosts] 172.16.60.2[12:14] #也可使用受控节点的主机名, 前提是主控节点要能跟受控节点的主机名正常通讯 [web-nodes] 172.16.60.21[2:3] #ansible-node03 #前提是要能ping通这个主机名, 即作好/etc/hosts主机名映射 简单测试,可设置是否生效 查看ansible清单里全部节点的uptime状况 [root@ansible-server ansible]# ansible all -m command -a "uptime" 172.16.60.213 | CHANGED | rc=0 >> 01:43:15 up 13 days, 22:19, 3 users, load average: 0.00, 0.01, 0.05 172.16.60.214 | CHANGED | rc=0 >> 01:43:15 up 13 days, 22:19, 3 users, load average: 0.01, 0.03, 0.05 172.16.60.212 | CHANGED | rc=0 >> 01:43:15 up 13 days, 23:30, 3 users, load average: 0.00, 0.01, 0.05 查看ansible清单里test-hosts组的节点的主机名 [root@ansible-server ansible]# ansible test-hosts -m command -a "hostname" 172.16.60.213 | CHANGED | rc=0 >> ansible-node02 172.16.60.214 | CHANGED | rc=0 >> ansible-node03 172.16.60.212 | CHANGED | rc=0 >> ansible-node01 查看ansible清单里web-nodes组的节点的系统时间 [root@ansible-server ansible]# ansible web-nodes -m command -a "date" 172.16.60.213 | CHANGED | rc=0 >> Sun Mar 17 01:51:02 CST 2019 172.16.60.212 | CHANGED | rc=0 >> Sun Mar 17 01:51:02 CST 2019 [root@ansible-server ansible]# ansible test-hosts --list-hosts hosts (3): 172.16.60.212 172.16.60.213 172.16.60.214 [root@ansible-server ansible]# ansible web-nodes --list-hosts hosts (2): 172.16.60.212 172.16.60.213 [root@ansible-server ansible]# ansible -i /etc/ansible/hosts web-nodes -m command -a "cat /etc/redhat-release" 172.16.60.213 | CHANGED | rc=0 >> CentOS Linux release 7.5.1804 (Core) 172.16.60.212 | CHANGED | rc=0 >> CentOS Linux release 7.5.1804 (Core)
Ansible内置变量
验证ansible, ansible是经过ssh端口探测通讯
[root@ansible-server ~]# cd /etc/ansible/ [root@ansible-server ansible]# cp hosts hosts.bak [root@ansible-server ansible]# vim hosts #以下, 这里设置了两组管理清单 ........... #可使用受控节点的ip [test-hosts] 172.16.60.212 172.16.60.213 172.16.60.214 #也可使用受控节点的主机名, 前提是主控节点要能跟受控节点的主机名正常通讯 [web-nodes] 172.16.60.212 172.16.60.213 #ansible-node03 #前提是要能ping通这个主机名, 即作好/etc/hosts主机名映射 简单测试,可设置是否生效 查看ansible清单里全部节点的uptime状况 [root@ansible-server ansible]# ansible all -m command -a "uptime" 172.16.60.213 | CHANGED | rc=0 >> 01:43:15 up 13 days, 22:19, 3 users, load average: 0.00, 0.01, 0.05 172.16.60.214 | CHANGED | rc=0 >> 01:43:15 up 13 days, 22:19, 3 users, load average: 0.01, 0.03, 0.05 172.16.60.212 | CHANGED | rc=0 >> 01:43:15 up 13 days, 23:30, 3 users, load average: 0.00, 0.01, 0.05 查看ansible清单里test-hosts组的节点的主机名 [root@ansible-server ansible]# ansible test-hosts -m command -a "hostname" 172.16.60.213 | CHANGED | rc=0 >> ansible-node02 172.16.60.214 | CHANGED | rc=0 >> ansible-node03 172.16.60.212 | CHANGED | rc=0 >> ansible-node01 查看ansible清单里web-nodes组的节点的系统时间 [root@ansible-server ansible]# ansible web-nodes -m command -a "date" 172.16.60.213 | CHANGED | rc=0 >> Sun Mar 17 01:51:02 CST 2019 172.16.60.212 | CHANGED | rc=0 >> Sun Mar 17 01:51:02 CST 2019 [root@ansible-server ansible]# ansible test-hosts --list-hosts hosts (3): 172.16.60.212 172.16.60.213 172.16.60.214 [root@ansible-server ansible]# ansible web-nodes --list-hosts hosts (2): 172.16.60.212 172.16.60.213 [root@ansible-server ansible]# ansible -i /etc/ansible/hosts web-nodes -m command -a "cat /etc/redhat-release" 172.16.60.213 | CHANGED | rc=0 >> CentOS Linux release 7.5.1804 (Core) 172.16.60.212 | CHANGED | rc=0 >> CentOS Linux release 7.5.1804 (Core)
4) Ansible 经常使用模块
1. ping模块
检查指定节点机器是否还能连通,用法很简单,不涉及参数. 主机若是在线,则回复pong. 测试连通性的模块.
[root@ansible-server ~]# ansible web-nodes -m ping 172.16.60.213 | SUCCESS => { "changed": false, "ping": "pong" } 172.16.60.212 | SUCCESS => { "changed": false, "ping": "pong" } [root@ansible-server ~]# ansible -i /etc/ansible/hosts all -m ping 172.16.60.214 | SUCCESS => { "changed": false, "ping": "pong" } 172.16.60.213 | SUCCESS => { "changed": false, "ping": "pong" } 172.16.60.212 | SUCCESS => { "changed": false, "ping": "pong" } 能够得到该模块的说明 [root@ansible-server ~]# ansible-doc -v ping
2. raw模块
执行原始的命令,而不是经过模块子系统。在任何状况下,使用shell或command命令模块也是合适的。给定原始的参数直接经过配置的远程shell运行。可返回标准输出、错误输出和返回代码。此模块没有变动处理程序支持。 这个模块不须要远程系统上的Python,就像脚本模块同样。此模块也支持Windows目标。raw, shell, command三个模块都能调用对象机器上的某条指令或者某个可执行文件。raw和shell模块很像, 都支持管道; command模块不支持管道.
注意下三者之间的微妙区别, 以下会发现raw模块执行的是系统原始命令, 执行后会主动关闭到被控制节点的链接! [root@ansible-server ~]# ansible -i /etc/ansible/hosts all -m raw -a "hostname" 172.16.60.212 | CHANGED | rc=0 >> ansible-node01 Shared connection to 172.16.60.212 closed. 172.16.60.213 | CHANGED | rc=0 >> ansible-node02 Shared connection to 172.16.60.213 closed. 172.16.60.214 | CHANGED | rc=0 >> ansible-node03 Shared connection to 172.16.60.214 closed. [root@ansible-server ~]# ansible -i /etc/ansible/hosts all -m shell -a "hostname" 172.16.60.214 | CHANGED | rc=0 >> ansible-node03 172.16.60.213 | CHANGED | rc=0 >> ansible-node02 172.16.60.212 | CHANGED | rc=0 >> ansible-node01 [root@ansible-server ~]# ansible -i /etc/ansible/hosts all -m command -a "hostname" 172.16.60.214 | CHANGED | rc=0 >> ansible-node03 172.16.60.213 | CHANGED | rc=0 >> ansible-node02 172.16.60.212 | CHANGED | rc=0 >> ansible-node01 raw和shell模块支持管道, command模块不支持管道 [root@ansible-server ~]# ansible -i /etc/ansible/hosts all -m raw -a "cat /etc/passwd|grep kevin" 172.16.60.213 | CHANGED | rc=0 >> kevin:x:1001:1001::/home/kevin:/bin/bash Shared connection to 172.16.60.213 closed. 172.16.60.212 | CHANGED | rc=0 >> kevin:x:1003:1003::/home/kevin:/bin/bash Shared connection to 172.16.60.212 closed. 172.16.60.214 | CHANGED | rc=0 >> kevin:x:1000:1000::/home/kevin:/bin/bash Shared connection to 172.16.60.214 closed. [root@ansible-server ~]# ansible -i /etc/ansible/hosts all -m shell -a "cat /etc/passwd|grep kevin" 172.16.60.213 | CHANGED | rc=0 >> kevin:x:1001:1001::/home/kevin:/bin/bash 172.16.60.214 | CHANGED | rc=0 >> kevin:x:1000:1000::/home/kevin:/bin/bash 172.16.60.212 | CHANGED | rc=0 >> kevin:x:1003:1003::/home/kevin:/bin/bash [root@ansible-server ~]# ansible -i /etc/ansible/hosts all -m command -a "cat /etc/passwd|grep kevin" 172.16.60.213 | FAILED | rc=1 >> cat: /etc/passwd|grep: No such file or directory cat: kevin: No such file or directorynon-zero return code 172.16.60.212 | FAILED | rc=1 >> cat: /etc/passwd|grep: No such file or directory cat: kevin: No such file or directorynon-zero return code 172.16.60.214 | FAILED | rc=1 >> cat: /etc/passwd|grep: No such file or directory cat: kevin: No such file or directorynon-zero return code
3. yum模块
这个模块是RedHat 和 CentOS做为远端受控节点OS的时候,用的最多的模块, 是RedHat / CentOS包管理工具的模块, 使用`yum’软件包管理器管理软件包,其选项有:
config_file:yum的配置文件 (optional)
disable_gpg_check:关闭gpg_check (optional)
disablerepo:不启用某个源 (optional)
enablerepo:启用某个源(optional)
name:要进行操做的软件包的名字,默认最新的程序包,指明要安装的程序包,能够带上版本号,也能够传递一个url或者一个本地的rpm包的路径
state:表示是安装仍是卸载的状态, 其中present、installed、latest 表示安装, absent 、removed表示卸载删除; present默认状态, laster表示安装最新版本.
舒适提示: 要确保受控节点的python版本对应正确, 不然执行下面命令会报错 (下面报错说明受控节点须要python2版本, 而当前是python3): "msg": "The Python 2 bindings for rpm are needed for this module. If you require Python 3 support use the `dnf` Ansible module instead.. The Python 2 yum module is needed for this module. If you require Python 3 support use the `dnf` Ansible module instead." 安装httpd [root@ansible-server ~]# ansible web-nodes -m yum -a 'name=httpd state=latest' [root@ansible-server ~]# ansible web-nodes -m yum -a 'name=httpd state=present' [root@ansible-server ~]# ansible web-nodes -m yum -a 'name=httpd state=installed' [root@ansible-server ~]# ansible web-nodes -m yum -a 'name=http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present' [root@ansible-server ~]# ansible web-nodes -m yum -a 'name="@Development tools" state=present' 卸载httpd [root@ansible-server ~]# ansible web-nodes -m yum -a 'name=httpd state=absent' [root@ansible-server ~]# ansible web-nodes -m yum -a 'name=httpd state=removed'
4. apt模块
这个模块是ubuntu做为远端受控节点OS的时候,用的最多的模块。Apt是Ubuntu/Debian的包管理工具。
deb: 用于安装远程机器上的.deb后缀的软件包(optional)
install_recommends: 这个参数能够控制远程电脑上是否只是下载软件包,仍是下载后安装,默认参数为true,设置为false的时候只下载软件包,不安装
update_cache: 当这个参数为yes的时候等于apt-get update(optional)
name: apt要下载的软件包名字,支持name=git=1.6 这种制定版本的模式
state: 状态(present,absent,latest),表示是安装仍是卸载. 其中present、installed、latest 表示安装, absent 、removed表示卸载删除; present默认状态, laster表示安装最新版本.
在安装foo软件包前更新而后安装foo # ansible web-nodes -m apt -a 'name=foo update_cache=yes' 移除foo软件包 # ansible web-nodes -m apt -a 'name=foo state=absent' 安装foo软件包 # ansible web-nodes -m apt -a 'name=foo state=present' 安装foo 1.0软件包 # ansible web-nodes -m apt -a 'name=foo=1.00 state=present' 安装nginx最新的名字为squeeze-backport发布包,而且安装前执行更新 # ansible web-nodes -m apt -a 'name=nginx state=latest default_release=squeeze-backports update_cache=yes' 只下载openjdk-6-jdk最新的软件包,不安装 # ansible web-nodes -m apt -a 'name=openjdk-6-jdk state=latest install_recommends=no' 安装全部软件包到最新版本 # ansible web-nodes -m apt -a 'upgrade=dist' 更新apt-get的list # ansible web-nodes -m apt -a 'update_cache=yes' 3600秒后中止update_cache # ansible web-nodes -m apt -a 'update_cache=yes cache_valid_time=3600' 安装远程节点上的/tmp/mypackage.deb软件包 # ansible web-nodes -m apt -a 'deb=/tmp/mypackage.deb'
5. pip 模块
用于管理Python库依赖项,为了使用pip模块,必须提供参数name或者requirements
chdir: 执行pip命令前cd进入的目录
name:要安装的Python库的名称或远程包的URL。
requirements:一个pip requirements.txt文件的路径,它应该是远程系统的本地文件,若是使用chdir选项,则能够将文件指定为相对路径。
version:指定的Python库的安装版本。
extra_args:额外的参数传递给pip (后面使用双引号)。
executable:显式可执行文件或可执行文件的路径名,用于为系统中安装的特定版本的Python运行pip。 例如pip-3.3,若是系统中安装了Python 2.7和3.3,而且想要为Python 3.3安装运行pip。 它不能与“virtualenv”参数一块儿指定(在2.1中添加)。 默认状况下,它将采用适用于python解释器的版本。 pip3在python 3上,pip2或pip在python 2上。
virtualenv:要安装到的virtualenv目录的可选路径。 它不能与’executable’参数一块儿指定(在2.1中添加)。 若是virtualenv不存在,则将在安装软件包以前建立它。 可选的virtualenv_site_packages,virtualenv_command和virtualenv_python选项会影响virtualenv的建立。
virtualenv_command:用于建立虚拟环境的命令或路径名。 例如pyvenv,virtualenv,virtualenv2,~/bin /virtualenv,/usr/local/bin/virtualenv。
virtualenv_python:用于建立虚拟环境的Python可执行文件。 例如python3.5,python2.7。 未指定时,将使用用于运行ansible模块的Python版本。 当virtualenv_command使用pyvenv或-m venv模块时,不该使用此参数。
state:状态(present,absent,latest, forcereinstall),表示是安装仍是卸载的状态. 其中present表示默认安装; lastest表示最新版本安装; absent表示卸载和删除; forcereinstall表示强制从新安装, "forcereinstall"选项仅适用于可ansible 2.1及更高版本.
安装bottle python包 [root@ansible-server ~]# ansible web-nodes -m pip -a 'name=bottle' [root@ansible-server ~]# ansible web-nodes -m pip -a 'name=bottle state=present' #默认的present状态能够省略不写 卸载bottle python包 [root@ansible-server ~]# ansible web-nodes -m pip -a 'name=bottle state=absent' 在0.4.6版安装bottle python包 [root@ansible-server ~]# ansible web-nodes -m pip -a 'name=bottle version=0.4.6 state=present' 使用远程协议(bzr +,hg +,git +,svn +)安装MyApp。 没必要在extra_args中提供'-e'选项。 [root@ansible-server ~]# ansible web-nodes -m pip -a 'name=svn+http://myrepo/svn/MyApp#egg=MyApp' 使用远程协议(bzr +,hg +,git +)安装MyApp。 [root@ansible-server ~]# ansible web-nodes -m pip -a 'name=git+http://myrepo/app/MyApp' 从本地压缩包安装MyApp [root@ansible-server ~]# ansible web-nodes -m pip -a 'name=file:///path/to/MyApp.tar.gz' 将bottle安装到指定的virtualenv中,继承全局安装的模块 [root@ansible-server ~]# ansible web-nodes -m pip -a 'name=bottle virtualenv=/my_app/venv virtualenv_site_packages=yes' 使用Python 2.7将bottle安装到指定的virtualenv中 [root@ansible-server ~]# ansible web-nodes -m pip -a 'name=bottle virtualenv=/my_app/venv virtualenv_command=virtualenv-2.7' 在用户主目录中安装bottle (extra_args后面使用双引号) [root@ansible-server ~]# ansible web-nodes -m pip -a 'name=bottle extra_args="--user"' 安装指定的python requirements [root@ansible-server ~]# ansible web-nodes -m pip -a 'requirements=/my_app/requirements.txt' 在指定的virtualenv中安装指定的python requirements [root@ansible-server ~]# ansible web-nodes -m pip -a 'requirements=/my_app/requirements.txt virtualenv=/my_app/venv' 安装指定的python requirements和自定义pip源URL [root@ansible-server ~]# ansible web-nodes -m pip -a 'requirements=/my_app/requirements.txt extra_args="-i https://example.com/pypi/simple"' 专门为Python 3.3安装bottle,使用'pip-3.3'可执行文件 [root@ansible-server ~]# ansible web-nodes -m pip -a 'name=bottle executable=pip-3.3' 安装 bottle,若是已安装,强制从新安装 [root@ansible-server ~]# ansible web-nodes -m pip -a 'name=bottle state=forcereinstall'
6. synchronize 模块
这个模块是使用rsync同步文件,将主控方目录推送到指定受控节点的目录下。其参数以下:
delete: 删除不存在的文件,delete=yes 使两边的内容同样(即以推送方为主),默认no
src: 要同步到目的地的源主机上的路径; 路径能够是绝对的或相对的。若是路径使用”/”来结尾,则只复制目录里的内容,若是没有使用”/”来结尾,则包含目录在内的整个内容所有复制
dest:目的地主机上将与源同步的路径; 路径能够是绝对的或相对的。
dest_port:默认目录主机上的端口 ,默认是22,走的ssh协议。
mode: push或pull,默认push,通常用于从本机向远程主机上传文件,pull 模式用于从远程主机上取文件。
rsync_opts:经过传递数组来指定其余rsync选项。
将主控节点上/data/kevin目录同步到受控节点的/home目录下 [root@ansible-server ~]# ansible web-nodes -m synchronize -a 'src=/data/kevin dest=/home' 将主控节点上/data/kevin/test.file文件同步到受控节点的/opt目录下 [root@ansible-server ~]# ansible web-nodes -m synchronize -a 'src=/data/kevin/test.file dest=/opt' 将主控节点上/data/kevin/test.file文件同步到受控节点的/root/bobo.file文件 [root@ansible-server ~]# ansible web-nodes -m synchronize -a 'src=/data/kevin/test.file dest=/root/bobo.file' 将主控节点上/opt/同步到受控节点的/opt/目录, 使受控节点保持和主控节点的opt目录一致, 不同的就删除! 默认delete=no [root@ansible-server ~]# ansible web-nodes -m synchronize -a 'src=/opt/ dest=/opt/ delete=yes' 注意命令中要加"/", 若是不加"/", 则主控节点的opt目录就同步到受控节点的/opt目录下即, 即/opt/opt [root@ansible-server ~]# ansible web-nodes -m synchronize -a 'src=/opt dest=/opt delete=yes' 将主控节点上/data/kevin目录同步到受控节点的/mnt/www目录下. 可是bobo目录排除在外! rsync_opts能够进行屡次传递. [root@ansible-server ~]# ansible web-nodes -m synchronize -a 'src=/data/kevin/ dest=/mnt/www/ rsync_opts="--no-motd" rsync_opts="--exclude=bobo"' 强制两边同步保持一致! 跟主控节点源目录保持一致! [root@ansible-server ~]# ansible web-nodes -m synchronize -a 'src=/data/kevin/ dest=/mnt/www/ rsync_opts="--no-motd" rsync_opts="--exclude=bobo" delete=yes' 从远程受控节点上将/usr/local/src/grace.file文件拉取到主控节点的/root目录下 [root@ansible-server ~]# ansible web-nodes -m synchronize -a 'src=/usr/local/src/grace.file dest=/root/ rsync_opts="-avpgolr" mode=pull '
synchronize 模块相比copy模块来讲, 同步速度增长不少.
7. template 模块
基于模板方式生成一个文件复制到远程主机(template使用Jinjia2格式做为文件模版,进行文档内变量的替换的模块。它的每次使用都会被ansible标记为”changed”状态。)
backup: 若是原目标文件存在,则先备份目标文件
src: 在ansible控制器上的Jinja2格式化模板的路径。 这能够是相对或绝对的路径。
dest: 将模板渲染到远程机器上的位置。
force: 是否强制覆盖,默认为yes
owner: 目标文件属主
group: 目标文件属组
mode: 目标文件的权限模式,模式能够被指定为符号模式(例如,u + rwx或u = rw,g = r,o = r)
[root@ansible-server ~]# ansible web-nodes -m template -a "src=/mytemplates/foo.j2 dest=/etc/file.conf owner=bin group=wheel mode=0644" 一样的例子,但使用等效于0644的符号模式 [root@ansible-server ~]# ansible web-nodes -m template -a "src=/mytemplates/foo.j2 dest=/etc/file.conf owner=bin group=wheel mode="u=rw,g=r,o=r"" 具体用法, 可参考playbook剧本写法.
8. copy 模块
把主控节点本地的文件上传同步到远程受控节点上, 该模块不支持从远程受控节点拉取文件到主控节点上。 参数选项以下:
src:指定源文件路径,能够是相对路径,也能够是绝对路径,能够是目录(并不是是必须的,可使用content,直接生成文件内容). src便是要复制到远程主机的文件在本地的地址,能够是绝对路径,也能够是相对路径。若是路径是一个目录,它将递归复制。在这种状况下,若是路径使用”/”来结尾,则只复制目录里的内容,若是没有使用”/”来结尾,则包含目录在内的整个内容所有复制,相似于rsync。
dest:指定目标文件路径,只能是绝对路径,若是src是目录,此项必须是目录. 这个是必选项!
owner:指定属主;
group:指定属组;
mode:指定权限,能够以数字指定好比0644;
content:代替src,直接往dest文件中写内容,能够引用变量,也能够直接使用inventory中的主机变量. 写后会覆盖原文件内容!
backup:在覆盖以前将原文件备份,备份文件包含时间信息。有两个选项:yes|no
force: 若是目标主机包含该文件,但内容不一样,若是设置为yes,则强制覆盖,若是为no,则只有当目标主机的目标位置不存在该文件时,才复制。默认为yes ;
directory_mode:递归的设定目录的权限,默认为系统默认权限;
others:全部的file模块里的选项均可以在这里使用;
特别注意: src和content不能同时使用!!!!
[root@ansible-server ~]# ansible web-nodes -m copy -a 'src=/data/kevin/ dest=/mnt/www/ backup=yes' [root@ansible-server ~]# ansible web-nodes -m copy -a 'src=/etc/hosts dest=/mnt/www/ owner=root group=root mode=0644' [root@ansible-server ~]# ansible web-nodes -m copy -a 'src=/etc/hosts dest=/mnt/www/ owner=root group=root mode="u=rw,g=r,o=r"' 向远程受控节点的/mnt/www/test.file文件写入内容, 把原内容覆盖掉 [root@ansible-server ~]# ansible web-nodes -m copy -a 'content="\nMy age is 26" dest=/mnt/www/test.file'
9. user 模块 与 group 模块
user模块是请求的是useradd, userdel, usermod三个指令,goup模块请求的是groupadd, groupdel, groupmod 三个指令。
user模块
home: 指定用户的家目录, 须要与createhome配合使用。
groups: 指定用户的属组。
uid: 指定用的uid。
password: 设定用户密码, password参数须要接受md5加密后的值. 特别注意: 指定password参数时, 不能使用明文密码, 由于后面这一串密码会被直接传送到被管理主机的/etc/shadow文件中, 因此须要先将密码字符串进行加密处理, 而后将获得的字符串放到password中便可。
name: 指定用户名。
system: 是否为系统用户。 表示默认建立为普通用户, 而非系统用户, 指定是用yes. 也就是说yes是默认建立为普通用户, 而非系统用户;
update_password: 修改用户密码, 其中always: 新密码和旧密码不一样时进行修改; on_create: 为新建立的用户指定密码.
createhome: 建立家目录, 其中yes表示默认项, 即建立用户默认是有家目录的; no表示建立用户时不建立家目录.
remove: 其中yes是删除用户家目录, 须要指定此参数; no是默认项, 删除用户时默认不删除用户的家目录. 当state=absent时, remove=yes则表示连同家目录一块儿删除, 等价于userdel -r。
state: 用户状态是建立仍是删除. (present, absent) ;默认为present; 其中present表示添加用户; absent表示删除用户
shell: 指定用户的shell环境。
generate_ssh_key: 是否为相关用户生成SSH密钥。 这不会覆盖现有的SSH密钥。
ssh_key_bits: 可选择指定要建立的SSH密钥中的位数。
ssh_key_passphrase: 设置SSH密钥的密码。 若是没有提供密码, SSH密钥将默认没有密码。
ssh_key_file: 指定SSH密钥文件名(可选). 若是这是一个相对的文件名, 那么它将是相对于用户的主目录。
ssh_key_type: 指定要生成的SSH密钥的类型(可选). 可用的SSH密钥类型将取决于目标主机上的实现。
使用bash shell添加用户haha,将组"管理员"和"开发人员"附加到用户组 [root@ansible-server ~]# ansible web-nodes -m user -a "name=haha shell=/bin/bash groups=admins,developers append=yes" 增长用户anhui (先对密码作明文到密文的处理) [root@ansible-server ~]# echo "anhui@123" | openssl passwd -1 -stdin $1$rj74jCVy$NDW80bgY0DUTuHUlSunVv1 [root@ansible-server ~]# ansible web-nodes -m user -a 'name=anhui system=yes password=$1$rj74jCVy$NDW80bgY0DUTuHUlSunVv1 state=present' 删除用户anhui [root@ansible-server ~]# ansible web-nodes -m user -a 'name=anhui remove=yes state=absent' 更新用户kevin的密码 (先对新密码kevin@bj123作明文到密文的处理) [root@ansible-server ~]# echo "kevin@bj123"| openssl passwd -1 -stdin $1$5X5bH5.J$RwE6o6g6bX953W7vAaizv/ [root@ansible-server ~]# ansible web-nodes -m user -a 'name=kevin update_password=always password=$1$5X5bH5.J$RwE6o6g6bX953W7vAaizv/' 在~/.ssh/id_rsa中为用户bobo建立一个2048位的SSH密钥 [root@ansible-server ~]# ansible web-nodes -m user -a 'name=bobo generate_ssh_key=yes ssh_key_bits=2048 ssh_key_file=.ssh/id_rsa' [root@ansible-server ~]# ansible web-nodes -m group -a "name=yang gid=888" [root@ansible-server ~]# ansible web-nodes -m user -a "name=yang uid=888 group=888 shell=/sbin/nologin create_home=no"
group模块
gid: 指定用的gid。
name: 指定用户名。
state: 是建立仍是删除, (present,absent);
system: 若是是,则表示建立的组是系统组;
[root@ansible-server ~]# ansible web-nodes -m group -a "name=huoqiu state=present" [root@ansible-server ~]# ansible web-nodes -m group -a "name=chenzun gid=897 state=present" [root@ansible-server ~]# ansible web-nodes -m group -a "name=huoqiu state=absent"
10. service 模块
管理服务的模块, service管理的服务必须是yum安装的服务, 即默认的系统服务脚本. 编译安装的服务很差使用service模块管理.
name: 服务名称;
state: started/stopped/restarted/reloaded;
enabled: true/false;
runlevel: 运行级别;
sleep: 若是执行了restarted,在stop和start之间沉睡几秒;
arguments: 给命令行提供一些选项;
[root@ansible-server ~]# ansible web-nodes -m service -a "name=sshd state=restarted" [root@ansible-server ~]# ansible web-nodes -m service -a "enabled=on name=httpd state=started" [root@ansible-server ~]# ansible web-nodes -m service -a "enabled=yes name=httpd state=started" [root@ansible-server ~]# ansible web-nodes -m service -a "name=httpd state=reloaded" [root@ansible-server ~]# ansible web-nodes -m service -a "name=httpd state=stopped"
11. get_url 模块
该模块主要用于从http、ftp、https服务器上下载文件(相似于wget), 主要有以下选项:
sha256sum: 下载完成后进行sha256 check;
timeout: 下载超时时间, 默认10s
url: 下载的URL
url_password、url_username: 主要用于须要用户名密码进行验证的状况
dest: 将文件下载到哪里的绝对路径。若是dest是目录, 则使用服务器提供的文件名, 或者若是没有提供, 将使用远程服务器上的URL的基本名称。
headers: 以格式“key: value, key: value”为请求添加自定义HTTP标头
[root@ansible-server ~]# ansible web-nodes -m get_url -a "url=http://lan.Okay686.com/RDPWrap-v1.6.1.zip dest=/usr/local/src/" [root@ansible-server ~]# ansible web-nodes -m get_url -a "url=http://10.0.8.50/ops.sh dest=/opt/shell.sh mode=0440" [root@ansible-server ~]# ansible web-nodes -m get_url -a 'url=http://10.0.8.50/ops.sh dest=/opt/shell.sh headers="key:value,key:value"' [root@ansible-server ~]# ansible web-nodes -m get_url -a 'url=http://10.0.8.50/ops.sh dest=/opt/shell.sh checksum=sha256:b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c'
12. fetch 模块 (从远程拉取出来文件)
它用于从远程机器获取文件,并将其本地存储在由主机名组织的文件树中。
src: 指定从远程控制节点上要拉取的文件, 记住: 这个只能拉取文件!! 不能拉取目录!!! 后续版本可能会支持递归提取。
dest: 将远程节点拉取的文件保存到本地的目录路径. 例如,若是dest目录是/backup,在主机host.example.com上命名为/ etc/profile的src文件将被保存到/backup/host.example.com/etc/profile。
flat: 容许覆盖将目标文件添加到主机名/path/to/file的默认行为。默认为no!! 若是设置为yes, 将不会显示相似172.16.60.221/root/信息. (即在dest的本机存放目录下不会建立远程节点ip命名的目录)
将web-nodes组内的远程节点的/root/bobo.file文件拉取到主控节点的/tmp下 [root@ansible-server ~]# ansible web-nodes -m fetch -a "dest=/mnt/ src=/root/bobo.file" 拉取过来后, 会发现dest定义的文件存放目录下会有以远程节点ip命名的目录 [root@ansible-server ~]# ls /mnt/ 172.16.60.212 172.16.60.213 [root@ansible-server ~]# ls /mnt/172.16.60.212/root/ bobo.file [root@ansible-server ~]# ls /mnt/172.16.60.213/root/ bobo.file flat参数,拉取的时候会自动建立设置的dest中不存在的目录(同名文件会覆盖): [root@ansible-server ~]# ansible web-nodes -m fetch -a "dest=/mnt/wangshibo/ src=/root/bobo.file" 172.16.60.213 | CHANGED => { "changed": true, "checksum": "804d716fc5844f1cc5516c8f0be7a480517fdea2", "dest": "/mnt/wangshibo/172.16.60.213/root/bobo.file", "md5sum": "4221d002ceb5d3c9e9137e495ceaa647", "remote_checksum": "804d716fc5844f1cc5516c8f0be7a480517fdea2", "remote_md5sum": null } 172.16.60.212 | CHANGED => { "changed": true, "checksum": "804d716fc5844f1cc5516c8f0be7a480517fdea2", "dest": "/mnt/wangshibo/172.16.60.212/root/bobo.file", "md5sum": "4221d002ceb5d3c9e9137e495ceaa647", "remote_checksum": "804d716fc5844f1cc5516c8f0be7a480517fdea2", "remote_md5sum": null } [root@ansible-server ~]# ls /mnt/ 172.16.60.212 172.16.60.213 wangshibo [root@ansible-server ~]# ls /mnt/wangshibo/ 172.16.60.212 172.16.60.213 [root@ansible-server ~]# ls /mnt/ 172.16.60.212 172.16.60.213 wangshibo [root@ansible-server ~]# ls /mnt/wangshibo/ 172.16.60.212 172.16.60.213 [root@ansible-server ~]# ls /mnt/wangshibo/172.16.60.212/root/ bobo.file [root@ansible-server ~]# ls /mnt/wangshibo/172.16.60.213/root/ bobo.file 若是不想要从远程节点拉取过来的文件在本机存放目录里已远程节点ip命名的目录里面, 即去掉上面的/mnt里面的172.16.60.212和172.16.60.213目录 [root@ansible-server ~]# ansible web-nodes -m fetch -a "dest=/mnt/kevin/ src=/root/bobo.file flat=yes" 172.16.60.213 | CHANGED => { "changed": true, "checksum": "804d716fc5844f1cc5516c8f0be7a480517fdea2", "dest": "/mnt/kevin/bobo.file", "md5sum": "4221d002ceb5d3c9e9137e495ceaa647", "remote_checksum": "804d716fc5844f1cc5516c8f0be7a480517fdea2", "remote_md5sum": null } 172.16.60.212 | CHANGED => { "changed": true, "checksum": "804d716fc5844f1cc5516c8f0be7a480517fdea2", "dest": "/mnt/kevin/bobo.file", "md5sum": "4221d002ceb5d3c9e9137e495ceaa647", "remote_checksum": "804d716fc5844f1cc5516c8f0be7a480517fdea2", "remote_md5sum": null } [root@ansible-server ~]# ls /mnt/ 172.16.60.212 172.16.60.213 kevin wangshibo [root@ansible-server ~]# ls /mnt/kevin/ bobo.file
13. file 模块
file模块主要用于远程主机上的文件操做,file模块包含以下选项:
force: 须要在两种状况下强制建立软连接,一种是源文件不存在但以后会创建的状况下;另外一种是目标软连接已存在,须要先取消以前的软链,而后建立新的软链,有两个选项: yes|no
group: 定义文件/目录的属组
mode: 定义文件/目录的权限
owner: 定义文件/目录的属主
path: 必选项,定义文件/目录的路径
recurse: 递归的设置文件的属性,只对目录有效
src: 要被连接的源文件的路径,只应用于state=link的状况
dest: 被连接到的路径,只应用于state=link的状况
state: 表示file的状态, 主要分为:
directory: 若是目录不存在,建立目录;
file: 即便文件不存在,也不会被建立;
link: 建立软连接;
hard: 建立硬连接;
touch: 若是文件不存在,则会建立一个新的文件,若是文件或目录已存在,则更新其最后修改时间;
absent: 删除目录、文件或者取消连接文件;
[root@ansible-server ~]# ansible web-nodes -m file -a "path=/data/test state=touch mode="600" owner=kevin group=kevin" [root@ansible-server ~]# ansible web-nodes -m file -a "path=/data/haha state=directory mode="755" owner=kevin group=kevin" [root@ansible-server ~]# ansible web-nodes -m file -a "path=/data/test state=absent" [root@ansible-server ~]# ansible web-nodes -m file -a "path=/data/test state=touch mode="u+rw,g-wx,o-rwx" owner=kevin group=kevin" 文件的软连接和硬连接 [root@ansible-server ~]# ansible web-nodes -m file -a "src=/data/test dest=/opt/heihei state=link" [root@ansible-server ~]# ansible web-nodes -m file -a "src=/data/test dest=/mnt/hh state=hard" [root@ansible-server ~]# ansible web-nodes -m file -a "src=/etc/passwd dest=/opt/heihei state=link force=yes" 目录作软连接 [root@ansible-server ~]# ansible web-nodes -m file -a "src=/data/haha/ dest=/root/aaa state=link"
14. lineinfile 模块
用于对远程受控节点的文件编辑模块. 主要选项有:
path: 指定要修改的配置文件, 包括:
regexp:匹配要修改的内容
line:要增长或者修改的内容
state: 状态, 包括:
absent:表示删除,当匹配到时进行删除
present:表示增长,当匹配到时进行修改,当没有匹配到时在最后增长一行,默认为此项
backrefs: 该参数值包括:
no:表示若是没有匹配到,则增长line;若是匹配成功,则替换line;
yes:表示若是没有匹配到,则不变line;若是匹配成功,则替换line;
backup: 该参数值包括:
no:表示若是没有匹配到,则增长line;若是匹配成功,则替换line;不备份原文件
yes:表示若是没有匹配到,则增长line;若是匹配成功,则替换line;备份原文件
insertafter(匹配的是此行): 在匹配到的行以后添加一行. (经测试, 发现是匹配到的行的最后一行的后面添加一行)
insertbefore(匹配的是此行): 在匹配到的行以前添加一行. (经测试, 发现是匹配到的行的最后一行的前面添加一行)
舒适提示: 经测试,当不添加backerfs: yes参数时,匹配到后也会进行替换,但当匹配到的内容不存在时,会在最后增长一行;因此当不增长backerfs参数时,要肯定匹配到的内容存在;
将远程受控节点的/data/test文件中的"123"字段修改成"wangshibo" [root@ansible-server ~]# ansible web-nodes -m lineinfile -a 'path=/data/test regexp="123" line="wangshibo" backrefs=no' [root@ansible-server ~]# ansible web-nodes -m lineinfile -a 'path=/etc/sudoers regexp="SYSTEM,SOFTWARE" line="STAPLES_ADMIN ALL=(ROOT) NOPASSWD:NETWORKING,LOCATE,STORAGE,DELEGATING,DRIVERS,SYSTEM,SOFTWARE,SERVICES,PROCESSES,FILE" backrefs=no' 匹配到的行后增长一行 [root@ansible-server ~]# ansible web-nodes -m lineinfile -a 'dest=/data/test insertafter="wangshibo" line="huihui"' 匹配到的行前增长一行 [root@ansible-server ~]# ansible web-nodes -m lineinfile -a 'dest=/data/test insertbefore="root" line="huihui"' 删除匹配到的行: [root@ansible-server ~]# ansible web-nodes -m lineinfile -a 'path=/data/test regexp="123" state=absent'
15. unarchive模块
用于解压文件,模块包含以下选项:
copy: 在解压文件以前,是否先将文件复制到远程主机,默认为yes。若为no,则要求目标主机上压缩包必须存在。
creates: 指定一个文件名,当该文件存在时,则解压指令不执行
dest: 远程主机上的一个路径,即文件解压的绝对路径。 必须是一个目录路径!
group: 解压后的目录或文件的属组;
list_files: 若是为yes,则会列出压缩包里的文件,默认为no,2.0版本新增的选项;
mode: 解压后文件的权限;
src: 若是copy为yes,则须要指定压缩文件的源路径;
owner: 解压后文件或目录的属主;
经测试, 该模块解压时识别zip, tgz , tar.gz, tar.bz2 格式的压缩包, gzip格式的测试时不支持.
将/usr/local/src/nginx.tgz解压缩到/data/www中 [root@ansible-server ~]# ansible web-nodes -m unarchive -a "src=/usr/local/src/nginx.tgz dest=/data/www/" 解压远程受控节点上已存在的文件 [root@ansible-server ~]# ansible web-nodes -m unarchive -a "src=/root/file.zip dest=/usr/local remote_src=yes" 解压文档须要下载的文件(2.0中添加) [root@ansible-server ~]# ansible web-nodes -m unarchive -a "src=https://example.com/example.zip dest=/usr/local/bin remote_src=yes"
16. command模块 和 shell模块
用于在各被管理节点运行指定的命令. shell和command的区别:shell模块能够特殊字符,而command是不支持!!
command 是ansible的默认模块, 即不指定模块的时候默认使用的模块就是command ! 使用ansible自带模块执行命令 若是要用 > < | & ‘ ‘ 使用shell 模块, command是不支持管道符之类的。
可使用"ansible-doc -s shell" 或 "ansible-doc -s command" 查看下shell和command的参数, 二者之间的参数用法上基本同样的.
[root@ansible-server ~]# ansible-doc -s shell - name: Execute commands in nodes. shell: chdir: # 在执行命令以前,先cd到指定的目录下 creates: # 用于判断命令是否要执行,若是指定的文件存在(可使用通配符)存在,则不执行 executable: # 再也不使⽤默认的/bin/sh解析并执⾏命令,⽽是使⽤此处指定的命令解析(例如使⽤expect解析expect脚本。必须为绝对路径) free_form: # 默认的选项,这里只是显示,其实是没有的 removes: # 用于判断命令是否要执行,若是指定的文件存在(可使用通配符)不存在,则不执行 stdin: # 将命令的stdin直接设置为指定值 warn: # 设置command的警告信息(在/etc/ansible/ansible.cfg中有配置项)
示例以下:
[root@ansible-server ~]# ansible all -a "date" 172.16.60.214 | CHANGED | rc=0 >> Sun Mar 17 15:24:55 CST 2019 172.16.60.212 | CHANGED | rc=0 >> Sun Mar 17 15:24:55 CST 2019 172.16.60.213 | CHANGED | rc=0 >> Sun Mar 17 15:24:55 CST 2019 [root@ansible-server ~]# ansible all -m command -a "date" 172.16.60.213 | CHANGED | rc=0 >> Sun Mar 17 15:25:14 CST 2019 172.16.60.212 | CHANGED | rc=0 >> Sun Mar 17 15:25:14 CST 2019 172.16.60.214 | CHANGED | rc=0 >> Sun Mar 17 15:25:14 CST 2019 在命令执行以前, 先切换到指定的目录路径下 [root@ansible-server ~]# ansible web-nodes -m command -a "chdir=/tmp pwd" 172.16.60.213 | CHANGED | rc=0 >> /tmp 172.16.60.212 | CHANGED | rc=0 >> /tmp creates 文件是否存在,不存在就执行命令 [root@ansible-server ~]# ansible web-nodes -m command -a "creates=/etc/hosts date" 172.16.60.212 | SUCCESS | rc=0 >> skipped, since /etc/hosts exists 172.16.60.213 | SUCCESS | rc=0 >> skipped, since /etc/hosts exists [root@ansible-server ~]# ansible web-nodes -m command -a "creates=/etc/hosts1231 date" 172.16.60.213 | CHANGED | rc=0 >> Sun Mar 17 15:26:40 CST 2019 172.16.60.212 | CHANGED | rc=0 >> Sun Mar 17 15:26:40 CST 2019 removes 文件是否存在,不存在就不执行命令, [root@ansible-server ~]# ansible web-nodes -m command -a "removes=/etc/hosts uptime" 172.16.60.213 | CHANGED | rc=0 >> 15:33:01 up 14 days, 12:08, 2 users, load average: 0.13, 0.04, 0.05 172.16.60.212 | CHANGED | rc=0 >> 15:33:01 up 14 days, 13:20, 2 users, load average: 0.06, 0.03, 0.05 [root@ansible-server ~]# ansible web-nodes -m command -a "removes=/etc/hosts1231 uptime" 172.16.60.213 | SUCCESS | rc=0 >> skipped, since /etc/hosts1231 does not exist 172.16.60.212 | SUCCESS | rc=0 >> skipped, since /etc/hosts1231 does not exist
shell 模块:这是个万能模块, 执行linux命令时能够用远程节点执行命令;
注意: shell 模块在远程执行脚本时,远程主机上必定要有相应的脚本;
[root@ansible-server ~]# ansible web-nodes -m shell -a "/bin/bash /opt/test.sh" 172.16.60.212 | CHANGED | rc=0 >> ansible-node01 it is OK 172.16.60.213 | CHANGED | rc=0 >> ansible-node02 it is OK 下面的shell模块命令中使用了特殊字符, 若是使用comand模块则就不行了!! [root@ansible-server ~]# ansible web-nodes -m shell -a "cat /etc/passwd > /tmp/test" 使用command模块执行下面命令, 则就会报错! command模块不支持特殊字符! [root@ansible-server ~]# ansible web-nodes -m command -a "cat /etc/passwd > /tmp/test"
17. hostname模块
修改远程受控节点的主机名的模块
使用hostname模块修改远程节点的主机名时, 最好指明远程单节点ip地址进行设置主机名了. 由于不一样节点的主机名不同 [root@ansible-server ~]# ansible 172.16.60.213 -m hostname -a 'name="web-node03"' 172.16.60.213 | CHANGED => { "ansible_facts": { "ansible_domain": "", "ansible_fqdn": "web-node03", "ansible_hostname": "web-node03", "ansible_nodename": "web-node03" }, "changed": true, "name": "web-node03" }
18. script模块
在远程受控节点上执行主控节点的脚本,至关于scp+shell组合
好比在远程受控节点上执行主控节点的/opt/test.sh脚本 [root@ansible-server ~]# ansible all -m script -a "/opt/test.sh"
须要注意: 使用scripts模块,不用将脚本传输到远程节点,脚本自己不用进行受权,便可利用script模块执行。直接执行脚本便可,不须要使用sh 或者 /bin/bash
19. stat模块
获取远程文件的状态信息,包括atime,ctime,mtime,md5,uid,gid等信息
[root@ansible-server ~]# ansible all -m stat -a "path=/data/test"
20. cron模块
远程受控节点的crontab配置
设置指定的crontab计划任务 [root@ansible-server ~]# ansible web-nodes -m cron -a "name='test' hour='2-5' minute='*/5' day='1' month='3,4' weekday='1' job='ls -l' user=root" 172.16.60.212 | CHANGED => { "changed": true, "envs": [], "jobs": [ "test" ] } 172.16.60.213 | CHANGED => { "changed": true, "envs": [], "jobs": [ "test" ] } 而后到172.16.60.212和172.16.60.213受控节点上查看: [root@ansible-node02 ~]# crontab -l #Ansible: test */5 2-5 1 3,4 1 ls -l 删除指定crontab [root@ansible-server ~]# ansible web-nodes -m cron -a "name='test' state=absent" ====================================================== 使用ansible添加一条定时任务 [root@ansible-server ~]# ansible web-nodes -m cron -a "minute=* hour=* day=* month=* weekday=* job='/bin/sh /server/scripts/test.sh'" 远程受控节点上查看 [root@ansible-node02 ~]# crontab -l #Ansible: None * * * * * /bin/sh /server/scripts/test.sh 设置定时任务注释信息,防止重复,name设定一个名称 [root@ansible-server ~]# ansible web-nodes -m cron -a "name='cron01' job='/bin/sh /server/scripts/test.sh'" [root@ansible-server ~]# ansible web-nodes -m cron -a "name='cron02' job='/bin/sh /server/scripts/test2.sh'" 远程受控节点上查看 [root@ansible-node02 ~]# crontab -l #Ansible: cron01 * * * * * /bin/sh /server/scripts/test.sh #Ansible: cron02 * * * * * /bin/sh /server/scripts/test2.sh 删除相应定时任务 [root@ansible-server ~]# ansible 172.16.60.212 -m cron -a "name='ansible cron02' minute=0 hour=0 job='/bin/sh /server/scripts/test.sh' state=absent" 注释相应定时任务,使定时任务失效 (disabled=yes 表示注释掉, 即无效; disabled=no 表示去掉注释, 即有效) [root@ansible-server ~]# ansible web-nodes -m cron -a "name='ansible cron01' minute=0 hour=0 job='/bin/sh /server/scripts/test.sh' disabled=yes"
21. mount模块
在远程受控节点上挂载文件系统。可用参数:
present: 开机挂载,仅将挂载配置写入/etc/fstab(不经常使用)
mounted: 挂载设备,并将配置写入/etc/fstab
unmounted: 卸载设备,不会清除/etc/fstab写入的配置
absent: 卸载设备,会清理/etc/fstab写入的配置
mount已经使用path代替了原来的name参数,可是name参数仍是可使用的.
将受控节点的/dev/sd0设备挂载到/mnt/data目录上, 文件格式为ext4, 只读属性 [root@ansible-server ~]# ansible web-nodes -m mount -a "path=/mnt/data src=/dev/sd0 fstype=ext4 ots=ro state=present" 仅将挂载的配置写入/etc/fstab,并不会执行挂载操做 [root@ansible-server ~]# ansible web-nodes -m mount -a "src=172.16.60.220:/data path=/data fstype=nfs opts=defaults state=present" 临时挂载设备,并将挂载信息写入/etc/fstab [root@ansible-server ~]# ansible web-nodes -m mount -a "src=172.16.60.220:/data path=/data fstype=nfs opts=defaults state=mounted" 临时卸载,不会清理/etc/fstab [root@ansible-server ~]# ansible web-nodes -m mount -a "src=172.16.60.220:/data path=/data fstype=nfs opts=defaults state=unmounted" 卸载,不只临时卸载,同时会清理/etc/fstab [root@ansible-server ~]# ansible web-nodes -m mount -a "src=172.16.60.220:/data path=/data fstype=nfs opts=defaults state=absent"
22. find模块
帮助在被管理的受控主机中查找符合条件的文件,就像 find 命令同样.
经常使用选项:
paths: 必须参数,指定在哪一个目录中查找文件,能够指定多个路径,路径间用逗号隔开,此参数有别名,使用别名 path 或者别名 name 能够代替 paths。
recurse: 默认状况下,只会在指定的目录中查找文件,也就是说,若是目录中还包含目录,ansible 并不会递归的进入子目录查找对应文件,若是想要递归的查找文件,须要使用 recurse 参数,当 recurse 参数设置为 yes 时,表示在指定目录中递归的查找文件。
hidden: 默认状况下,隐藏文件会被忽略,当 hidden 参数的值设置为 yes 时,才会查找隐藏文件。
file_type: 默认状况下,ansible 只会根据条件查找”文件”,并不会查找”目录”或”软连接”等文件类型,若是想要指定查找的文件类型,能够经过 file_type 指定文件类型,可指定的文件类型有 any、directory、file、link 四种。
patterns: 使用此参数指定须要查找的文件名称,支持使用 shell(好比通配符)或者正则表达式去匹配文件名称,默认状况下,使用 shell 匹配对应的文件名,若是想要使用 python 的正则去匹配文件名,须要将 use_regex 参数的值设置为 yes。
use_regex:默认状况下,find 模块不会使用正则表达式去解析 patterns 参数中对应的内容,当 use_regex 设置为 yes 时,表示使用 python 正则解析 patterns 参数中的表达式,不然,使用 glob 通配符解析 patterns 参数中的表达式。
contains:使用此参数能够根据文章内容查找文件,此参数的值为一个正则表达式,find 模块会根据对应的正则表达式匹配文件内容。
age: 使用此参数能够根据时间范围查找文件,默认以文件的 mtime 为准与指定的时间进行对比,好比,若是想要查找 mtime 在3天以前的文件,那么能够设置 age=3d,若是想要查找 mtime 在3天之内的文件,能够设置 age=-3d,这里所说的3天是按照当前时间往前推3天,可使用的单位有秒(s)、分(m)、时(h)、天(d)、星期(w)。
age_stamp: 文件的时间属性中有三个时间种类,atime、ctime、mtime,当咱们根据时间范围查找文件时,能够指定以哪一个时间种类为准,当根据时间查找文件时,默认以 mtime 为准。
size: 使用此参数能够根据文件大小查找文件,好比,若是想要查找大于3M的文件,那么能够设置 size=3m,若是想要查找小于50k的文件,能够设置 size=-50k,可使用的单位有 t、g、m、k、b。
get_checksum: 当有符合查找条件的文件被找到时,会同时返回对应文件的 sha1校验码,若是要查找的文件比较大,那么生成校验码的时间会比较长
在受控主机的 /data目录中查找文件内容中包含wang字符串的文件,隐藏文件会被忽略,不会进行递归查找。 [root@ansible-server ~]# ansible web-nodes -m find -a "paths=/data contains=".*wang.*"" 在受控主机的的 /data目录以及其子目录中查找文件内容中包含wang字符串的文件,隐藏文件会被忽略。 [root@ansible-server ~]# ansible web-nodes -m find -a "paths=/data contains=".*wang.*" recurse=yes" 在受控主机的的 /data目录中查找以 .sh 结尾的文件,包括隐藏文件,可是不包括目录或其余文件类型,不会进行递归查找。 [root@ansible-server ~]# ansible web-nodes -m find -a 'paths=/data patterns="*.sh" file_type=any hidden=yes' 在受控主机的的 /data目录中查找以 .sh 结尾的文件,只不过patterns对应的表达式为正则表达式,查找范围包括隐藏文件,包括全部文件类型, 可是不会进行递归查找,不会对/data目录的子目录进行查找。 [root@ansible-server ~]# ansible web-nodes -m find -a 'paths=/data patterns=".*\.sh" use_regex=yes file_type=any hidden=yes' 在受控主机的的 /data目录中以及其子目录中查找 mtime 在1天之内的文件,不包含隐藏文件,不包含目录或软连接文件等文件类型。 [root@ansible-server ~]# ansible web-nodes -m find -a "path=/data age=-1d recurse=yes" 在受控主机的的 /data目录中以及其子目录中查找大于 2g 的文件,不包含隐藏文件,不包含目录或软连接文件等文件类型。 [root@ansible-server ~]# ansible web-nodes -m find -a "paths=/data size=2g recurse=yes" 在受控主机的的 /data目录中以及其子目录中查找以 .sh 结尾的文件,而且返回符合条件文件的 sha1 校验码,包括隐藏文件 [root@ansible-server ~]# ansible web-nodes -m find -a "paths=/data patterns=*.sh get_checksum=yes hidden=yes recurse=yes"
23. selinux模块
管理远程受控节点的selinux的模块
[root@ansible-server ~]# ansible web-nodes -m selinux -a 'state=disabled' 172.16.60.212 | SUCCESS => { "changed": false, "configfile": "/etc/selinux/config", "msg": "", "policy": "targeted", "reboot_required": false, "state": "disabled" } 172.16.60.213 | SUCCESS => { "changed": false, "configfile": "/etc/selinux/config", "msg": "", "policy": "targeted", "reboot_required": false, "state": "disabled" }
24. setup模块
用于收集远程受控主机的一些基本信息.
filter参数:用于进行条件过滤。若是设置,仅返回匹配过滤条件的信息
不加过滤参数, 就会将受控主机的全部信息都打印出来 [root@ansible-server ~]# ansible web-nodes -m setup 获取受控主机的 IPV4 地址 [root@ansible-server ~]# ansible web-nodes -m setup -a "filter=ansible_all_ipv4_addresses" 172.16.60.212 | SUCCESS => { "ansible_facts": { "ansible_all_ipv4_addresses": [ "172.16.60.212" ] }, "changed": false } 172.16.60.213 | SUCCESS => { "ansible_facts": { "ansible_all_ipv4_addresses": [ "172.16.60.213", "172.19.0.1", "172.17.0.1" ] }, "changed": false } 获取受控主机的内存信息 [root@ansible-server ~]# ansible web-nodes -m setup -a "filter=ansible_memory_mb" 172.16.60.213 | SUCCESS => { "ansible_facts": { "ansible_memory_mb": { "nocache": { "free": 7244, "used": 578 }, "real": { "free": 4772, "total": 7822, "used": 3050 }, "swap": { "cached": 0, "free": 8063, "total": 8063, "used": 0 } } }, "changed": false } 172.16.60.212 | SUCCESS => { "ansible_facts": { "ansible_memory_mb": { "nocache": { "free": 7194, "used": 628 }, "real": { "free": 3544, "total": 7822, "used": 4278 }, "swap": { "cached": 0, "free": 8063, "total": 8063, "used": 0 } } }, "changed": false } 经过通配符实现模糊匹配,好比以”mb”关键字结尾的信息 [root@ansible-server ~]# ansible web-nodes -m setup -a "filter=*mb" 172.16.60.212 | SUCCESS => { "ansible_facts": { "ansible_memfree_mb": 3543, "ansible_memory_mb": { "nocache": { "free": 7193, "used": 629 }, "real": { "free": 3543, "total": 7822, "used": 4279 }, "swap": { "cached": 0, "free": 8063, "total": 8063, "used": 0 } }, "ansible_memtotal_mb": 7822, "ansible_swapfree_mb": 8063, "ansible_swaptotal_mb": 8063 }, "changed": false } 172.16.60.213 | SUCCESS => { "ansible_facts": { "ansible_memfree_mb": 4771, "ansible_memory_mb": { "nocache": { "free": 7243, "used": 579 }, "real": { "free": 4771, "total": 7822, "used": 3051 }, "swap": { "cached": 0, "free": 8063, "total": 8063, "used": 0 } }, "ansible_memtotal_mb": 7822, "ansible_swapfree_mb": 8063, "ansible_swaptotal_mb": 8063 }, "changed": false }
其余经常使用信息列出:
ansible_all_ipv4_addresses:仅显示ipv4的信息。 ansible_devices:仅显示磁盘设备信息。 ansible_distribution:显示是什么系统,例:centos,suse等。 ansible_distribution_major_version:显示是系统主版本。 ansible_distribution_version:仅显示系统版本。 ansible_machine:显示系统类型,例:32位,仍是64位。 ansible_eth0:仅显示eth0的信息。 ansible_hostname:仅显示主机名。 ansible_kernel:仅显示内核版本。 ansible_lvm:显示lvm相关信息。 ansible_memtotal_mb:显示系统总内存。 ansible_memfree_mb:显示可用系统内存。 ansible_memory_mb:详细显示内存状况。 ansible_swaptotal_mb:显示总的swap内存。 ansible_swapfree_mb:显示swap内存的可用内存。 ansible_mounts:显示系统磁盘挂载状况。 ansible_processor:显示cpu个数(具体显示每一个cpu的型号)。 ansible_processor_vcpus:显示cpu个数(只显示总的个数).
Ansible系列命令介绍
Ansible命令是平常工做中最长使用的命令,主要的使用场景为非固化需求,临时一次性的操做。
1) ansible命令的用法
ansible命令其实在运维工做中用的最多的命令,它的主要目的或者说是主要的应用场景是:在作临时性的操做的时候(好比只想看看被控端的一台主机或者多台主机是否存活),在man中的定义是:run a command somewhere else ansible经过ssh实现配置管理、应用部署、任务执行等功能,建议配置ansible端能基于密钥认证的方式联系各个被管理节点. ansible命令在运维工做中是尤其重要的在操做的时候结合ansible的模块(ansible-doc --list命令查看模块)能够实现不少功能:
语法:ansible <host-pattern> [options] 使用ansible --help能够查看到命令参数, 经常使用选项参数: --version #显示版本 -m module #指定使用的模块,默认为command -v #查看执行的详细过程(-vv、-vvvv更详细) --list-hosts #显示主机列表(能够简写为--list) -k,--ask-pass #提示输入ssh链接密码,默认使用key验证 -K,--ask-become-pass #提示执行输入sudo的密码 -C,--check #检查,并不执行 -T,--timeout=TIMEOUT #执行命令的超时时间,默认10s -u,--user=REMOTE_USER #指定远程执行的执行用户 -b,--become #代替旧版本的sudo切换 -h,--help #显示使用帮助 -u #指定远程主机运行此命令的用户 -s #至关于sudo -S #使用sudo ====================================================== [普通选项] -a MODULE_ARGS --args=MODULE_ARGS 传递参数给模块 --ask-vault-pass 询问vault的密码 -B SECONDS --background=SECONDS 异步后台⽅式执⾏任务,并在指定的秒数后超时,超时会杀掉任务的进程。默认是同步,即保持长链接,它会等待 全部执⾏完毕(即阻塞模式)。但有时候是不必这样的,⽐如某些命令的执⾏时间⽐ssh的超时时间还长。若是 不指定超时秒数,将以同步⽅式运⾏任务 -P POLL_INTERVAL --poll=POLL_INTERVAL 异步模式下轮询任务的时间间隔,默认60秒 -C --check 不对远程主机作一些改变,而是预测某些可能发生的改变(检查功能) -e EXTRA_VARS --extra-vars=EXTRA_VARS 配置额外的配置变量(key=value或者YAML/JSON格式) -f FORKS --forks=FORKS 指定并行处理的进程数量,默认5个 -h --help 显示帮助信息 -i INVENTORY --inventory-file=INVENTORY 指定inventory⽂件,多个⽂件使⽤逗号分隔。默认为/etc/ansible/hosts -l SUBSET --limit=SECONDS 使⽤额外的匹配模式来筛选⽬标主机列表。 此处的匹配模式是在已有匹配模式下进⾏的,因此是更严格的筛选。例如指定了主机组的状况下,使⽤-l选项从中只选⼀台主机进⾏控制 --list-hosts 不会执行任何操做,而是列出匹配到的主机列表 -m MODULE_NAME --module-name=MODULE_NAME 指定要执行的模块名,默认的模块为"command" --new-vault-password-file=NEW_VAULT_PASSWORD_FILE new vault password f ile f or rekey -o --one-line 简化输出(一行输出模式) --output-OUTPUT_FILE output f ile name f or encrypt or decrypt; use - f or stdout --syntax-check 检查playbook的语法,不会执行 -t TREE --tree=TREE 记录输出到此⽬录中(测试时以每一个host名如IP地址为⽂件名记录,结果记录到对应的⽂件中)。 此选项在ansible巨慢的时候(如瞬间应该返回的命令还须要10多秒才完成)有奇⽤,或者将ansible的结果重 定向到某个⽂件中也能解决,为何如此,我也不明⽩(表⾯看来和输出⽅式有关系),屡次亲测有效。 --vault-password-file=VAULT_PASSWORD_FILE 指定vault密码文件 -v --verbose 输出详细信息,-vvv和-vvvv输出更详细的信息 --version 显示ansbile的版本 ====================================================== [如下是链接选项,⽤于控制谁以及如何链接主机] -k --ask-pass 询问链接时的密码 --private-key=KEY_FILE --key-file=KEY_FILE 使用文件来认证SSH链接的过程 -u REMOTE_USER --user=REMOTE_USER 使用指定的用户名进行链接 -c CINNECTION --connection=CINNECTION 链接类型,默认为ssh。paramiko (SSH), ssh, winrm and local. local is mostly usef ul f or crontab or kickstarts. -T TIMEOUT --time=TIMEOUT 链接的超时时间,单位为秒,默认10秒 --ssh-common-args=SSH_COMMON_ARGS 指定传递给sftp/scp/ssh等⼯具的通⽤额外参数 --sftp-extra-args=SFTP_EXTRA_ARGS 指定只传递给sftp的额外参数,如-f --scp-extra-args=SCP_EXTRA_ARGS 指定只传递给scp的额外参数,如-l --ssh-extra-args=SSH_EXTRA_ARGS 指定只传递给ssh的额外参数,如-R ====================================================== [如下是权限控制选项:控制在⽬标主机上以什么⾝份和权限运⾏任务] -s --sudo 为运行ansible命令的用户提高权限为sudo_user的权限,此命令已经报废,使用become代替 -u SUDO_USER --sudo-user=SUDO_USER 指望的sudo_user,默认为root,已报废,使用become代替 -S --su 使⽤su的⽅式执⾏操做,已废弃,使⽤become替代 -R SU_USER --su-user=SU_USER 使⽤此user的su执⾏操做,默认为root,已废弃,使⽤become替代 -b --become 使用become的方式升级权限 --become-method=BECOME_METHOD 指定提高权限的方式,可选如下⼏种:sudo/su/pbrun/pf exec/doas/dzdo/ksu/runas值 --become-user=BECOME_USER 要提高为哪一个user的权限,默认为root --ask-sudo-pass 询问sudo密码,已废弃,使⽤become替代 --ask-su-pass 询问su的密码,已废弃,使⽤become替代 -K --ask-become-pass 询问become提高权限时的密码
Ansible返回结果通常会使用红色,橘色,橘黄色显示,红色表示执行过程异常,会终止剩余任务的执行,绿色和橘黄色表示执行过程没有异常,但橘黄色表示命令执行结束后目标有状态变化,绿色表示命令执行结束后目标没有状态变化。
2) ansible-doc
ansible-doc是查看ansible模块(插件)文档说明,针对每一个模块都有详细的用法说明,功能和Linux的man命令相似
语法:ansible-doc [-l|-F|-s] [options] [-t <plugin type> ] [plugin] 参数: -a, --all #显示文档全部的模块(这个选项有个bug) -h, --help #显示使用帮助 -j, --json #将全部的模块转储为JSON格式 -l, --list #查看模块列表 -F, --list_files #显示模块的名称和模块原路径 -M MODULE_PATH, --module-path=MODULE_PATH # -s, --snippet #简介的显示模块的帮助信息 -t TYPE, --type=TYPE #指定模块类型(默认为module) -v, --verbose #查看执行的详细过程(-vv、-vvvv更详细) --version #查看版本 执行 ansible-doc 模块名 查看模块的详细信息 示例: 查看模块详细信息 [root@ansible ~]# ansible-doc ping > PING (/usr/lib/python2.7/site-packages/ansible/modules/system/ping.py) A trivial test module, this module always returns `pong' on successful contact. It does not make sense in playbooks, but it is useful from `/usr/bin/ansible' to verify the ability to login and that a usable Python is configured. This is NOT ICMP ping, this is just a trivial test module that requires Python on the remote-node. For Windows targets, use the [win_ping] module instead. For Network targets, use the [net_ping] module instead. OPTIONS (= is mandatory): - data Data to return for the `ping' return value. If this parameter is set to `crash', the module will cause an exception. [Default: pong] NOTES: * For Windows targets, use the [win_ping] module instead. * For Network targets, use the [net_ping] module instead. AUTHOR: Ansible Core Team, Michael DeHaan METADATA: status: - stableinterface supported_by: core EXAMPLES: # Test we can logon to 'webservers' and execute python with json lib. # ansible webservers -m ping # Example from an Ansible Playbook - ping: # Induce an exception to see what happens - ping: data: crash RETURN VALUES: ping: description: value provided with the data parameter returned: success type: string sample: pong 查看模块的简单信息 [root@ansible ~]# ansible-doc -s ping - name: Try to connect to host, verify a usable python and return `pong' on success ping: data: # Data to return for the `ping' return value. If this parameter is set to `crash', the module will cause an exception.
3) ansible-playbook
ansible-playbook是平常用的最多的命令,其工做机制是:经过读取预先编写好的playbook文件实现批量管理,要实现的功能与命令ansbile同样,能够理解为按必定的条件组成ansible的任务集ansible-playbook命令后跟YML格式的playbook文件,执行事先编排好的任务集
语法: ansible-playbook [options] playbook.yml [playbook2 ...] 参数:大部分的参数和ansible的参数一致 可使用:ansible-playbook --help能够查看到命令参数 例如: 使用ansible-playbook执行一个yaml文件 [root@ansible ~]# ansible-playbook -C /opt/ansible-playbook/test.yaml
4) ansible-galaxy
这个命令是一个下载互联网上roles集合的工具(这里提到的roles集合其实就是多个playbook文件的集合)
roles集合所在地址: https://galaxy.ansible.com
使用方法: 语法:ansible-galaxy [delete|import|info|init|install|list|login|remove|search|setup] [--help] [options] ... 参数: -h, --help #查看命令帮助 -c, --ignore-certs #忽略SSL证书验证错误 -s API_SERVER, --server=API_SERVER #API服务器地址 -v, --verbose #查看执行的详细过程(-vv、-vvvv更详细) --version #查看版本 示例: 搜索roles [root@ansible ~]# ansible-galaxy search nginx 查看search的使用帮助 [root@ansible ~]# ansible-galaxy search --help 安装roles [root@ansible ~]# ansible-galaxy install geerlingguy.nginx - downloading role 'nginx', owned by geerlingguy - downloading role from https://github.com/geerlingguy/ansible-role-nginx/archive/2.6.2.tar.gz - extracting geerlingguy.nginx to /root/.ansible/roles/geerlingguy.nginx - geerlingguy.nginx (2.6.2) was installed successfully 上面能够看到实际上就是互联网上的geerlingguy.nginx下载到本地的/root/.ansible/roles/中 查看install的使用帮助 [root@ansible ~]# ansible-galaxy install --help 删除roles [root@ansible ~]# ansible-galaxy remove geerlingguy.nginx 这个步骤就是把/root/.ansible/roles/中的geerlingguy.nginx文件夹删除 查看remove的使用帮助 [root@ansible ~]# ansible-galaxy remove --help 列出安装的roles [root@ansible ~]# ansible-galaxy list - geerlingguy.nginx, 2.6.2 查看list的使用帮助 [root@ansible ~]# ansible-galaxy list --help ==================================================================== 舒适提示: ansible-galaxy的使用方法还有不少,在语法中能够看到能执行的操做,可使用示例中的相同方法查看各个功能的帮助信息!!! ====================================================================
5) ansible-pull
该指令设计到了ansible的另外一种的工做模式: pull模式 (ansible默认使用的是push模式),这个和一般使用的push模式的工做机制正好相反(push拉取,pull推送)
ansible的pull模式适用于:
-> 你有数量巨大的机器须要配置,即便使用高并发线程依然须要花费大量的时间
-> 你要在刚启动的,没有联网的主机上执行ansible
语法: ansible-pull -U <repository> [options] [<playbook.yml>] 参数: 大部分的参数和ansible的参数一直,由于不经常使用全部就不列举了 可使用 ansible-pull --help查看具体的帮助 经过ansible-pull结合Git和crontab一并实现,其原理以下:经过crontab按期拉取指定的Git版本到本地,并以指定模式自动运行预先制订好的指令。 具体示例参考以下: */20 * * * * root /usr/local/bin/ansible-pull -o -C 2.1.0 -d /srv/www/king-gw/ -i /etc/ansible/hosts -U git:// git.kingifa.com/king-gw-ansiblepull >> /var/log/ansible-pull.log 2>&1 =========================================================== 舒适提示: ansible-pull一般在配置大批量的机器的场景下使用,灵活性有小小的欠缺,但效率几乎能够无限的提高,对于运维人员的技术水平和前瞻性的规划有很高要求!!! ===========================================================
6) ansible-console (ansible本身的终端)
Ansible-console是ansible为用户提供的一款交互式工具,用户可在ansible-console虚拟出的终端上使用ansible内置的各类命令。全部的操做与shell相似,并支持tab补全.
语法:ansible-console [<host-pattern>] [options] 参数:大部分的参数和ansible的参数一直,由于不经常使用全部就不列举了 可使用:ansible-console --help 查看帮助 ===============使用示例================ 对全部的被控客户机进行终端命令操做 [root@ansible ~]# ansible-console Welcome to the ansible console. Type help or ? to list commands. root@all (3)[f:5]$ 而后在虚拟终端里执行命令, 好比输入"uptime", 就会显示全部被控客户机的uptime命令结果 对/etc/ansible/hosts里面定义的某个清单列表进行终端命令操做 [root@ansible ~]# ansible-console web-nodes Welcome to the ansible console. Type help or ? to list commands. root@web-nodes (9)[f:5]$ list 172.16.60.212 172.16.60.213 root@web-nodes (9)[f:5]$ cat /etc/passwd|grep root 172.16.60.212 | CHANGED | rc=0 >> root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin dockerroot:x:998:995:Docker User:/var/lib/docker:/sbin/nologin 172.16.60.213 | CHANGED | rc=0 >> root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin dockerroot:x:998:995:Docker User:/var/lib/docker:/sbin/nologin root@web (2)[f:5]$ 在终端可作的操做 设置并发数: forks n 例如:forks 10 切换组: cd 组名 例如:cd web 列出当前组主机列表 例如:list 列出全部的内置命令(其实就是内置的模块) 例如:?或者help exit命令退出终端
7) ansible-config (该命令不经常使用)
查看,编辑管理ansible的配置文件
语法:ansible-config [view|dump|list] [--help] [options] [ansible.cfg] 参数: -c CONFIG_FILE, --config=CONFIG_FILE #指定配置文件所在的路径 -h, --help #查看帮助信息 -v, --verbose #查看执行的详细过程(-vv、-vvvv更详细) --version #查看版本
8) ansible-connection
这是一个插件,指定执行模式(测试用)
9) ansible-inventory
查看被控制端主机清单的详细信息默认状况下它使用库存脚本,返回JSON格式:
[root@ansible ~]# ansible-inventory --list { "_meta": { "hostvars": { "172.16.60.205": {}, "172.16.60.206": {}, "172.16.60.207": {}, "172.16.60.212": {}, "172.16.60.213": {}, "172.16.60.214": {}, "172.16.60.220": {}, "172.16.60.221": {}, "172.16.60.222": {} } }, "all": { "children": [ "test", "ungrouped", "web-nodes" ] }, "test": { "hosts": [ "172.16.60.212", "172.16.60.213" ] }, "ungrouped": {}, "web-nodes": { "hosts": [ "172.16.60.205", "172.16.60.206", "172.16.60.207", "172.16.60.212", "172.16.60.213", "172.16.60.214", "172.16.60.220", "172.16.60.221", "172.16.60.222" ] } }
10) ansible-vault
ansible-vault主要用于配置文件的加密,如编写的playbook配置文件中包含敏感的信息,不但愿其余人随便的看,ansible-vault可加密/解密这个配置文件:
语法:ansible-vault [create|decrypt|edit|encrypt|encrypt_string|rekey|view] [options] [vaultfile.yml] 参数: --ask-vault-pass ask for vault password -h, --help #查看帮助信息 --new-vault-id=NEW_VAULT_ID #设置用于rekey的新vault标识 --new-vault-password-file=NEW_VAULT_PASSWORD_FILE #新的保险库密码文件为rekey --vault-id=VAULT_IDS #要使用的保险库标识 --vault-password-file=VAULT_PASSWORD_FILES #库密码文件 -v, --verbose #查看执行的详细过程(-vv、-vvvv更详细) --version #查看版本 使用参数: encrypt (加密) decrypt (解密) create (建立) view (查看) edit (编辑) rekey (修改密码) 示例说明 新建一个yml的文件,写入一些数据 [root@ansible-server ~]# echo "kevin123" > bo.yaml 给test.yml设置密码 [root@ansible-server ~]# ansible-vault encrypt bo.yaml New Vault password: Confirm New Vault password: Encryption successful 查看这个yaml文件 [root@ansible-server ~]# cat bo.yaml $ANSIBLE_VAULT;1.1;AES256 33663035323365353562633732366331336261356561636531393039336166623537383263346533 3634363730303134376230653163376239386536626533640a356461633233663937343061313563 36313036643738626531373331623237373636313332313830333738343561306132643836306232 3139393632343163620a313939333639333362373663323065666161646231663263363338663934 6431 上面查看到明显被加密了. 正确的查看方式以下 (即先解密, 而后再查看): [root@ansible-server ~]# ansible-vault decrypt bo.yaml Vault password: Decryption successful [root@ansible-server ~]# cat bo.yaml kevin123 查看被加密的文件 [root@ansible-server ~]# ansible-vault encrypt bo.yaml New Vault password: Confirm New Vault password: Encryption successful [root@ansible-server ~]# ansible-vault view bo.yaml Vault password: kevin123 接着编辑被加密的文件 [root@ansible-server ~]# ansible-vault edit bo.yaml Vault password: #输入密码后, 就进入了bo.yaml文件的打开方式(至关于vim进入编辑) [root@ansible-server ~]# ansible-vault view bo.yaml Vault password: kevin123 this is test 建立被加密的文件 [root@ansible-server ~]# ansible-vault create grace.yaml New Vault password: #输定这个新文件密码 Confirm New Vault password: #再次确认密码后, 就直接进入到这个新文件中进行编辑 [root@ansible-server ~]# ansible-vault view grace.yaml Vault password: this is test of grace
Ansible 其余用法
1) ansible限制后台运行时间
后台运行老是耗费比较长的时间,从而其状态在随后老是可以查看的,若是踢掉主机,又不想轮训,以下: [root@ansible-server ~]# ansible web-nodes -B 3600 -P 0 -a "/usr/bin/long_running_operation --do-stuff" 若是要检查服务的状态,可使用模块async_status,传递job id,以下: [root@ansible-server ~]# ansible web-nodes -m async_status -a "jid=488359678239.2844" 轮训是内建的, 以下: [root@ansible-server ~]# ansible web-nodes -B 1800 -P 60 -a "/usr/bin/long_running_operation --do-stuff" 解释说明: 参数-B表示运行最多30分钟,30*60,-P 60 轮训其状态每60S, 当时间运行在-B参数后的时间以后,此服务会被中止运行。 可使用参数—forksvalue,来确保服务尽快运行
2) ansible收集和查看相关信息, 用于过滤等
[root@ansible-server ~]# ansible all -m setup [root@ansible-server ~]# ansible web-nodes -m setup [root@ansible-server ~]# ansible 172.16.60.214 -m setup
3) Ansible的委托、并发、任务超时
Ansible-playbook 并发运行async、poll, 即ansible的异步执行
ansible方便在于能批量下发,并返回结果和呈现。简单、高效。但有的任务执行起来却不那么直接,可能会花比较长的时间,甚至可能会比ssh的超时时间还要长。这种状况任务是否是无法执行了?
ansible考虑到了这种状况,解决方法就是让下发的任务执行的链接变为异步:任务下发以后,长链接再也不保持,而是每隔一段时间轮询结果,直到任务结束。这就须要在playbook的任务中加入两个参数:async和poll。其中:
-> async参数值表明了这个任务执行时间的上限值。即任务执行所用时间若是超出这个时间,则认为任务失败。此参数若未设置,则为同步执行。
-> poll参数值表明了任务异步执行时轮询的时间间隔。
ansible默认只会建立5个进程,因此一次任务只能同时控制5台机器执行,那若是你有大量的机器须要控制,或者你但愿减小进程数,那你能够采起异步执行。ansible的模块能够把task放进后台,而后轮询它.这使得在必定进程数下能让大量须要的机器同时运做起来。使用async和poll这两个关键字即可以并行运行一个任务。async这个关键字触发ansible并行运做任务,而async的值是ansible等待运行这个任务的最大超时值,而poll就是ansible检查这个任务是否完成的频率时间。
1. 异步和轮询
Ansible 有时候要执行等待时间很长的操做, 这个操做可能要持续很长时间,设置超过ssh的timeout,这时候你能够在step中指定async 和 poll 来实现异步操做。
async 表示这个step的最长等待时长,若是设置为0,表示一直等待下去直到动做完成。
poll 表示检查step操做结果的间隔时长。
示例1
--- - name: Test hosts: localhost tasks: - name: wair for shell: sleep 16 async: 10 poll: 2 结果: TASK: [wair for] ************************************************************** ok: [localhost] <job 207388424975.101038> polling, 8s remaining ok: [localhost] <job 207388424975.101038> polling, 6s remaining ok: [localhost] <job 207388424975.101038> polling, 4s remaining ok: [localhost] <job 207388424975.101038> polling, 2s remaining ok: [localhost] <job 207388424975.101038> polling, 0s remaining <job 207388424975.101038> FAILED on localhost 这个step失败, 由于操做时间超过了最大等待时长
示例2
--- - name: Test hosts: localhost tasks: - name: wair for shell: sleep 16 async: 10 poll: 0 结果: TASK: [wair for] ************************************************************** <job 621720484791.102116> finished on localhost PLAY RECAP ******************************************************************** poll 设置为0, 表示不用等待执行结果, 该step执行成功
示例3
--- - name: Test hosts: localhost tasks: - name: wair for shell: sleep 16 async: 0 poll: 10 结果: # time ansible-playbook xiama.yml TASK: [wair for] ************************************************************** changed: [localhost] PLAY RECAP ******************************************************************** localhost : ok=2 changed=1 unreachable=0 failed=0 real 0m16.693s async设置为0, 会一直等待直到该操做完成.
2. ansible-playbook执行时的并发限制
通常状况下, ansible会同时在全部服务器上执行用户定义的操做, 可是用户能够经过serial参数来定义同时能够在多少太机器上执行操做。
- name: test play hosts: webservers serial: 3 webservers组中的3台机器彻底完成play后, 其余3台机器才会开始执行
serial参数在ansible-1.8之后就开始支持百分比
最大失败百分比
默认状况下, 只要group中还有server没有失败, ansible就是继续执行tasks. 实际上, 用户能够经过"max_fail_percentage" 来定义, 只要超过max_fail_percentage台的server失败, ansible 就能够停止tasks的执行:
- hosts: webservers max_fail_percentage: 30 serial: 10
注意: 实际失败机器必须大于这个百分比时, tasks才会被停止. 等于时是不会停止tasks的
3. 委托
经过"delegate_to", 用户能够把某一个任务放在委托的机器上执行.
- hosts: webservers serial: 5 tasks: - name: take out of load balancer pool command: /usr/bin/take_out_of_pool {{ inventory_hostname }} delegate_to: 127.0.0.1
上面的task会在跑ansible的机器上执行, "delegate_to: 127.0.0.1" 能够用local_action来代替:
tasks: - name: take out of load balancer pool local_action: command /usr/bin/take_out_of_pool {{ inventory_hostname }}
委托者的facts
默认状况下, 委托任务的facts是inventory_hostname中主机的facts, 而不是被委托机器的facts。在ansible 2.0 中, 设置delegate_facts为true可让任务去收集被委托机器的facts
- hosts: app_servers tasks: - name: gather facts from db servers setup: delegate_to: "{{item}}" delegate_facts: True with_items: "{{groups[‘dbservers‘}}" 该例子会收集dbservers的facts并分配给这些机器, 而不会去收集app_servers的facts
RUN ONCE
经过run_once: true来指定该task只能在某一台机器上执行一次. 能够和delegate_to 结合使用
- command: /opt/application/upgrade_db.py run_once: true delegate_to: web01.example.org 指定在"web01.example.org"上执行这
若是没有delegate_to, 那么这个task会在第一台机器上执行