目录
ansible 简介
ansible 是什么?
ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。
ansible是基于 paramiko 开发的,并且基于模块化工作,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。ansible不需要在远程主机上安装client/agents,因为它们是基于ssh来和远
程主机通讯的。ansible目前已经已经被红帽官方收购,是自动化运维工具中大家认可度最高的,并且上手容易,学习简单。是每位运维工程师必须掌握的技能之一。
ansible 特点
- 部署简单,只需在主控端部署Ansible环境,被控端无需做任何操作;
- 默认使用SSH协议对设备进行管理;
- 有大量常规运维操作模块,可实现日常绝大部分操作;
- 配置简单、功能强大、扩展性强;
- 支持API及自定义模块,可通过Python轻松扩展;
- 通过Playbooks来定制强大的配置、状态管理;
- 轻量级,无需在客户端安装agent,更新时,只需在操作机上进行一次更新即可;
- 提供一个功能强大、操作性强的Web管理界面和REST API接口——AWX平台。
ansible 架构图
上图中我们看到的主要模块如下:
Ansible
:Ansible核心程序。HostInventory
:记录由Ansible管理的主机信息,包括端口、密码、ip等。Playbooks
:“剧本”YAML格式文件,多个任务定义在一个文件中,定义主机需要调用哪些模块来完成的功能。CoreModules
:核心模块,主要操作是通过调用核心模块来完成管理任务。CustomModules
:自定义模块,完成核心模块无法完成的功能,支持多种语言。ConnectionPlugins
:连接插件,Ansible和Host通信使用
ansible 任务执行
ansible 任务执行模式
Ansible 系统由控制主机对被管节点的操作方式可分为两类,即adhoc
和playbook
:
- ad-hoc模式(点对点模式)
使用单个模块,支持批量执行单条命令。ad-hoc 命令是一种可以快速输入的命令,而且不需要保存起来的命令。就相当于bash中的一句话shell。 - playbook模式(剧本模式)
是Ansible主要管理方式,也是Ansible功能强大的关键所在。playbook通过多个task集合完成一类功能,如Web服务的安装部署、数据库服务器的批量备份等。可以简单地把playbook理解为通过组合多条ad-hoc操作的配置文件。
ansible 执行流程
简单理解就是Ansible在运行时, 首先读取ansible.cfg
中的配置, 根据规则获取Inventory
中的管理主机列表, 并行的在这些主机中执行配置的任务, 最后等待执行返回的结果。
ansible 命令执行过程
- 加载自己的配置文件,默认
/etc/ansible/ansible.cfg
; - 查找对应的主机配置文件,找到要执行的主机或者组;
- 加载自己对应的模块文件,如 command;
- 通过ansible将模块或命令生成对应的临时py文件(python脚本), 并将该文件传输至远程服务器;
- 对应执行用户的家目录的
.ansible/tmp/XXX/XXX.PY
文件; - 给文件 +x 执行权限;
- 执行并返回结果;
- 删除临时py文件,
sleep 0
退出;
ansible 配置详解
ansible 安装方式
ansible安装常用两种方式,yum安装
和pip程序安装
。下面我们来详细介绍一下这两种安装方式。
使用 pip(python的包管理模块)安装
首先,我们需要安装一个python-pip
包,安装完成以后,则直接使用pip
命令来安装我们的包,具体操作过程如下:
<span style="color:#000000"><code class="language-mipsasm"> yum <span style="color:#0000ff">install </span>python-pip
pip <span style="color:#0000ff">install </span>ansible
</code></span>
使用 yum 安装
yum 安装是我们很熟悉的安装方式了。我们需要先安装一个epel-release
包,然后再安装我们的 ansible 即可。
<span style="color:#000000"><code class="language-mipsasm"> yum <span style="color:#0000ff">install </span>epel-release -y
yum <span style="color:#0000ff">install </span>ansible –y
</code></span>
ansible 程序结构
安装目录如下(yum安装):
配置文件目录:/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/
ansible配置文件查找顺序
ansible与我们其他的服务在这一点上有很大不同,这里的配置文件查找是从多个地方找的,顺序如下:
- 检查环境变量
ANSIBLE_CONFIG
指向的路径文件(export ANSIBLE_CONFIG=/etc/ansible.cfg); ~/.ansible.cfg
,检查当前目录下的ansible.cfg配置文件;/etc/ansible.cfg
检查etc目录的配置文件。
ansible配置文件
ansible 的配置文件为/etc/ansible/ansible.cfg
,ansible 有许多参数,下面我们列出一些常见的参数:
<span style="color:#000000"><code class="language-ini"> <span style="color:#ff0000">inventory</span> = /etc/ansible/hosts <span style="color:#008000">#这个参数表示资源清单inventory文件的位置</span>
<span style="color:#ff0000">library</span> = /usr/share/ansible <span style="color:#008000">#指向存放Ansible模块的目录,支持多个目录方式,只要用冒号(:)隔开就可以</span>
<span style="color:#ff0000">forks</span> = <span style="color:#880000">5</span> <span style="color:#008000">#并发连接数,默认为5</span>
<span style="color:#ff0000">sudo_user</span> = root <span style="color:#008000">#设置默认执行命令的用户</span>
<span style="color:#ff0000">remote_port</span> = <span style="color:#880000">22</span> <span style="color:#008000">#指定连接被管节点的管理端口,默认为22端口,建议修改,能够更加安全</span>
<span style="color:#ff0000">host_key_checking</span> = <span style="color:#a31515">False</span> <span style="color:#008000">#设置是否检查SSH主机的密钥,值为True/False。关闭后第一次连接不会提示配置实例</span>
<span style="color:#ff0000">timeout</span> = <span style="color:#880000">60</span> <span style="color:#008000">#设置SSH连接的超时时间,单位为秒</span>
<span style="color:#ff0000">log_path</span> = /var/log/ansible.log <span style="color:#008000">#指定一个存储ansible日志的文件(默认不记录日志)</span>
</code></span>
ansuble主机清单
在配置文件中,我们提到了资源清单,这个清单就是我们的主机清单,里面保存的是一些 ansible 需要连接管理的主机列表。我们可以来看看他的定义方式:
<span style="color:#000000"><code class="language-shell">1、 直接指明主机地址或主机名:
<span style="color:#2b91af"> #</span><span style="color:#008000"># green.example.com#</span>
<span style="color:#2b91af"> # </span>blue.example.com<span style="color:#008000">#</span>
<span style="color:#2b91af"> # </span>192.168.100.1
<span style="color:#2b91af"> # </span>192.168.100.10
2、 定义一个主机组[组名]把地址或主机名加进去
[mysql_test]
192.168.253.159
192.168.253.160
192.168.253.153
</code></span>
需要注意的是,这里的组成员可以使用通配符来匹配,这样对于一些标准化的管理来说就很轻松方便了。
我们可以根据实际情况来配置我们的主机列表,具体操作如下:
<span style="color:#000000"><code class="language-csharp">[<span style="color:#2b91af">root@server ~</span>]<span style="color:#2b91af"># vim /etc/ansible/hosts</span>
[<span style="color:#2b91af">web</span>]
<span style="color:#880000">192.168</span><span style="color:#880000">.37</span><span style="color:#880000">.122</span>
<span style="color:#880000">192.168</span><span style="color:#880000">.37</span><span style="color:#880000">.133</span>
</code></span>
ansible 常用命令
ansible 命令集
/usr/bin/ansible
Ansibe AD-Hoc 临时命令执行工具,常用于临时命令的执行/usr/bin/ansible-doc
Ansible 模块功能查看工具/usr/bin/ansible-galaxy
下载/上传优秀代码或Roles模块 的官网平台,基于网络的/usr/bin/ansible-playbook
Ansible 定制自动化的任务集编排工具/usr/bin/ansible-pull
Ansible远程执行命令的工具,拉取配置而非推送配置(使用较少,海量机器时使用,对运维的架构能力要求较高)/usr/bin/ansible-vault
Ansible 文件加密工具/usr/bin/ansible-console
Ansible基于Linux Consoble界面可与用户交互的命令执行工具
其中,我们比较常用的是/usr/bin/ansible
和/usr/bin/ansible-playbook
。
ansible-doc 命令
ansible-doc 命令常用于获取模块信息及其使用帮助,一般用法如下:
<span style="color:#000000"><code class="language-bash"> ansible-doc -l <span style="color:#008000">#获取全部模块的信息</span>
ansible-doc -s MOD_NAME <span style="color:#008000">#获取指定模块的使用帮助</span>
</code></span>
我们也可以查看一下ansible-doc的全部用法:
<span style="color:#000000"><code class="language-sql">[root<span style="color:#008000">@server</span> <span style="color:#ab5656">~</span>]# ansible<span style="color:#ab5656">-</span>doc
Usage: ansible<span style="color:#ab5656">-</span>doc [options] [module...]
Options:
<span style="color:#ab5656">-</span>h, <span style="color:#008000">--help show this help message and exit # 显示命令参数API文档</span>
<span style="color:#ab5656">-</span>l, <span style="color:#008000">--list List available modules #列出可用的模块</span>
<span style="color:#ab5656">-</span>M MODULE_PATH, <span style="color:#008000">--module-path=MODULE_PATH #指定模块的路径</span>
specify path(s) <span style="color:#0000ff">to</span> <span style="color:#0000ff">module</span> library (<span style="color:#0000ff">default</span><span style="color:#ab5656">=</span><span style="color:#0000ff">None</span>)
<span style="color:#ab5656">-</span>s, <span style="color:#008000">--snippet Show playbook snippet for specified module(s) #显示playbook制定模块的用法</span>
<span style="color:#ab5656">-</span>v, <span style="color:#008000">--verbose verbose mode (-vvv for more, -vvvv to enable # 显示ansible-doc的版本号查看模块列表:</span>
connection debugging)
<span style="color:#008000">--version show program's version number and exit</span>
</code></span>
我们可以来看一下,以mysql相关的为例:
<span style="color:#000000"><code class="language-csharp">[<span style="color:#2b91af">root@server ~</span>]<span style="color:#2b91af"># ansible-doc -l |grep mysql</span>
mysql_db Add <span style="color:#0000ff">or</span> <span style="color:#0000ff">remove</span> MySQL databases <span style="color:#0000ff">from</span> a remote...
mysql_replication Manage MySQL replication
mysql_user Adds <span style="color:#0000ff">or</span> removes a user <span style="color:#0000ff">from</span> a MySQL databas...
mysql_variables Manage MySQL <span style="color:#0000ff">global</span> variables
[<span style="color:#2b91af">root@server ~</span>]<span style="color:#2b91af"># ansible-doc -s mysql_user</span>
</code></span>
ansible 命令详解
命令的具体格式如下:
<span style="color:#000000"><code class="language-css">ansible <host-pattern> <span style="color:#2b91af">[-f forks]</span> <span style="color:#2b91af">[-m module_name]</span> <span style="color:#2b91af">[-a args]</span>
</code></span>
也可以通过ansible -h
来查看帮助,下面我们列出一些比较常用的选项,并解释其含义:
-a MODULE_ARGS
#模块的参数,如果执行默认COMMAND的模块,即是命令参数,如: “date”,“pwd”等等-k
,--ask-pass
#ask for SSH password。登录密码,提示输入SSH密码而不是假设基于密钥的验证--ask-su-pass
#ask for su password。su切换密码-K
,--ask-sudo-pass
#ask for sudo password。提示密码使用sudo,sudo表示提权操作--ask-vault-pass
#ask for vault password。假设我们设定了加密的密码,则用该选项进行访问-B SECONDS
#后台运行超时时间-C
#模拟运行环境并进行预运行,可以进行查错测试-c CONNECTION
#连接类型使用-f FORKS
#并行任务数,默认为5-i INVENTORY
#指定主机清单的路径,默认为/etc/ansible/hosts
--list-hosts
#查看有哪些主机组-m MODULE_NAME
#执行模块的名字,默认使用 command 模块,所以如果是只执行单一命令可以不用 -m参数-o
#压缩输出,尝试将所有结果在一行输出,一般针对收集工具使用-S
#用 su 命令-R SU_USER
#指定 su 的用户,默认为 root 用户-s
#用 sudo 命令-U SUDO_USER
#指定 sudo 到哪个用户,默认为 root 用户-T TIMEOUT
#指定 ssh 默认超时时间,默认为10s,也可在配置文件中修改-u REMOTE_USER
#远程用户,默认为 root 用户-v
#查看详细信息,同时支持-vvv
,-vvvv
可查看更详细信息
ansible 配置公私钥
上面我们已经提到过 ansible 是基于 ssh 协议实现的,所以其配置公私钥的方式与 ssh 协议的方式相同,具体操作步骤如下:
<span style="color:#000000"><code class="language-csharp"><span style="color:#2b91af">#1.生成私钥</span>
[<span style="color:#2b91af">root@server ~</span>]<span style="color:#2b91af"># ssh-keygen </span>
<span style="color:#2b91af">#2.向主机分发私钥</span>
[<span style="color:#2b91af">root@server ~</span>]<span style="color:#2b91af"># ssh-copy-id root@192.168.37.122</span>
[<span style="color:#2b91af">root@server ~</span>]<span style="color:#2b91af"># ssh-copy-id root@192.168.37.133</span>
</code></span>
这样的话,就可以实现无密码登录,我们的实验过程也会顺畅很多。
注意,如果出现了一下报错:
<span style="color:#000000"><code class="language-dockerfile"> -bash: ssh-<span style="color:#0000ff">copy</span>-<span style="color:#0000ff">id</span>: <span style="color:#0000ff">command</span> not found
</code></span>
那么就证明我们需要安装一个包:
<span style="color:#000000"><code class="language-markdown"><span style="color:#397300"> yum -y install openssh-clientsansible
</span></code></span>
把包安装上即可。
ansible 常用模块
1)主机连通性测试
我们使用ansible web -m ping
命令来进行主机连通性测试,效果如下:
<span style="color:#000000"><code class="language-ruby">[root<span style="color:#008000">@server</span> ~]<span style="color:#008000"># ansible web -m ping</span>
<span style="color:#2b91af">192.168.37.122 | SUCCESS =></span> {
<span style="color:#a31515">"changed"</span>: false,
<span style="color:#a31515">"ping"</span>: <span style="color:#a31515">"pong"</span>
}
<span style="color:#2b91af">192.168.37.133 | SUCCESS =></span> {
<span style="color:#a31515">"changed"</span>: false,
<span style="color:#a31515">"ping"</span>: <span style="color:#a31515">"pong"</span>
}
</code></span>
这样就说明我们的主机是连通状态的。接下来的操作才可以正常进行。
2)command 模块
这个模块可以直接在远程主机上执行命令,并将结果返回本主机。
举例如下:
<span style="color:#000000"><code class="language-ruby">[root<span style="color:#008000">@server</span> ~]<span style="color:#008000"># ansible web -m command -a 'ss -ntl'</span>
<span style="color:#880000">192.168</span>.<span style="color:#880000">37.122</span> | SUCCESS | rc=<span style="color:#880000">0</span> >>
State Recv-Q Send-Q Local <span style="color:#00b0e8">Address:</span>Port Peer <span style="color:#00b0e8">Address:</span>Port
LISTEN <span style="color:#880000">0</span> <span style="color:#880000">128</span> *<span style="color:#00b0e8">:</span><span style="color:#880000">111</span> *<span style="color:#00b0e8">:*</span>
LISTEN <span style="color:#880000">0</span> <span style="color:#880000">5</span> <span style="color:#880000">192.168</span>.<span style="color:#880000">122.1</span><span style="color:#00b0e8">:</span><span style="color:#880000">53</span> *<span style="color:#00b0e8">:*</span>
LISTEN <span style="color:#880000">0</span> <span style="color:#880000">128</span> *<span style="color:#00b0e8">:</span><span style="color:#880000">22</span> *<span style="color:#00b0e8">:*</span>
LISTEN <span style="color:#880000">0</span> <span style="color:#880000">128</span> <span style="color:#880000">127.0</span>.<span style="color:#880000">0.1</span><span style="color:#00b0e8">:</span><span style="color:#880000">631</span> *<span style="color:#00b0e8">:*</span>
LISTEN <span style="color:#880000">0</span> <span style="color:#880000">128</span> *<span style="color:#00b0e8">:</span><span style="color:#880000">23000</span> *<span style="color:#00b0e8">:*</span>
LISTEN <span style="color:#880000">0</span> <span style="color:#880000">100</span> <span style="color:#880000">127.0</span>.<span style="color:#880000">0.1</span><span style="color:#00b0e8">:</span><span style="color:#880000">25</span> *<span style="color:#00b0e8">:*</span>
LISTEN <span style="color:#880000">0</span> <span style="color:#880000">128</span> <span style="color:#00b0e8">:</span><span style="color:#00b0e8">:</span><span style="color:#00b0e8">:</span><span style="color:#880000">111</span> <span style="color:#00b0e8">:</span><span style="color:#00b0e8">:</span><span style="color:#00b0e8">:*</span>
LISTEN <span style="color:#880000">0</span> <span style="color:#880000">128</span> <span style="color:#00b0e8">:</span><span style="color:#00b0e8">:</span><span style="color:#00b0e8">:</span><span style="color:#880000">22</span> <span style="color:#00b0e8">:</span><span style="color:#00b0e8">:</span><span style="color:#00b0e8">:*</span>
LISTEN <span style="color:#880000">0</span> <span style="color:#880000">128</span> <span style="color:#00b0e8">:</span><span style="color:#00b0e8">:</span><span style="color:#880000">1</span><span style="color:#00b0e8">:</span><span style="color:#880000">631</span> <span style="color:#00b0e8">:</span><span style="color:#00b0e8">:</span><span style="color:#00b0e8">:*</span>
LISTEN <span style="color:#880000">0</span> <span style="color:#880000">100</span> <span style="color:#00b0e8">:</span><span style="color:#00b0e8">:</span><span style="color:#880000">1</span><span style="color:#00b0e8">:</span><span style="color:#880000">25</span> <span style="color:#00b0e8">:</span><span style="color:#00b0e8">:</span><span style="color:#00b0e8">:*</span>
<span style="color:#880000">192.168</span>.<span style="color:#880000">37.133</span> | SUCCESS | rc=<span style="color:#880000">0</span> >>
State Recv-Q Send-Q Local <span style="color:#00b0e8">Address:</span>Port Peer <span style="color:#00b0e8">Address:</span>Port
LISTEN <span style="color:#880000">0</span> <span style="color:#880000">128</span> *<span style="color:#00b0e8">:</span><span style="color:#880000">111</span> *<span style="color:#00b0e8">:*</span>
LISTEN <span style="color:#880000">0</span> <span style="color:#880000">128</span> *<span style="color:#00b0e8">:</span><span style="color:#880000">22</span> *<span style="color:#00b0e8">:*</span>
LISTEN <span style="color:#880000">0</span> <span style="color:#880000">128</span> <span style="color:#880000">127.0</span>.<span style="color:#880000">0.1</span><span style="color:#00b0e8">:</span><span style="color:#880000">631</span> *<span style="color:#00b0e8">:*</span>
LISTEN <span style="color:#880000">0</span> <span style="color:#880000">128</span> *<span style="color:#00b0e8">:</span><span style="color:#880000">23000</span> *<span style="color:#00b0e8">:*</span>
LISTEN <span style="color:#880000">0</span> <span style="color:#880000">100</span> <span style="color:#880000">127.0</span>.<span style="color:#880000">0.1</span><span style="color:#00b0e8">:</span><span style="color:#880000">25</span> *<span style="color:#00b0e8">:*</span>
LISTEN <span style="color:#880000">0</span> <span style="color:#880000">128</span> <span style="color:#00b0e8">:</span><span style="color:#00b0e8">:</span><span style="color:#00b0e8">:</span><span style="color:#880000">111</span> <span style="color:#00b0e8">:</span><span style="color:#00b0e8">:</span><span style="color:#00b0e8">:*</span>
LISTEN <span style="color:#880000">0</span> <span style="color:#880000">128</span> <span style="color:#00b0e8">:</span><span style="color:#00b0e8">:</span><span style="color:#00b0e8">:</span><span style="color:#880000">22</span> <span style="color:#00b0e8">:</span><span style="color:#00b0e8">:</span><span style="color:#00b0e8">:*</span>
LISTEN <span style="color:#880000">0</span> <span style="color:#880000">128</span> <span style="color:#00b0e8">:</span><span style="color:#00b0e8">:</span><span style="color:#880000">1</span><span style="color:#00b0e8">:</span><span style="color:#880000">631</span> <span style="color:#00b0e8">:</span><span style="color:#00b0e8">:</span><span style="color:#00b0e8">:*</span>
LISTEN <span style="color:#880000">0</span> <span style="color:#880000">100</span> <span style="color:#00b0e8">:</span><span style="color:#00b0e8">:</span><span style="color:#880000">1</span><span style="color:#00b0e8">:</span><span style="color:#880000">25</span> <span style="color:#00b0e8">:</span><span style="color:#00b0e8">:</span><span style="color:#00b0e8">:*</span>
</code></span>
命令模块接受命令名称,后面是空格分隔的列表参数。给定的命令将在所有选定的节点上执行。它不会通过shell进行处理,比如$HOME和操作如"<",">","|",";","&" 工作(需要使用(shell)模块实现这些功能)。注意,该命令不支持| 管道命令
。
下面来看一看该模块下常用的几个命令:
chdir # 在执行命令之前,先切换到该目录
executable # 切换shell来执行命令,需要使用命令的绝对路径
free_form # 要执行的Linux指令,一般使用Ansible的-a参数代替。
creates # 一个文件名,当这个文件存在,则该命令不执行,可以
用来做判断
removes # 一个文件名,这个文件不存在,则该命令不执行
下面我们来看看这些命令的执行效果:
<span style="color:#000000"><code class="language-mipsasm">[root@server ~]<span style="color:#008000"># ansible web -m command -a 'chdir=/data/ ls' #先切换到/data/ 目录,再执行“ls”命令</span>
<span style="color:#880000">192</span>.<span style="color:#880000">168</span>.<span style="color:#880000">37</span>.<span style="color:#880000">122</span> <span style="color:#a31515">| SUCCESS |</span> rc=<span style="color:#880000">0</span> >>
aaa.<span style="color:#0000ff">jpg
</span>fastdfs
mogdata
tmp
web
wKgleloeYoCAMLtZAAAWEekAtkc497.<span style="color:#0000ff">jpg
</span>
<span style="color:#880000">192</span>.<span style="color:#880000">168</span>.<span style="color:#880000">37</span>.<span style="color:#880000">133</span> <span style="color:#a31515">| SUCCESS |</span> rc=<span style="color:#880000">0</span> >>
aaa.<span style="color:#0000ff">jpg
</span>fastdfs
mogdata
tmp
web
wKgleloeYoCAMLtZAAAWEekAtkc497.<span style="color:#0000ff">jpg
</span></code></span>
<span style="color:#000000"><code class="language-haskell">[root@server ~]# ansible web -m command -a 'creates=/<span style="color:#0000ff">data</span>/aaa.jpg ls' #如果/<span style="color:#0000ff">data</span>/aaa.jpg存在,则不执行“ls”命令
<span style="color:#880000">192.168</span>.<span style="color:#880000">37.122</span> | <span style="color:#a31515">SUCCESS</span> | rc=<span style="color:#880000">0</span> >>
<span style="color:#a31515">skipped</span>, since /<span style="color:#0000ff">data</span>/aaa.jpg exists
<span style="color:#880000">192.168</span>.<span style="color:#880000">37.133</span> | <span style="color:#a31515">SUCCESS</span> | rc=<span style="color:#880000">0</span> >>
<span style="color:#a31515">skipped</span>, since /<span style="color:#0000ff">data</span>/aaa.jpg exists
</code></span>
<span style="color:#000000"><code class="language-haskell">[root@server ~]# ansible web -m command -a 'removes=/<span style="color:#0000ff">data</span>/aaa.jpg cat /<span style="color:#0000ff">data</span>/a' #如果/<span style="color:#0000ff">data</span>/aaa.jpg存在,则执行“cat /<span style="color:#0000ff">data</span>/a”命令
<span style="color:#880000">192.168</span>.<span style="color:#880000">37.122</span> | <span style="color:#a31515">SUCCESS</span> | rc=<span style="color:#880000">0</span> >>
<span style="color:#a31515">hello</span>
<span style="color:#880000">192.168</span>.<span style="color:#880000">37.133</span> | <span style="color:#a31515">SUCCESS</span> | rc=<span style="color:#880000">0</span> >>
<span style="color:#a31515">hello</span>
</code></span>
3)shell 模块
shell模块可以在远程主机上调用shell解释器运行命令,支持shell的各种功能,例如管道等。
<span style="color:#000000"><code class="language-ruby">[root<span style="color:#008000">@server</span> ~]<span style="color:#008000"># ansible web -m shell -a 'cat /etc/passwd |grep "keer"'</span>
<span style="color:#880000">192.168</span>.<span style="color:#880000">37.122</span> | SUCCESS | rc=<span style="color:#880000">0</span> >>
<span style="color:#00b0e8">keer:</span><span style="color:#00b0e8">x:</span><span style="color:#880000">10001</span><span style="color:#00b0e8">:</span><span style="color:#880000">1000</span><span style="color:#00b0e8">:keer</span><span style="color:#00b0e8">:/home/keer</span><span style="color:#00b0e8">:/bin/sh</span>
<span style="color:#880000">192.168</span>.<span style="color:#880000">37.133</span> | SUCCESS | rc=<span style="color:#880000">0</span> >>
<span style="color:#00b0e8">keer:</span><span style="color:#00b0e8">x:</span><span style="color:#880000">10001</span><span style="color:#00b0e8">:</span><span style="color:#880000">10001</span><span style="color:#00b0e8">:</span><span style="color:#00b0e8">:/home/keer</span><span style="color:#00b0e8">:/bin/sh</span>
</code></span>
只要是我们的shell命令,都可以通过这个模块在远程主机上运行,这里就不一一举例了。
4)copy 模块
这个模块用于将文件复制到远程主机,同时支持给定内容生成文件和修改权限等。
其相关选项如下:
src
#被复制到远程主机的本地文件。可以是绝对路径,也可以是相对路径。如果路径是一个目录,则会递归复制,用法类似于"rsync"content
#用于替换"src",可以直接指定文件的值dest
#必选项,将源文件复制到的远程主机的绝对路径backup
#当文件内容发生改变后,在覆盖之前把源文件备份,备份文件包含时间信息directory_mode
#递归设定目录的权限,默认为系统默认权限force
#当目标主机包含该文件,但内容不同时,设为"yes",表示强制覆盖;设为"no",表示目标主机的目标位置不存在该文件才复制。默认为"yes"others
#所有的 file 模块中的选项可以在这里使用
用法举例如下:
① 复制文件:
<span style="color:#000000"><code class="language-ruby">[root<span style="color:#008000">@server</span> ~]<span style="color:#008000"># ansible web -m copy -a 'src=~/hello dest=/data/hello' </span>
<span style="color:#2b91af">192.168.37.122 | SUCCESS =></span> {
<span style="color:#a31515">"changed"</span>: true,
<span style="color:#a31515">"checksum"</span>: <span style="color:#a31515">"22596363b3de40b06f981fb85d82312e8c0ed511"</span>,
<span style="color:#a31515">"dest"</span>: <span style="color:#a31515">"/data/hello"</span>,
<span style="color:#a31515">"gid"</span>: <span style="color:#880000">0</span>,
<span style="color:#a31515">"group"</span>: <span style="color:#a31515">"root"</span>,
<span style="color:#a31515">"md5sum"</span>: <span style="color:#a31515">"6f5902ac237024bdd0c176cb93063dc4"</span>,
<span style="color:#a31515">"mode"</span>: <span style="color:#a31515">"0644"</span>,
<span style="color:#a31515">"owner"</span>: <span style="color:#a31515">"root"</span>,
<span style="color:#a31515">"size"</span>: <span style="color:#880000">12</span>,
<span style="color:#a31515">"src"</span>: <span style="color:#a31515">"/root/.ansible/tmp/ansible-tmp-1512437093.55-228281064292921/source"</span>,
<span style="color:#a31515">"state"</span>: <span style="color:#a31515">"file"</span>,
<span style="color:#a31515">"uid"</span>: <span style="color:#880000">0</span>
}
<span style="color:#2b91af">192.168.37.133 | SUCCESS =></span> {
<span style="color:#a31515">"changed"</span>: true,
<span style="color:#a31515">"checksum"</span>: <span style="color:#a31515">"22596363b3de40b06f981fb85d82312e8c0ed511"</span>,
<span style="color:#a31515">"dest"</span>: <span style="color:#a31515">"/data/hello"</span>,
<span style="color:#a31515">"gid"</span>: <span style="color:#880000">0</span>,
<span style="color:#a31515">"group"</span>: <span style="color:#a31515">"root"</span>,
<span style="color:#a31515">"md5sum"</span>: <span style="color:#a31515">"6f5902ac237024bdd0c176cb93063dc4"</span>,
<span style="color:#a31515">"mode"</span>: <span style="color:#a31515">"0644"</span>,
<span style="color:#a31515">"owner"</span>: <span style="color:#a31515">"root"</span>,
<span style="color:#a31515">"size"</span>: <span style="color:#880000">12</span>,
<span style="color:#a31515">"src"</span>: <span style="color:#a31515">"/root/.ansible/tmp/ansible-tmp-1512437093.74-44694985235189/source"</span>,
<span style="color:#a31515">"state"</span>: <span style="color:#a31515">"file"</span>,
<span style="color:#a31515">"uid"</span>: <span style="color:#880000">0</span>
}
</code></span>
② 给定内容生成文件,并制定权限
<span style="color:#000000"><code class="language-ruby">[root<span style="color:#008000">@server</span> ~]<span style="color:#008000"># ansible web -m copy -a 'content="I am keer\n" dest=/data/name mode=666'</span>
<span style="color:#2b91af">192.168.37.122 | SUCCESS =></span> {
<span style="color:#a31515">"changed"</span>: true,
<span style="color:#a31515">"checksum"</span>: <span style="color:#a31515">"0421570938940ea784f9d8598dab87f07685b968"</span>,
<span style="color:#a31515">"dest"</span>: <span style="color:#a31515">"/data/name"</span>,
<span style="color:#a31515">"gid"</span>: <span style="color:#880000">0</span>,
<span style="color:#a31515">"group"</span>: <span style="color:#a31515">"root"</span>,
<span style="color:#a31515">"md5sum"</span>: <span style="color:#a31515">"497fa8386590a5fc89090725b07f175c"</span>,
<span style="color:#a31515">"mode"</span>: <span style="color:#a31515">"0666"</span>,
<span style="color:#a31515">"owner"</span>: <span style="color:#a31515">"root"</span>,
<span style="color:#a31515">"size"</span>: <span style="color:#880000">10</span>,
<span style="color:#a31515">"src"</span>: <span style="color:#a31515">"/root/.ansible/tmp/ansible-tmp-1512437327.37-199512601767687/source"</span>,
<span style="color:#a31515">"state"</span>: <span style="color:#a31515">"file"</span>,
<span style="color:#a31515">"uid"</span>: <span style="color:#880000">0</span>
}
<span style="color:#2b91af">192.168.37.133 | SUCCESS =></span> {
<span style="color:#a31515">"changed"</span>: true,
<span style="color:#a31515">"checksum"</span>: <span style="color:#a31515">"0421570938940ea784f9d8598dab87f07685b968"</span>,
<span style="color:#a31515">"dest"</span>: <span style="color:#a31515">"/data/name"</span>,
<span style="color:#a31515">"gid"</span>: <span style="color:#880000">0</span>,
<span style="color:#a31515">"group"</span>: <span style="color:#a31515">"root"</span>,
<span style="color:#a31515">"md5sum"</span>: <span style="color:#a31515">"497fa8386590a5fc89090725b07f175c"</span>,
<span style="color:#a31515">"mode"</span>: <span style="color:#a31515">"0666"</span>,
<span style="color:#a31515">"owner"</span>: <span style="color:#a31515">"root"</span>,
<span style="color:#a31515">"size"</span>: <span style="color:#880000">10</span>,
<span style="color:#a31515">"src"</span>: <span style="color:#a31515">"/root/.ansible/tmp/ansible-tmp-1512437327.55-218104039503110/source"</span>,
<span style="color:#a31515">"state"</span>: <span style="color:#a31515">"file"</span>,
<span style="color:#a31515">"uid"</span>: <span style="color:#880000">0</span>
}
</code></span>
我们现在可以去查看一下我们生成的文件及其权限:
<span style="color:#000000"><code class="language-sql">[root<span style="color:#008000">@server</span> <span style="color:#ab5656">~</span>]# ansible web <span style="color:#ab5656">-</span>m shell <span style="color:#ab5656">-</span>a <span style="color:#a31515">'ls -l /data/'</span>
<span style="color:#880000">192.168</span><span style="color:#880000">.37</span><span style="color:#880000">.122</span> <span style="color:#ab5656">|</span> SUCCESS <span style="color:#ab5656">|</span> rc<span style="color:#ab5656">=</span><span style="color:#880000">0</span> <span style="color:#ab5656">>></span>
total <span style="color:#880000">28</span>
<span style="color:#ab5656">-</span>rw<span style="color:#ab5656">-</span>rw<span style="color:#ab5656">-</span>rw<span style="color:#ab5656">-</span> <span style="color:#880000">1</span> root root <span style="color:#880000">12</span> <span style="color:#a31515">Dec</span> <span style="color:#880000">6</span> <span style="color:#880000">09</span>:<span style="color:#880000">45</span> name
<span style="color:#880000">192.168</span><span style="color:#880000">.37</span><span style="color:#880000">.133</span> <span style="color:#ab5656">|</span> SUCCESS <span style="color:#ab5656">|</span> rc<span style="color:#ab5656">=</span><span style="color:#880000">0</span> <span style="color:#ab5656">>></span>
total <span style="color:#880000">40</span>
<span style="color:#ab5656">-</span>rw<span style="color:#ab5656">-</span>rw<span style="color:#ab5656">-</span>rw<span style="color:#ab5656">-</span> <span style="color:#880000">1</span> root root <span style="color:#880000">12</span> <span style="color:#a31515">Dec</span> <span style="color:#880000">5</span> <span style="color:#880000">09</span>:<span style="color:#880000">45</span> name
</code></span>
可以看出我们的name文件已经生成,并且权限为666。
③ 关于覆盖
我们把文件的内容修改一下,然后选择覆盖备份:
<span style="color:#000000"><code class="language-ruby">[root<span style="color:#008000">@server</span> ~]<span style="color:#008000"># ansible web -m copy -a 'content="I am keerya\n" backup=yes dest=/data/name mode=666'</span>
<span style="color:#2b91af">192.168.37.122 | SUCCESS =></span> {
<span style="color:#a31515">"backup_file"</span>: <span style="color:#a31515">"/data/name.4394.2017-12-06@09:46:25~"</span>,
<span style="color:#a31515">"changed"</span>: true,
<span style="color:#a31515">"checksum"</span>: <span style="color:#a31515">"064a68908ab9971ee85dbc08ea038387598e3778"</span>,
<span style="color:#a31515">"dest"</span>: <span style="color:#a31515">"/data/name"</span>,
<span style="color:#a31515">"gid"</span>: <span style="color:#880000">0</span>,
<span style="color:#a31515">"group"</span>: <span style="color:#a31515">"root"</span>,
<span style="color:#a31515">"md5sum"</span>: <span style="color:#a31515">"8ca7c11385856155af52e560f608891c"</span>,
<span style="color:#a31515">"mode"</span>: <span style="color:#a31515">"0666"</span>,
<span style="color:#a31515">"owner"</span>: <span style="color:#a31515">"root"</span>,
<span style="color:#a31515">"size"</span>: <span style="color:#880000">12</span>,
<span style="color:#a31515">"src"</span>: <span style="color:#a31515">"/root/.ansible/tmp/ansible-tmp-1512438383.78-228128616784888/source"</span>,
<span style="color:#a31515">"state"</span>: <span style="color:#a31515">"file"</span>,
<span style="color:#a31515">"uid"</span>: <span style="color:#880000">0</span>
}
<span style="color:#2b91af">192.168.37.133 | SUCCESS =></span> {
<span style="color:#a31515">"backup_file"</span>: <span style="color:#a31515">"/data/name.5962.2017-12-05@09:46:24~"</span>,
<span style="color:#a31515">"changed"</span>: true,
<span style="color:#a31515">"checksum"</span>: <span style="color:#a31515">"064a68908ab9971ee85dbc08ea038387598e3778"</span>,
<span style="color:#a31515">"dest"</span>: <span style="color:#a31515">"/data/name"</span>,
<span style="color:#a31515">"gid"</span>: <span style="color:#880000">0</span>,
<span style="color:#a31515">"group"</span>: <span style="color:#a31515">"root"</span>,
<span style="color:#a31515">"md5sum"</span>: <span style="color:#a31515">"8ca7c11385856155af52e560f608891c"</span>,
<span style="color:#a31515">"mode"</span>: <span style="color:#a31515">"0666"</span>,
<span style="color:#a31515">"owner"</span>: <span style="color:#a31515">"root"</span>,
<span style="color:#a31515">"size"</span>: <span style="color:#880000">12</span>,
<span style="color:#a31515">"src"</span>: <span style="color:#a31515">"/root/.ansible/tmp/ansible-tmp-1512438384.0-170718946740009/source"</span>,
<span style="color:#a31515">"state"</span>: <span style="color:#a31515">"file"</span>,
<span style="color:#a31515">"uid"</span>: <span style="color:#880000">0</span>
}
</code></span>
现在我们可以去查看一下:
<span style="color:#000000"><code class="language-sql">[root<span style="color:#008000">@server</span> <span style="color:#ab5656">~</span>]# ansible web <span style="color:#ab5656">-</span>m shell <span style="color:#ab5656">-</span>a <span style="color:#a31515">'ls -l /data/'</span>
<span style="color:#880000">192.168</span><span style="color:#880000">.37</span><span style="color:#880000">.122</span> <span style="color:#ab5656">|</span> SUCCESS <span style="color:#ab5656">|</span> rc<span style="color:#ab5656">=</span><span style="color:#880000">0</span> <span style="color:#ab5656">>></span>
total <span style="color:#880000">28</span>
<span style="color:#ab5656">-</span>rw<span style="color:#ab5656">-</span>rw<span style="color:#ab5656">-</span>rw<span style="color:#ab5656">-</span> <span style="color:#880000">1</span> root root <span style="color:#880000">12</span> <span style="color:#a31515">Dec</span> <span style="color:#880000">6</span> <span style="color:#880000">09</span>:<span style="color:#880000">46</span> name
<span style="color:#ab5656">-</span>rw<span style="color:#ab5656">-</span>rw<span style="color:#ab5656">-</span>rw<span style="color:#ab5656">-</span> <span style="color:#880000">1</span> root root <span style="color:#880000">10</span> <span style="color:#a31515">Dec</span> <span style="color:#880000">6</span> <span style="color:#880000">09</span>:<span style="color:#880000">45</span> name<span style="color:#880000">.4394</span><span style="color:#880000">.2017</span><span style="color:#880000">-12</span><span style="color:#880000">-06</span><span style="color:#008000">@09</span>:<span style="color:#880000">46</span>:<span style="color:#880000">25</span><span style="color:#ab5656">~</span>
<span style="color:#880000">192.168</span><span style="color:#880000">.37</span><span style="color:#880000">.133</span> <span style="color:#ab5656">|</span> SUCCESS <span style="color:#ab5656">|</span> rc<span style="color:#ab5656">=</span><span style="color:#880000">0</span> <span style="color:#ab5656">>></span>
total <span style="color:#880000">40</span>
<span style="color:#ab5656">-</span>rw<span style="color:#ab5656">-</span>rw<span style="color:#ab5656">-</span>rw<span style="color:#ab5656">-</span> <span style="color:#880000">1</span> root root <span style="color:#880000">12</span> <span style="color:#a31515">Dec</span> <span style="color:#880000">5</span> <span style="color:#880000">09</span>:<span style="color:#880000">46</span> name
<span style="color:#ab5656">-</span>rw<span style="color:#ab5656">-</span>rw<span style="color:#ab5656">-</span>rw<span style="color:#ab5656">-</span> <span style="color:#880000">1</span> root root <span style="color:#880000">10</span> <span style="color:#a31515">Dec</span> <span style="color:#880000">5</span> <span style="color:#880000">09</span>:<span style="color:#880000">45</span> name<span style="color:#880000">.5962</span><span style="color:#880000">.2017</span><span style="color:#880000">-12</span><span style="color:#880000">-05</span><span style="color:#008000">@09</span>:<span style="color:#880000">46</span>:<span style="color:#880000">24</span><span style="color:#ab5656">~</span>
</code></span>
可以看出,我们的源文件已经被备份,我们还可以查看一下name
文件的内容:
<span style="color:#000000"><code class="language-csharp">[<span style="color:#2b91af">root@server ~</span>]<span style="color:#2b91af"># ansible web -m shell -a 'cat /data/name'</span>
<span style="color:#880000">192.168</span><span style="color:#880000">.37</span><span style="color:#880000">.122</span> | SUCCESS | rc=<span style="color:#880000">0</span> >>
I am keerya
<span style="color:#880000">192.168</span><span style="color:#880000">.37</span><span style="color:#880000">.133</span> | SUCCESS | rc=<span style="color:#880000">0</span> >>
I am keerya
</code></span>
证明,这正是我们新导入的文件的内容。
5)file 模块
该模块主要用于设置文件的属性,比如创建文件、创建链接文件、删除文件等。
下面是一些常见的命令:
force
#需要在两种情况下强制创建软链接,一种是源文件不存在,但之后会建立的情况下;另一种是目标软链接已存在,需要先取消之前的软链,然后创建新的软链,有两个选项:yes|nogroup
#定义文件/目录的属组。后面可以加上mode
:定义文件/目录的权限owner
#定义文件/目录的属主。后面必须跟上path
:定义文件/目录的路径recurse
#递归设置文件的属性,只对目录有效,后面跟上src
:被链接的源文件路径,只应用于state=link
的情况dest
#被链接到的路径,只应用于state=link
的情况state
#状态,有以下选项:
directory
:如果目录不存在,就创建目录file
:即使文件不存在,也不会被创建link
:创建软链接hard
:创建硬链接touch
:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间absent
:删除目录、文件或者取消链接文件
用法举例如下:
① 创建目录:
<span style="color:#000000"><code class="language-ruby">[root<span style="color:#008000">@server</span> ~]<span style="color:#008000"># ansible web -m file -a 'path=/data/app state=directory'</span>
<span style="color:#2b91af">192.168.37.122 | SUCCESS =></span> {
<span style="color:#a31515">"changed"</span>: true,
<span style="color:#a31515">"gid"</span>: <span style="color:#880000">0</span>,
<span style="color:#a31515">"group"</span>: <span style="color:#a31515">"root"</span>,
<span style="color:#a31515">"mode"</span>: <span style="color:#a31515">"0755"</span>,
<span style="color:#a31515">"owner"</span>: <span style="color:#a31515">"root"</span>,
<span style="color:#a31515">"path"</span>: <span style="color:#a31515">"/data/app"</span>,
<span style="color:#a31515">"size"</span>: <span style="color:#880000">6</span>,
<span style="color:#a31515">"state"</span>: <span style="color:#a31515">"directory"</span>,
<span style="color:#a31515">"uid"</span>: <span style="color:#880000">0</span>
}
<span style="color:#2b91af">192.168.37.133 | SUCCESS =></span> {
<span style="color:#a31515">"changed"</span>: true,
<span style="color:#a31515">"gid"</span>: <span style="color:#880000">0</span>,
<span style="color:#a31515">"group"</span>: <span style="color:#a31515">"root"</span>,
<span style="color:#a31515">"mode"</span>: <span style="color:#a31515">"0755"</span>,
<span style="color:#a31515">"owner"</span>: <span style="color:#a31515">"root"</span>,
<span style="color:#a31515">"path"</span>: <span style="color:#a31515">"/data/app"</span>,
<span style="color:#a31515">"size"</span>: <span style="color:#880000">4096</span>,
<span style="color:#a31515">"state"</span>: <span style="color:#a31515">"directory"</span>,
<span style="color:#a31515">"uid"</span>: <span style="color:#880000">0</span>
}
</code></span>
我们可以查看一下:
<span style="color:#000000"><code class="language-sql">[root<span style="color:#008000">@server</span> <span style="color:#ab5656">~</span>]# ansible web <span style="color:#ab5656">-</span>m shell <span style="color:#ab5656">-</span>a <span style="color:#a31515">'ls -l /data'</span>
<span style="color:#880000">192.168</span><span style="color:#880000">.37</span><span style="color:#880000">.122</span> <span style="color:#ab5656">|</span> SUCCESS <span style="color:#ab5656">|</span> rc<span style="color:#ab5656">=</span><span style="color:#880000">0</span> <span style="color:#ab5656">>></span>
total <span style="color:#880000">28</span>
drwxr<span style="color:#ab5656">-</span>xr<span style="color:#ab5656">-</span>x <span style="color:#880000">2</span> root root <span style="color:#880000">6</span> <span style="color:#a31515">Dec</span> <span style="color:#880000">6</span> <span style="color:#880000">10</span>:<span style="color:#880000">21</span> app
<span style="color:#880000">192.168</span><span style="color:#880000">.37</span><span style="color:#880000">.133</span> <span style="color:#ab5656">|</span> SUCCESS <span style="color:#ab5656">|</span> rc<span style="color:#ab5656">=</span><span style="color:#880000">0</span> <span style="color:#ab5656">>></span>
total <span style="color:#880000">44</span>
drwxr<span style="color:#ab5656">-</span>xr<span style="color:#ab5656">-</span>x <span style="color:#880000">2</span> root root <span style="color:#880000">4096</span> <span style="color:#a31515">Dec</span> <span style="color:#880000">5</span> <span style="color:#880000">10</span>:<span style="color:#880000">21</span> app
</code></span>
可以看出,我们的目录已经创建完成。
② 创建链接文件
<span style="color:#000000"><code class="language-ruby">[root<span style="color:#008000">@server</span> ~]<span style="color:#008000"># ansible web -m file -a 'path=/data/bbb.jpg src=aaa.jpg state=link'</span>
<span style="color:#2b91af">192.168.37.122 | SUCCESS =></span> {
<span style="color:#a31515">"changed"</span>: true,
<span style="color:#a31515">"dest"</span>: <span style="color:#a31515">"/data/bbb.jpg"</span>,
<span style="color:#a31515">"gid"</span>: <span style="color:#880000">0</span>,
<span style="color:#a31515">"group"</span>: <span style="color:#a31515">"root"</span>,
<span style="color:#a31515">"mode"</span>: <span style="color:#a31515">"0777"</span>,
<span style="color:#a31515">"owner"</span>: <span style="color:#a31515">"root"</span>,
<span style="color:#a31515">"size"</span>: <span style="color:#880000">7</span>,
<span style="color:#a31515">"src"</span>: <span style="color:#a31515">"aaa.jpg"</span>,
<span style="color:#a31515">"state"</span>: <span style="color:#a31515">"link"</span>,
<span style="color:#a31515">"uid"</span>: <span style="color:#880000">0</span>
}
<span style="color:#2b91af">192.168.37.133 | SUCCESS =></span> {
<span style="color:#a31515">"changed"</span>: true,
<span style="color:#a31515">"dest"</span>: <span style="color:#a31515">"/data/bbb.jpg"</span>,
<span style="color:#a31515">"gid"</span>: <span style="color:#880000">0</span>,
<span style="color:#a31515">"group"</span>: <span style="color:#a31515">"root"</span>,
<span style="color:#a31515">"mode"</span>: <span style="color:#a31515">"0777"</span>,
<span style="color:#a31515">"owner"</span>: <span style="color:#a31515">"root"</span>,
<span style="color:#a31515">"size"</span>: <span style="color:#880000">7</span>,
<span style="color:#a31515">"src"</span>: <span style="color:#a31515">"aaa.jpg"</span>,
<span style="color:#a31515">"state"</span>: <span style="color:#a31515">"link"</span>,
<span style="color:#a31515">"uid"</span>: <span style="color:#880000">0</span>
}
</code></span>
我们可以去查看一下:
<span style="color:#000000"><code class="language-x86asm">[root@server ~]# ansible web -m shell -a <span style="color:#a31515">'ls -l /data'</span>
<span style="color:#880000">192.168</span><span style="color:#880000">.37</span><span style="color:#880000">.122</span> | SUCCESS | rc=<span style="color:#880000">0</span> >>
total <span style="color:#880000">28</span>
-rw-r--r-- <span style="color:#880000">1</span> root root <span style="color:#880000">5649</span> <span style="color:#0000ff">Dec</span> <span style="color:#880000">5</span> <span style="color:#880000">13</span>:<span style="color:#880000">49</span> <span style="color:#0000ff">aaa</span><span style="color:#880000">.</span>jpg
lrwxrwxrwx <span style="color:#880000">1</span> root root <span style="color:#880000">7</span> <span style="color:#0000ff">Dec</span> <span style="color:#880000">6</span> <span style="color:#880000">10</span>:<span style="color:#880000">25</span> bbb<span style="color:#880000">.</span>jpg -> <span style="color:#0000ff">aaa</span><span style="color:#880000">.</span>jpg
<span style="color:#880000">192.168</span><span style="color:#880000">.37</span><span style="color:#880000">.133</span> | SUCCESS | rc=<span style="color:#880000">0</span> >>
total <span style="color:#880000">44</span>
-rw-r--r-- <span style="color:#880000">1</span> root root <span style="color:#880000">5649</span> <span style="color:#0000ff">Dec</span> <span style="color:#880000">4</span> <span style="color:#880000">14</span>:<span style="color:#880000">44</span> <span style="color:#0000ff">aaa</span><span style="color:#880000">.</span>jpg
lrwxrwxrwx <span style="color:#880000">1</span> root root <span style="color:#880000">7</span> <span style="color:#0000ff">Dec</span> <span style="color:#880000">5</span> <span style="color:#880000">10</span>:<span style="color:#880000">25</span> bbb<span style="color:#880000">.</span>jpg -> <span style="color:#0000ff">aaa</span><span style="color:#880000">.</span>jpg
</code></span>
我们的链接文件已经创建成功。
③ 删除文件
<span style="color:#000000"><code class="language-ruby">[root<span style="color:#008000">@server</span> ~]<span style="color:#008000"># ansible web -m file -a 'path=/data/a state=absent'</span>
<span style="color:#2b91af">192.168.37.122 | SUCCESS =></span> {
<span style="color:#a31515">"changed"</span>: true,
<span style="color:#a31515">"path"</span>: <span style="color:#a31515">"/data/a"</span>,
<span style="color:#a31515">"state"</span>: <span style="color:#a31515">"absent"</span>
}
<span style="color:#2b91af">192.168.37.133 | SUCCESS =></span> {
<span style="color:#a31515">"changed"</span>: true,
<span style="color:#a31515">"path"</span>: <span style="color:#a31515">"/data/a"</span>,
<span style="color:#a31515">"state"</span>: <span style="color:#a31515">"absent"</span>
}
</code></span>
我们可以查看一下:
<span style="color:#000000"><code class="language-haskell">[root@server ~]# ansible web -m shell -a 'ls /<span style="color:#0000ff">data</span>/a'
<span style="color:#880000">192.168</span>.<span style="color:#880000">37.122</span> | <span style="color:#a31515">FAILED</span> | rc=<span style="color:#880000">2</span> >>
<span style="color:#a31515">ls</span>: cannot access /<span style="color:#0000ff">data</span>/a: <span style="color:#a31515">No</span> such file or directory
<span style="color:#880000">192.168</span>.<span style="color:#880000">37.133</span> | <span style="color:#a31515">FAILED</span> | rc=<span style="color:#880000">2</span> >>
<span style="color:#a31515">ls</span>: cannot access /<span style="color:#0000ff">data</span>/a: <span style="color:#a31515">No</span> such file or directory
</code></span>
发现已经没有这个文件了。
6)fetch 模块
该模块用于从远程某主机获取(复制)文件到本地。
有两个选项:
dest
:用来存放文件的目录src
:在远程拉取的文件,并且必须是一个file,不能是目录
具体举例如下:
<span style="color:#000000"><code class="language-dart">[root<span style="color:#2b91af">@server</span> ~]# ansible web -m fetch -a <span style="color:#a31515">'src=/data/hello dest=/data'</span>
<span style="color:#880000">192.168</span><span style="color:#880000">.37</span><span style="color:#880000">.122</span> | SUCCESS => {
<span style="color:#a31515">"changed"</span>: <span style="color:#0000ff">true</span>,
<span style="color:#a31515">"checksum"</span>: <span style="color:#a31515">"22596363b3de40b06f981fb85d82312e8c0ed511"</span>,
<span style="color:#a31515">"dest"</span>: <span style="color:#a31515">"/data/192.168.37.122/data/hello"</span>,
<span style="color:#a31515">"md5sum"</span>: <span style="color:#a31515">"6f5902ac237024bdd0c176cb93063dc4"</span>,
<span style="color:#a31515">"remote_checksum"</span>: <span style="color:#a31515">"22596363b3de40b06f981fb85d82312e8c0ed511"</span>,
<span style="color:#a31515">"remote_md5sum"</span>: <span style="color:#0000ff">null</span>
}
<span style="color:#880000">192.168</span><span style="color:#880000">.37</span><span style="color:#880000">.133</span> | SUCCESS => {
<span style="color:#a31515">"changed"</span>: <span style="color:#0000ff">true</span>,
<span style="color:#a31515">"checksum"</span>: <span style="color:#a31515">"22596363b3de40b06f981fb85d82312e8c0ed511"</span>,
<span style="color:#a31515">"dest"</span>: <span style="color:#a31515">"/data/192.168.37.133/data/hello"</span>,
<span style="color:#a31515">"md5sum"</span>: <span style="color:#a31515">"6f5902ac237024bdd0c176cb93063dc4"</span>,
<span style="color:#a31515">"remote_checksum"</span>: <span style="color:#a31515">"22596363b3de40b06f981fb85d82312e8c0ed511"</span>,
<span style="color:#a31515">"remote_md5sum"</span>: <span style="color:#0000ff">null</span>
}
</code></span>
我们可以在本机上查看一下文件是否复制成功。要注意,文件保存的路径是我们设置的接收目录下的被管制主机ip
目录下:
<span style="color:#000000"><code class="language-haskell">[root@server ~]# cd /<span style="color:#0000ff">data</span>/
[root@server <span style="color:#0000ff">data</span>]# ls
<span style="color:#880000">1</span> <span style="color:#880000">192.168</span>.<span style="color:#880000">37.122</span> <span style="color:#880000">192.168</span>.<span style="color:#880000">37.133</span> fastdfs web
[root@server <span style="color:#0000ff">data</span>]# cd 192.168.37.122
[root@server <span style="color:#880000">192.168</span>.<span style="color:#880000">37.122</span>]# ls
<span style="color:#0000ff">data</span>
[root@server <span style="color:#880000">192.168</span>.<span style="color:#880000">37.122</span>]# cd <span style="color:#0000ff">data</span>/
[root@server <span style="color:#0000ff">data</span>]# ls
<span style="color:#a31515">hello</span>
[root@server <span style="color:#0000ff">data</span>]# pwd
/<span style="color:#0000ff">data</span>/192.168.37.122/<span style="color:#0000ff">data</span>
</code></span>
7)cron 模块
该模块适用于管理cron
计划任务的。
其使用的语法跟我们的crontab
文件中的语法一致,同时,可以指定以下选项:
day=
#日应该运行的工作( 1-31, *, */2, )hour=
# 小时 ( 0-23, *, */2, )minute=
#分钟( 0-59, *, */2, )month=
# 月( 1-12, *, /2, )weekday=
# 周 ( 0-6 for Sunday-Saturday,, )job=
#指明运行的命令是什么name=
#定时任务描述reboot
# 任务在重启时运行,不建议使用,建议使用special_timespecial_time
#特殊的时间范围,参数:reboot(重启时),annually(每年),monthly(每月),weekly(每周),daily(每天),hourly(每小时)state
#指定状态,present表示添加定时任务,也是默认设置,absent表示删除定时任务user
# 以哪个用户的身份执行
举例如下:
① 添加计划任务
<span style="color:#000000"><code class="language-ruby">[root<span style="color:#008000">@server</span> ~]<span style="color:#008000"># ansible web -m cron -a 'name="ntp update every 5 min" minute=*/5 job="/sbin/ntpdate 172.17.0.1 &> /dev/null"'</span>
<span style="color:#2b91af">192.168.37.122 | SUCCESS =></span> {
<span style="color:#a31515">"changed"</span>: true,
<span style="color:#a31515">"envs"</span>: [],
<span style="color:#a31515">"jobs"</span>: [
<span style="color:#a31515">"ntp update every 5 min"</span>
]
}
<span style="color:#2b91af">192.168.37.133 | SUCCESS =></span> {
<span style="color:#a31515">"changed"</span>: true,
<span style="color:#a31515">"envs"</span>: [],
<span style="color:#a31515">"jobs"</span>: [
<span style="color:#a31515">"ntp update every 5 min"</span>
]
}
</code></span>
我们可以去查看一下:
<span style="color:#000000"><code class="language-perl">[root@server ~]<span style="color:#008000"># ansible web -m shell -a 'crontab -l'</span>
<span style="color:#880000">192.168</span>.<span style="color:#880000">37.122</span> | SUCCESS | rc=<span style="color:#880000">0</span> >>
<span style="color:#008000">#Ansible: ntp update every 5 min</span>
*<span style="color:#ab5656">/5 * * * * /s</span>bin/ntpdate <span style="color:#880000">172.17</span>.<span style="color:#880000">0</span>.<span style="color:#880000">1</span> &> <span style="color:#ab5656">/dev/null</span>
<span style="color:#880000">192.168</span>.<span style="color:#880000">37.133</span> | SUCCESS | rc=<span style="color:#880000">0</span> >>
<span style="color:#008000">#Ansible: ntp update every 5 min</span>
*<span style="color:#ab5656">/5 * * * * /s</span>bin/ntpdate <span style="color:#880000">172.17</span>.<span style="color:#880000">0</span>.<span style="color:#880000">1</span> &> <span style="color:#ab5656">/dev/null</span>
</code></span>
可以看出,我们的计划任务已经设置成功了。
② 删除计划任务
如果我们的计划任务添加错误,想要删除的话,则执行以下操作:
首先我们查看一下现有的计划任务:
<span style="color:#000000"><code class="language-bash">[root@server ~]<span style="color:#008000"># ansible web -m shell -a 'crontab -l'</span>
192.168.37.122 | SUCCESS | rc=0 >>
<span style="color:#008000">#Ansible: ntp update every 5 min</span>
*/5 * * * * /sbin/ntpdate 172.17.0.1 &> /dev/null
<span style="color:#008000">#Ansible: df everyday</span>
* 15 * * * <span style="color:#0000ff">df</span> -lh >> /tmp/disk_total &> /dev/null
192.168.37.133 | SUCCESS | rc=0 >>
<span style="color:#008000">#Ansible: ntp update every 5 min</span>
*/5 * * * * /sbin/ntpdate 172.17.0.1 &> /dev/null
<span style="color:#008000">#Ansible: df everyday</span>
* 15 * * * <span style="color:#0000ff">df</span> -lh >> /tmp/disk_total &> /dev/null
</code></span>
然后执行删除操作:
<span style="color:#000000"><code class="language-ruby">[root<span style="color:#008000">@server</span> ~]<span style="color:#008000"># ansible web -m cron -a 'name="df everyday" hour=15 job="df -lh >> /tmp/disk_total &> /dev/null" state=absent'</span>
<span style="color:#2b91af">192.168.37.122 | SUCCESS =></span> {
<span style="color:#a31515">"changed"</span>: true,
<span style="color:#a31515">"envs"</span>: [],
<span style="color:#a31515">"jobs"</span>: [
<span style="color:#a31515">"ntp update every 5 min"</span>
]
}
<span style="color:#2b91af">192.168.37.133 | SUCCESS =></span> {
<span style="color:#a31515">"changed"</span>: true,
<span style="color:#a31515">"envs"</span>: [],
<span style="color:#a31515">"jobs"</span>: [
<span style="color:#a31515">"ntp update every 5 min"</span>
]
}
</code></span>
删除完成后,我们再查看一下现有的计划任务确认一下:
<span style="color:#000000"><code class="language-perl">[root@server ~]<span style="color:#008000"># ansible web -m shell -a 'crontab -l'</span>
<span style="color:#880000">192.168</span>.<span style="color:#880000">37.122</span> | SUCCESS | rc=<span style="color:#880000">0</span> >>
<span style="color:#008000">#Ansible: ntp update every 5 min</span>
*<span style="color:#ab5656">/5 * * * * /s</span>bin/ntpdate <span style="color:#880000">172.17</span>.<span style="color:#880000">0</span>.<span style="color:#880000">1</span> &> <span style="color:#ab5656">/dev/null</span>
<span style="color:#880000">192.168</span>.<span style="color:#880000">37.133</span> | SUCCESS | rc=<span style="color:#880000">0</span> >>
<span style="color:#008000">#Ansible: ntp update every 5 min</span>
*<span style="color:#ab5656">/5 * * * * /s</span>bin/ntpdate <span style="color:#880000">172.17</span>.<span style="color:#880000">0</span>.<span style="color:#880000">1</span> &> <span style="color:#ab5656">/dev/null</span>
</code></span>
我们的删除操作已经成功。
8)yum 模块
顾名思义,该模块主要用于软件的安装。
其选项如下:
name=
#所安装的包的名称state=
#present
--->安装,latest
--->安装最新的,absent
---> 卸载软件。update_cache
#强制更新yum的缓存conf_file
#指定远程yum安装时所依赖的配置文件(安装本地已有的包)。disable_pgp_check
#是否禁止GPG checking,只用于present
orlatest
。disablerepo
#临时禁止使用yum库。 只用于安装或更新时。enablerepo
#临时使用的yum库。只用于安装或更新时。
下面我们就来安装一个包试试看:
<span style="color:#000000"><code class="language-swift">[root<span style="color:#2b91af">@server</span> <span style="color:#ab5656">~</span>]# ansible web <span style="color:#ab5656">-</span>m yum <span style="color:#ab5656">-</span>a 'name<span style="color:#ab5656">=</span>htop state<span style="color:#ab5656">=</span>present'
<span style="color:#880000">192.168</span>.<span style="color:#880000">37.122</span> <span style="color:#ab5656">|</span> <span style="color:#a31515">SUCCESS</span> <span style="color:#ab5656">=></span> {
<span style="color:#a31515">"changed"</span>: <span style="color:#a31515">true</span>,
<span style="color:#a31515">"msg"</span>: <span style="color:#a31515">""</span>,
<span style="color:#a31515">"rc"</span>: <span style="color:#880000">0</span>,
<span style="color:#a31515">"results"</span>: [
<span style="color:#a31515">"Loaded plugins: fastestmirror, langpacks\nLoading mirror speeds from cached hostfile\nResolving Dependencies\n--> Running transaction check\n---> Package htop.x86_64 0:2.0.2-1.el7 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n htop x86_64 2.0.2-1.el7 epel 98 k\n\nTransaction Summary\n================================================================================\nInstall 1 Package\n\nTotal download size: 98 k\nInstalled size: 207 k\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : htop-2.0.2-1.el7.x86_64 1/1 \n Verifying : htop-2.0.2-1.el7.x86_64 1/1 \n\nInstalled:\n htop.x86_64 0:2.0.2-1.el7 \n\nComplete!\n"</span>
]
}
<span style="color:#880000">192.168</span>.<span style="color:#880000">37.133</span> <span style="color:#ab5656">|</span> <span style="color:#a31515">SUCCESS</span> <span style="color:#ab5656">=></span> {
<span style="color:#a31515">"changed"</span>: <span style="color:#a31515">true</span>,
<span style="color:#a31515">"msg"</span>: <span style="color:#a31515">"Warning: RPMDB altered outside of yum.\n** Found 3 pre-existing rpmdb problem(s), 'yum check' output follows:\nipa-client-4.4.0-12.el7.centos.x86_64 has installed conflicts freeipa-client: ipa-client-4.4.0-12.el7.centos.x86_64\nipa-client-common-4.4.0-12.el7.centos.noarch has installed conflicts freeipa-client-common: ipa-client-common-4.4.0-12.el7.centos.noarch\nipa-common-4.4.0-12.el7.centos.noarch has installed conflicts freeipa-common: ipa-common-4.4.0-12.el7.centos.noarch\n"</span>,
<span style="color:#a31515">"rc"</span>: <span style="color:#880000">0</span>,
<span style="color:#a31515">"results"</span>: [
<span style="color:#a31515">"Loaded plugins: fastestmirror, langpacks\nLoading mirror speeds from cached hostfile\nResolving Dependencies\n--> Running transaction check\n---> Package htop.x86_64 0:2.0.2-1.el7 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n htop x86_64 2.0.2-1.el7 epel 98 k\n\nTransaction Summary\n================================================================================\nInstall 1 Package\n\nTotal download size: 98 k\nInstalled size: 207 k\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : htop-2.0.2-1.el7.x86_64 1/1 \n Verifying : htop-2.0.2-1.el7.x86_64 1/1 \n\nInstalled:\n htop.x86_64 0:2.0.2-1.el7 \n\nComplete!\n"</span>
]
}
</code></span>
安装成功。
9)service 模块
该模块用于服务程序的管理。
其主要选项如下:
arguments
#命令行提供额外的参数enabled
#设置开机启动。name=
#服务名称runlevel
#开机启动的级别,一般不用指定。sleep
#在重启服务的过程中,是否等待。如在服务关闭以后等待2秒再启动。(定义在剧本中。)state
#有四种状态,分别为:started
--->启动服务,stopped
--->停止服务,restarted
--->重启服务,reloaded
--->重载配置
下面是一些例子:
① 开启服务并设置自启动
<span style="color:#000000"><code class="language-dart">[root<span style="color:#2b91af">@server</span> ~]# ansible web -m service -a <span style="color:#a31515">'name=nginx state=started enabled=true'</span>
<span style="color:#880000">192.168</span><span style="color:#880000">.37</span><span style="color:#880000">.122</span> | SUCCESS => {
<span style="color:#a31515">"changed"</span>: <span style="color:#0000ff">true</span>,
<span style="color:#a31515">"enabled"</span>: <span style="color:#0000ff">true</span>,
<span style="color:#a31515">"name"</span>: <span style="color:#a31515">"nginx"</span>,
<span style="color:#a31515">"state"</span>: <span style="color:#a31515">"started"</span>,
……
}
<span style="color:#880000">192.168</span><span style="color:#880000">.37</span><span style="color:#880000">.133</span> | SUCCESS => {
<span style="color:#a31515">"changed"</span>: <span style="color:#0000ff">true</span>,
<span style="color:#a31515">"enabled"</span>: <span style="color:#0000ff">true</span>,
<span style="color:#a31515">"name"</span>: <span style="color:#a31515">"nginx"</span>,
<span style="color:#a31515">"state"</span>: <span style="color:#a31515">"started"</span>,
……
}
</code></span>
我们可以去查看一下端口是否打开:
<span style="color:#000000"><code class="language-mipsasm">[root@server ~]<span style="color:#008000"># ansible web -m shell -a 'ss -ntl'</span>
<span style="color:#880000">192</span>.<span style="color:#880000">168</span>.<span style="color:#880000">37</span>.<span style="color:#880000">122</span> <span style="color:#a31515">| SUCCESS |</span> rc=<span style="color:#880000">0</span> >>
State Recv-Q Send-Q Local <span style="color:#0000ff">Address:Port </span> Peer <span style="color:#0000ff">Address:Port </span>
LISTEN <span style="color:#880000">0</span> <span style="color:#880000">128</span> *:<span style="color:#880000">80</span> *:*
<span style="color:#880000">192</span>.<span style="color:#880000">168</span>.<span style="color:#880000">37</span>.<span style="color:#880000">133</span> <span style="color:#a31515">| SUCCESS |</span> rc=<span style="color:#880000">0</span> >>
State Recv-Q Send-Q Local <span style="color:#0000ff">Address:Port </span> Peer <span style="color:#0000ff">Address:Port </span>
LISTEN <span style="color:#880000">0</span> <span style="color:#880000">128</span> *:<span style="color:#880000">80</span> *:*
</code></span>
可以看出我们的80端口已经打开。
② 关闭服务
我们也可以通过该模块来关闭我们的服务:
<span style="color:#000000"><code class="language-ruby">[root<span style="color:#008000">@server</span> ~]<span style="color:#008000"># ansible web -m service -a 'name=nginx state=stopped'</span>
<span style="color:#2b91af">192.168.37.122 | SUCCESS =></span> {
<span style="color:#a31515">"changed"</span>: true,
<span style="color:#a31515">"name"</span>: <span style="color:#a31515">"nginx"</span>,
<span style="color:#a31515">"state"</span>: <span style="color:#a31515">"stopped"</span>,
……
}
<span style="color:#2b91af">192.168.37.133 | SUCCESS =></span> {
<span style="color:#a31515">"changed"</span>: true,
<span style="color:#a31515">"name"</span>: <span style="color:#a31515">"nginx"</span>,
<span style="color:#a31515">"state"</span>: <span style="color:#a31515">"stopped"</span>,
……
}
</code></span>
一样的,我们来查看一下端口:
<span style="color:#000000"><code class="language-csharp">[<span style="color:#2b91af">root@server ~</span>]<span style="color:#2b91af"># ansible web -m shell -a 'ss -ntl | grep 80'</span>
<span style="color:#880000">192.168</span><span style="color:#880000">.37</span><span style="color:#880000">.122</span> | FAILED | rc=<span style="color:#880000">1</span> >>
<span style="color:#880000">192.168</span><span style="color:#880000">.37</span><span style="color:#880000">.133</span> | FAILED | rc=<span style="color:#880000">1</span> >>
</code></span>
可以看出,我们已经没有80端口了,说明我们的nginx服务已经关闭了。
10)user 模块
该模块主要是用来管理用户账号。
其主要选项如下:
comment
# 用户的描述信息createhome
# 是否创建家目录force
# 在使用state=absent时, 行为与userdel –force一致.group
# 指定基本组groups
# 指定附加组,如果指定为(groups=)表示删除所有组home
# 指定用户家目录move_home
# 如果设置为home=时, 试图将用户主目录移动到指定的目录name
# 指定用户名non_unique
# 该选项允许改变非唯一的用户ID值password
# 指定用户密码remove
# 在使用state=absent时, 行为是与userdel –remove一致shell
# 指定默认shellstate
# 设置帐号状态,不指定为创建,指定值为absent表示删除system
# 当创建一个用户,设置这个用户是系统用户。这个设置不能更改现有用户uid
# 指定用户的uid
举例如下:
① 添加一个用户并指定其 uid
<span style="color:#000000"><code class="language-smalltalk">[root@server ~]# ansible web -m user -a <span style="color:#a31515">'name=keer uid=11111'</span>
<span style="color:#880000">192.168</span><span style="color:#880000">.37</span><span style="color:#880000">.122</span> | <span style="color:#a31515">SUCCESS</span> => {
<span style="color:#008000">"changed"</span>: <span style="color:#0000ff">true</span>,
<span style="color:#008000">"comment"</span>: <span style="color:#008000">""</span>,
<span style="color:#008000">"createhome"</span>: <span style="color:#0000ff">true</span>,
<span style="color:#008000">"group"</span>: <span style="color:#880000">11111</span>,
<span style="color:#008000">"home"</span>: <span style="color:#008000">"/home/keer"</span>,
<span style="color:#008000">"name"</span>: <span style="color:#008000">"keer"</span>,
<span style="color:#008000">"shell"</span>: <span style="color:#008000">"/bin/bash"</span>,
<span style="color:#008000">"state"</span>: <span style="color:#008000">"present"</span>,
<span style="color:#008000">"stderr"</span>: <span style="color:#008000">"useradd: warning: the home directory already exists.\nNot copying any file from skel directory into it.\nCreating mailbox file: File exists\n"</span>,
<span style="color:#008000">"system"</span>: <span style="color:#0000ff">false</span>,
<span style="color:#008000">"uid"</span>: <span style="color:#880000">11111</span>
}
<span style="color:#880000">192.168</span><span style="color:#880000">.37</span><span style="color:#880000">.133</span> | <span style="color:#a31515">SUCCESS</span> => {
<span style="color:#008000">"changed"</span>: <span style="color:#0000ff">true</span>,
<span style="color:#008000">"comment"</span>: <span style="color:#008000">""</span>,
<span style="color:#008000">"createhome"</span>: <span style="color:#0000ff">true</span>,
<span style="color:#008000">"group"</span>: <span style="color:#880000">11111</span>,
<span style="color:#008000">"home"</span>: <span style="color:#008000">"/home/keer"</span>,
<span style="color:#008000">"name"</span>: <span style="color:#008000">"keer"</span>,
<span style="color:#008000">"shell"</span>: <span style="color:#008000">"/bin/bash"</span>,
<span style="color:#008000">"state"</span>: <span style="color:#008000">"present"</span>,
<span style="color:#008000">"stderr"</span>: <span style="color:#008000">"useradd: warning: the home directory already exists.\nNot copying any file from skel directory into it.\nCreating mailbox file: File exists\n"</span>,
<span style="color:#008000">"system"</span>: <span style="color:#0000ff">false</span>,
<span style="color:#008000">"uid"</span>: <span style="color:#880000">11111</span>
}
</code></span>
添加完成,我们可以去查看一下:
<span style="color:#000000"><code class="language-ruby">[root<span style="color:#008000">@server</span> ~]<span style="color:#008000"># ansible web -m shell -a 'cat /etc/passwd |grep keer'</span>
<span style="color:#880000">192.168</span>.<span style="color:#880000">37.122</span> | SUCCESS | rc=<span style="color:#880000">0</span> >>
<span style="color:#00b0e8">keer:</span><span style="color:#00b0e8">x:</span><span style="color:#880000">11111</span><span style="color:#00b0e8">:</span><span style="color:#880000">11111</span><span style="color:#00b0e8">:</span><span style="color:#00b0e8">:/home/keer</span><span style="color:#00b0e8">:/bin/bash</span>
<span style="color:#880000">192.168</span>.<span style="color:#880000">37.133</span> | SUCCESS | rc=<span style="color:#880000">0</span> >>
<span style="color:#00b0e8">keer:</span><span style="color:#00b0e8">x:</span><span style="color:#880000">11111</span><span style="color:#00b0e8">:</span><span style="color:#880000">11111</span><span style="color:#00b0e8">:</span><span style="color:#00b0e8">:/home/keer</span><span style="color:#00b0e8">:/bin/bash</span>
</code></span>
② 删除用户
<span style="color:#000000"><code class="language-dart">[root<span style="color:#2b91af">@server</span> ~]# ansible web -m user -a <span style="color:#a31515">'name=keer state=absent'</span>
<span style="color:#880000">192.168</span><span style="color:#880000">.37</span><span style="color:#880000">.122</span> | SUCCESS => {
<span style="color:#a31515">"changed"</span>: <span style="color:#0000ff">true</span>,
<span style="color:#a31515">"force"</span>: <span style="color:#0000ff">false</span>,
<span style="color:#a31515">"name"</span>: <span style="color:#a31515">"keer"</span>,
<span style="color:#a31515">"remove"</span>: <span style="color:#0000ff">false</span>,
<span style="color:#a31515">"state"</span>: <span style="color:#a31515">"absent"</span>
}
<span style="color:#880000">192.168</span><span style="color:#880000">.37</span><span style="color:#880000">.133</span> | SUCCESS => {
<span style="color:#a31515">"changed"</span>: <span style="color:#0000ff">true</span>,
<span style="color:#a31515">"force"</span>: <span style="color:#0000ff">false</span>,
<span style="color:#a31515">"name"</span>: <span style="color:#a31515">"keer"</span>,
<span style="color:#a31515">"remove"</span>: <span style="color:#0000ff">false</span>,
<span style="color:#a31515">"state"</span>: <span style="color:#a31515">"absent"</span>
}
</code></span>
一样的,删除之后,我们去看一下:
<span style="color:#000000"><code class="language-csharp">[<span style="color:#2b91af">root@server ~</span>]<span style="color:#2b91af"># ansible web -m shell -a 'cat /etc/passwd |grep keer'</span>
<span style="color:#880000">192.168</span><span style="color:#880000">.37</span><span style="color:#880000">.122</span> | FAILED | rc=<span style="color:#880000">1</span> >>
<span style="color:#880000">192.168</span><span style="color:#880000">.37</span><span style="color:#880000">.133</span> | FAILED | rc=<span style="color:#880000">1</span> >>
</code></span>
发现已经没有这个用户了。
11)group 模块
该模块主要用于添加或删除组。
常用的选项如下:
gid=
#设置组的GID号name=
#指定组的名称state=
#指定组的状态,默认为创建,设置值为absent
为删除system=
#设置值为yes
,表示创建为系统组
举例如下:
① 创建组
<span style="color:#000000"><code class="language-dart">[root<span style="color:#2b91af">@server</span> ~]# ansible web -m group -a <span style="color:#a31515">'name=sanguo gid=12222'</span>
<span style="color:#880000">192.168</span><span style="color:#880000">.37</span><span style="color:#880000">.122</span> | SUCCESS => {
<span style="color:#a31515">"changed"</span>: <span style="color:#0000ff">true</span>,
<span style="color:#a31515">"gid"</span>: <span style="color:#880000">12222</span>,
<span style="color:#a31515">"name"</span>: <span style="color:#a31515">"sanguo"</span>,
<span style="color:#a31515">"state"</span>: <span style="color:#a31515">"present"</span>,
<span style="color:#a31515">"system"</span>: <span style="color:#0000ff">false</span>
}
<span style="color:#880000">192.168</span><span style="color:#880000">.37</span><span style="color:#880000">.133</span> | SUCCESS => {
<span style="color:#a31515">"changed"</span>: <span style="color:#0000ff">true</span>,
<span style="color:#a31515">"gid"</span>: <span style="color:#880000">12222</span>,
<span style="color:#a31515">"name"</span>: <span style="color:#a31515">"sanguo"</span>,
<span style="color:#a31515">"state"</span>: <span style="color:#a31515">"present"</span>,
<span style="color:#a31515">"system"</span>: <span style="color:#0000ff">false</span>
}
</code></span>
创建过后,我们来查看一下:
<span style="color:#000000"><code class="language-makefile">[root@server ~]<span style="color:#008000"># ansible web -m shell -a 'cat /etc/group | grep 12222' </span>
192.168.37.122 | SUCCESS | rc=0 >>
<span style="color:#a31515">sanguo:x:12222:</span>
192.168.37.133 | SUCCESS | rc=0 >>
<span style="color:#a31515">sanguo:x:12222:</span>
</code></span>
可以看出,我们的组已经创建成功了。
② 删除组
<span style="color:#000000"><code class="language-ruby">[root<span style="color:#008000">@server</span> ~]<span style="color:#008000"># ansible web -m group -a 'name=sanguo state=absent'</span>
<span style="color:#2b91af">192.168.37.122 | SUCCESS =></span> {
<span style="color:#a31515">"changed"</span>: true,
<span style="color:#a31515">"name"</span>: <span style="color:#a31515">"sanguo"</span>,
<span style="color:#a31515">"state"</span>: <span style="color:#a31515">"absent"</span>
}
<span style="color:#2b91af">192.168.37.133 | SUCCESS =></span> {
<span style="color:#a31515">"changed"</span>: true,
<span style="color:#a31515">"name"</span>: <span style="color:#a31515">"sanguo"</span>,
<span style="color:#a31515">"state"</span>: <span style="color:#a31515">"absent"</span>
}
</code></span>
照例查看一下:
<span style="color:#000000"><code class="language-csharp">[<span style="color:#2b91af">root@server ~</span>]<span style="color:#2b91af"># ansible web -m shell -a 'cat /etc/group | grep 12222' </span>
<span style="color:#880000">192.168</span><span style="color:#880000">.37</span><span style="color:#880000">.122</span> | FAILED | rc=<span style="color:#880000">1</span> >>
<span style="color:#880000">192.168</span><span style="color:#880000">.37</span><span style="color:#880000">.133</span> | FAILED | rc=<span style="color:#880000">1</span> >>
</code></span>
已经没有这个组的相关信息了。
12)script 模块
该模块用于将本机的脚本在被管理端的机器上运行。
该模块直接指定脚本的路径即可,我们通过例子来看一看到底如何使用的:
首先,我们写一个脚本,并给其加上执行权限:
<span style="color:#000000"><code class="language-bash">[root@server ~]<span style="color:#008000"># vim /tmp/df.sh</span>
<span style="color:#008000">#!/bin/bash</span>
<span style="color:#0000ff">date</span> >> /tmp/disk_total.log
<span style="color:#0000ff">df</span> -lh >> /tmp/disk_total.log
[root@server ~]<span style="color:#008000"># chmod +x /tmp/df.sh </span>
</code></span>
然后,我们直接运行命令来实现在被管理端执行该脚本:
<span style="color:#000000"><code class="language-swift">[root<span style="color:#2b91af">@server</span> <span style="color:#ab5656">~</span>]# ansible web <span style="color:#ab5656">-</span>m script <span style="color:#ab5656">-</span>a '<span style="color:#ab5656">/</span>tmp<span style="color:#ab5656">/</span>df.sh'
<span style="color:#880000">192.168</span>.<span style="color:#880000">37.122</span> <span style="color:#ab5656">|</span> <span style="color:#a31515">SUCCESS</span> <span style="color:#ab5656">=></span> {
<span style="color:#a31515">"changed"</span>: <span style="color:#a31515">true</span>,
<span style="color:#a31515">"rc"</span>: <span style="color:#880000">0</span>,
<span style="color:#a31515">"stderr"</span>: <span style="color:#a31515">"Shared connection to 192.168.37.122 closed.\r\n"</span>,
<span style="color:#a31515">"stdout"</span>: <span style="color:#a31515">""</span>,
<span style="color:#a31515">"stdout_lines"</span>: []
}
<span style="color:#880000">192.168</span>.<span style="color:#880000">37.133</span> <span style="color:#ab5656">|</span> <span style="color:#a31515">SUCCESS</span> <span style="color:#ab5656">=></span> {
<span style="color:#a31515">"changed"</span>: <span style="color:#a31515">true</span>,
<span style="color:#a31515">"rc"</span>: <span style="color:#880000">0</span>,
<span style="color:#a31515">"stderr"</span>: <span style="color:#a31515">"Shared connection to 192.168.37.133 closed.\r\n"</span>,
<span style="color:#a31515">"stdout"</span>: <span style="color:#a31515">""</span>,
<span style="color:#a31515">"stdout_lines"</span>: []
}
</code></span>
照例查看一下文件内容:
<span style="color:#000000"><code class="language-bash">[root@server ~]<span style="color:#008000"># ansible web -m shell -a 'cat /tmp/disk_total.log'</span>
192.168.37.122 | SUCCESS | rc=0 >>
Tue Dec 5 15:58:21 CST 2017
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 47G 4.4G 43G 10% /
devtmpfs 978M 0 978M 0% /dev
tmpfs 993M 84K 993M 1% /dev/shm
tmpfs 993M 9.1M 984M 1% /run
tmpfs 993M 0 993M 0% /sys/fs/cgroup
/dev/sda3 47G 33M 47G 1% /app
/dev/sda1 950M 153M 798M 17% /boot
tmpfs 199M 16K 199M 1% /run/user/42
tmpfs 199M 0 199M 0% /run/user/0
192.168.37.133 | SUCCESS | rc=0 >>
Tue Dec 5 15:58:21 CST 2017
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 46G 4.1G 40G 10% /
devtmpfs 898M 0 898M 0% /dev
tmpfs 912M 84K 912M 1% /dev/shm
tmpfs 912M 9.0M 903M 1% /run
tmpfs 912M 0 912M 0% /sys/fs/cgroup
/dev/sda3 3.7G 15M 3.4G 1% /app
/dev/sda1 1.9G 141M 1.6G 9% /boot
tmpfs 183M 16K 183M 1% /run/user/42
tmpfs 183M 0 183M 0% /run/user/0
</code></span>
可以看出已经执行成功了。
13)setup 模块
该模块主要用于收集信息,是通过调用facts组件来实现的。
facts组件是Ansible用于采集被管机器设备信息的一个功能,我们可以使用setup模块查机器的所有facts信息,可以使用filter来查看指定信息。整个facts信息被包装在一个JSON格式的数据结构中,ansible_facts是最上层的值。
facts就是变量,内建变量 。每个主机的各种信息,cpu颗数、内存大小等。会存在facts中的某个变量中。调用后返回很多对应主机的信息,在后面的操作中可以根据不同的信息来做不同的操作。如redhat系列用yum安装,而debian系列用apt来安装软件。
① 查看信息
我们可以直接用命令获取到变量的值,具体我们来看看例子:
<span style="color:#000000"><code class="language-yaml">[<span style="color:#a31515">root@server</span> <span style="color:#a31515">~</span>]<span style="color:#008000"># ansible web -m setup -a 'filter="*mem*"' #查看内存</span>
<span style="color:#880000">192.168</span><span style="color:#880000">.37</span><span style="color:#880000">.122</span> <span style="color:#a31515">|</span> <span style="color:#a31515">SUCCESS</span> <span style="color:#a31515">=></span> {
<span style="color:#ff0000">"ansible_facts":</span> {
<span style="color:#ff0000">"ansible_memfree_mb":</span> <span style="color:#880000">1116</span>,
<span style="color:#ff0000">"ansible_memory_mb":</span> {
<span style="color:#ff0000">"nocache":</span> {
<span style="color:#ff0000">"free":</span> <span style="color:#880000">1397</span>,
<span style="color:#ff0000">"used":</span> <span style="color:#880000">587</span>
},
<span style="color:#ff0000">"real":</span> {
<span style="color:#ff0000">"free":</span> <span style="color:#880000">1116</span>,
<span style="color:#ff0000">"total":</span> <span style="color:#880000">1984</span>,
<span style="color:#ff0000">"used":</span> <span style="color:#880000">868</span>
},
<span style="color:#ff0000">"swap":</span> {
<span style="color:#ff0000">"cached":</span> <span style="color:#880000">0</span>,
<span style="color:#ff0000">"free":</span> <span style="color:#880000">3813</span>,
<span style="color:#ff0000">"total":</span> <span style="color:#880000">3813</span>,
<span style="color:#ff0000">"used":</span> <span style="color:#880000">0</span>
}
},
<span style="color:#ff0000">"ansible_memtotal_mb":</span> <span style="color:#880000">1984</span>
},
<span style="color:#ff0000">"changed":</span> <span style="color:#a31515">false</span>
}
<span style="color:#880000">192.168</span><span style="color:#880000">.37</span><span style="color:#880000">.133</span> <span style="color:#a31515">|</span> <span style="color:#a31515">SUCCESS</span> <span style="color:#a31515">=></span> {
<span style="color:#ff0000">"ansible_facts":</span> {
<span style="color:#ff0000">"ansible_memfree_mb":</span> <span style="color:#880000">1203</span>,
<span style="color:#ff0000">"ansible_memory_mb":</span> {
<span style="color:#ff0000">"nocache":</span> {
<span style="color:#ff0000">"free":</span> <span style="color:#880000">1470</span>,
<span style="color:#ff0000">"used":</span> <span style="color:#880000">353</span>
},
<span style="color:#ff0000">"real":</span> {
<span style="color:#ff0000">"free":</span> <span style="color:#880000">1203</span>,
<span style="color:#ff0000">"total":</span> <span style="color:#880000">1823</span>,
<span style="color:#ff0000">"used":</span> <span style="color:#880000">620</span>
},
<span style="color:#ff0000">"swap":</span> {
<span style="color:#ff0000">"cached":</span> <span style="color:#880000">0</span>,
<span style="color:#ff0000">"free":</span> <span style="color:#880000">3813</span>,
<span style="color:#ff0000">"total":</span> <span style="color:#880000">3813</span>,
<span style="color:#ff0000">"used":</span> <span style="color:#880000">0</span>
}
},
<span style="color:#ff0000">"ansible_memtotal_mb":</span> <span style="color:#880000">1823</span>
},
<span style="color:#ff0000">"changed":</span> <span style="color:#a31515">false</span>
}
</code></span>
我们可以通过命令查看一下内存的大小以确认一下是否一致:
<span style="color:#000000"><code class="language-yaml">[<span style="color:#a31515">root@server</span> <span style="color:#a31515">~</span>]<span style="color:#008000"># ansible web -m shell -a 'free -m'</span>
<span style="color:#880000">192.168</span><span style="color:#880000">.37</span><span style="color:#880000">.122</span> <span style="color:#a31515">|</span> <span style="color:#a31515">SUCCESS</span> <span style="color:#a31515">|</span> <span style="color:#a31515">rc=0</span> <span style="color:#a31515">>></span>
<span style="color:#a31515">total</span> <span style="color:#a31515">used</span> <span style="color:#a31515">free</span> <span style="color:#a31515">shared</span> <span style="color:#a31515">buff/cache</span> <span style="color:#a31515">available</span>
<span style="color:#ff0000">Mem:</span> <span style="color:#880000">1984 </span><span style="color:#880000">404</span> <span style="color:#880000">1122 </span><span style="color:#880000">9</span> <span style="color:#880000">457</span> <span style="color:#880000">1346</span>
<span style="color:#ff0000">Swap:</span> <span style="color:#880000">3813 </span><span style="color:#880000">0</span> <span style="color:#880000">3813</span>
<span style="color:#880000">192.168</span><span style="color:#880000">.37</span><span style="color:#880000">.133</span> <span style="color:#a31515">|</span> <span style="color:#a31515">SUCCESS</span> <span style="color:#a31515">|</span> <span style="color:#a31515">rc=0</span> <span style="color:#a31515">>></span>
<span style="color:#a31515">total</span> <span style="color:#a31515">used</span> <span style="color:#a31515">free</span> <span style="color:#a31515">shared</span> <span style="color:#a31515">buff/cache</span> <span style="color:#a31515">available</span>
<span style="color:#ff0000">Mem:</span> <span style="color:#880000">1823 </span><span style="color:#880000">292</span> <span style="color:#880000">1207 </span><span style="color:#880000">9</span> <span style="color:#880000">323</span> <span style="color:#880000">1351</span>
<span style="color:#ff0000">Swap:</span> <span style="color:#880000">3813 </span><span style="color:#880000">0</span> <span style="color:#880000">3813</span>
</code></span>
可以看出信息是一致的。
② 保存信息
我们的setup模块还有一个很好用的功能就是可以保存我们所筛选的信息至我们的主机上,同时,文件名为我们被管制的主机的IP,这样方便我们知道是哪台机器出的问题。
我们可以看一看例子:
<span style="color:#000000"><code class="language-yaml">[<span style="color:#a31515">root@server</span> <span style="color:#a31515">tmp</span>]<span style="color:#008000"># ansible web -m setup -a 'filter="*mem*"' --tree /tmp/facts</span>
<span style="color:#880000">192.168</span><span style="color:#880000">.37</span><span style="color:#880000">.122</span> <span style="color:#a31515">|</span> <span style="color:#a31515">SUCCESS</span> <span style="color:#a31515">=></span> {
<span style="color:#ff0000">"ansible_facts":</span> {
<span style="color:#ff0000">"ansible_memfree_mb":</span> <span style="color:#880000">1115</span>,
<span style="color:#ff0000">"ansible_memory_mb":</span> {
<span style="color:#ff0000">"nocache":</span> {
<span style="color:#ff0000">"free":</span> <span style="color:#880000">1396</span>,
<span style="color:#ff0000">"used":</span> <span style="color:#880000">588</span>
},
<span style="color:#ff0000">"real":</span> {
<span style="color:#ff0000">"free":</span> <span style="color:#880000">1115</span>,
<span style="color:#ff0000">"total":</span> <span style="color:#880000">1984</span>,
<span style="color:#ff0000">"used":</span> <span style="color:#880000">869</span>
},
<span style="color:#ff0000">"swap":</span> {
<span style="color:#ff0000">"cached":</span> <span style="color:#880000">0</span>,
<span style="color:#ff0000">"free":</span> <span style="color:#880000">3813</span>,
<span style="color:#ff0000">"total":</span> <span style="color:#880000">3813</span>,
<span style="color:#ff0000">"used":</span> <span style="color:#880000">0</span>
}
},
<span style="color:#ff0000">"ansible_memtotal_mb":</span> <span style="color:#880000">1984</span>
},
<span style="color:#ff0000">"changed":</span> <span style="color:#a31515">false</span>
}
<span style="color:#880000">192.168</span><span style="color:#880000">.37</span><span style="color:#880000">.133</span> <span style="color:#a31515">|</span> <span style="color:#a31515">SUCCESS</span> <span style="color:#a31515">=></span> {
<span style="color:#ff0000">"ansible_facts":</span> {
<span style="color:#ff0000">"ansible_memfree_mb":</span> <span style="color:#880000">1199</span>,
<span style="color:#ff0000">"ansible_memory_mb":</span> {
<span style="color:#ff0000">"nocache":</span> {
<span style="color:#ff0000">"free":</span> <span style="color:#880000">1467</span>,
<span style="color:#ff0000">"used":</span> <span style="color:#880000">356</span>
},
<span style="color:#ff0000">"real":</span> {
<span style="color:#ff0000">"free":</span> <span style="color:#880000">1199</span>,
<span style="color:#ff0000">"total":</span> <span style="color:#880000">1823</span>,
<span style="color:#ff0000">"used":</span> <span style="color:#880000">624</span>
},
<span style="color:#ff0000">"swap":</span> {
<span style="color:#ff0000">"cached":</span> <span style="color:#880000">0</span>,
<span style="color:#ff0000">"free":</span> <span style="color:#880000">3813</span>,
<span style="color:#ff0000">"total":</span> <span style="color:#880000">3813</span>,
<span style="color:#ff0000">"used":</span> <span style="color:#880000">0</span>
}
},
<span style="color:#ff0000">"ansible_memtotal_mb":</span> <span style="color:#880000">1823</span>
},
<span style="color:#ff0000">"changed":</span> <span style="color:#a31515">false</span>
}
</code></span>
然后我们可以去查看一下:
<span style="color:#000000"><code class="language-yaml">[<span style="color:#a31515">root@server</span> <span style="color:#a31515">~</span>]<span style="color:#008000"># cd /tmp/facts/</span>
[<span style="color:#a31515">root@server</span> <span style="color:#a31515">facts</span>]<span style="color:#008000"># ls</span>
<span style="color:#880000">192.168</span><span style="color:#880000">.37</span><span style="color:#880000">.122</span> <span style="color:#880000">192.168</span><span style="color:#880000">.37</span><span style="color:#880000">.133</span>
[<span style="color:#a31515">root@server</span> <span style="color:#a31515">facts</span>]<span style="color:#008000"># cat 192.168.37.122 </span>
{<span style="color:#ff0000">"ansible_facts":</span> {<span style="color:#ff0000">"ansible_memfree_mb":</span> <span style="color:#880000">1115</span>, <span style="color:#ff0000">"ansible_memory_mb":</span> {<span style="color:#ff0000">"nocache":</span> {<span style="color:#ff0000">"free":</span> <span style="color:#880000">1396</span>, <span style="color:#ff0000">"used":</span> <span style="color:#880000">588</span>}, <span style="color:#ff0000">"real":</span> {<span style="color:#ff0000">"free":</span> <span style="color:#880000">1115</span>, <span style="color:#ff0000">"total":</span> <span style="color:#880000">1984</span>, <span style="color:#ff0000">"used":</span> <span style="color:#880000">869</span>}, <span style="color:#ff0000">"swap":</span> {<span style="color:#ff0000">"cached":</span> <span style="color:#880000">0</span>, <span style="color:#ff0000">"free":</span> <span style="color:#880000">3813</span>, <span style="color:#ff0000">"total":</span> <span style="color:#880000">3813</span>, <span style="color:#ff0000">"used":</span> <span style="color:#880000">0</span>}}, <span style="color:#ff0000">"ansible_memtotal_mb":</span> <span style="color:#880000">1984</span>}, <span style="color:#ff0000">"changed":</span> <span style="color:#a31515">false</span>}
</code></span>