RHCE
- 一、Ansible的三个命令模块
- 1、组成
- 2、特点
- 3、区别
- 3.1 command、shell模块:
- 3.2 raw模块
- 4、command模块
- 4.1 参数表
- 4.2 free_form参数
- 5、shell模块
- 5.1 作用
- 5.2 例2
- 5.3 script模块
- 5.4 例3
- 6、raw模块
- 6.1 参数
- 6.2 例4
- 二、文件操作模块
- 1、file 模块
- 1.1 参数
- 1.2 案例
- 2、copy 模块
- 2.1 参数
- 2.2 案例
- 3、fetch 模块
- 3.1 参数
- 3.2 案例
- 三、软件包管理
- 1、yum/dnf 模块
- 1.1 参数
- 1.2 案例
- 2、service/systemd 模块
- 2.1 参数
- 2.2 案例
- 四、压缩解压缩
- unarchive 模块
- 参数
- 案例:
一、Ansible的三个命令模块
1、组成
- command 、shell 、raw
2、特点
- 应尽量避免使用这三个模块来执行命令,因为其他模块大部分都是幂等性的,可以自动进行更改跟踪。
- 幂等性:输入相同,输出相同,无论多少次执行,比如说,确认接口,如果传入订单号,返回确认OK,如果已经确认过了,再次调用确认接口,返回如果还是确认OK,那么这个接口就是满足幂等性
command、shell、raw不具备幂等性
3、区别
3.1 command、shell模块:
- 相同点:要求受管主机上安装Python。
- 不同点:command可以在受管主机上执行shell命令,但是不支持环境变量和操作符(例如 ‘|’, ‘<’, ‘>’,‘&’) ,shell模块调用的/bin/sh指令执行。
3.2 raw模块
- 不需要受管主机上安装Python,直接使用远程shell运行命令,通常用于无法安装Python的系统(例如:网络设备等)
4、command模块
4.1 参数表
名称 | 必选 | 备注 |
---|---|---|
chdir | no | 运行command命令前先cd到这个目录 |
creates | no | 如果这个参数对应的文件存在,就不运行command |
free_form | yes | 需要执行的脚本(没有真正的参数为free_form) |
executable | no | 改变用来执行命令的shell,是可执行文件的绝对路径 |
removes | no | 如果这个参数对应的文件不存在,就不运行command,与creates参数作用相反 |
stdin | no | 2.4后新的增,将命令的stdin设置为指定的值 |
4.2 free_form参数
- 必须参数,指定需要远程执行的命令。
- free_form 参数与其他参数(如果想要使用一个参数,那么则需要为这个参数赋值,也就是name=value模式)并不相同。
- 如:需要在远程主机上执行 ls 命令时,错误写法:”free_form=ls” ,因为并没有任何参数的名字是 free_form,若要在远程主机中执行 ls 命令时,直接写成 ls 即可。因为 command 模块的作用是执行命令,所以任何一个可以在远程主机上执行的命令都可以被称为free_form
- 例1:
ansible-inventory --graph # 分组查看
ansible all -m command -a "ls /root" # 查看目录
ansible all -m command -a "cd /root" # 切换到/root目录
ansible all -m command -a "pwd"
ansible all -m command -a "touch t1.sh" # 新建文件
ansible all -m command -a "ls" #浏览
# 当文件t1.sh存在则就不执行前面的命令
ansible all -m command -a "ls /root creates=t1.sh"
# 当文件t2.sh 不 存在则就不执行前面的命令
ansible all -m command -a "ls /root removes=t2.sh"
# 无法使用管道符
ansible all -m command -a "echo 'hello world' > t1.sh "
ansible all -m command -a "cat t1.sh" # 无内容
ansible all -m command -a "ls /root | grep t1.sh"
5、shell模块
5.1 作用
- 让远程主机在shell进程下执行命令,从而支持shell的特性,如管道等,参数与command模块几乎相同,但在执行命令的时候使用的是/bin/sh
5.2 例2
ansible all -m shell -a 'tree chdir=/root'
ansible test -m shell -a "echo 'hello world' > t1.sh"
ansible test -m shell -a "cat t1.sh" # 查看内容
5.3 script模块
- script 与shell 类似,都可以执行脚本,
- 区别:script执行的脚本在ansible管理机上,而shell执行的脚本必须先放到目标节点上去,才能执行;
- shell执行可以使用环境变量,bash等,但是script只是执行脚本,不能带 bash
5.4 例3
- 在server操作
vim t2.sh # 输入下列内容:
#!/bin/bash
echo "hello world"
ansible all -m script -a "t2.sh" # 执行本机的脚本到all
ansible all -m shell -a "bash t2.sh" # 可以使用shell模块执行目标节点的脚本
6、raw模块
raw模块主要用于执行一些低级的命令,一般适用于下列两种场景
- 第一种:在较老的(Python 2.4和之前的版本)主机上执行命令
- 第二种:对任何没有安装Python的设备(如路由器)
- 注意:在任何其他情况下,使用shell或command模块更为合适
6.1 参数
名称 | 必选 | 备注 |
---|---|---|
executable | no | 改变用来执行命令的shell,是可执行文件的绝对路径 |
free_form | yes | 需要执行的脚本(没有真正的参数为free_form) |
6.2 例4
ansible dev -m raw -a "pwd"
二、文件操作模块
1、file 模块
作用:实现对文件的基本操作,如:创建文件或目录、删除文件或目录、修改文件权限等
1.1 参数
-
path :必须参数,用于指定要操作的文件或目录,在之前版本的ansible中,使用dest参数或者name参数指定要操作的文件或目录,为了兼容之前的版本,使用dest或name也可以
-
state :
-
格式:path=“路径” state= touch|directory|link|hard|absent
-
此参数使用灵活,如:在远程主机中创建一个目录,则使用path参数指定对应的目录路径,假设在远程主机上创建/testdir/a/b目录,则设置路径:path=/testdir/a/b,但ansible无法从"/testdir/a/b"这个路径看出b是一个文件还是一个目录,所以需要通过state参数进行说明
-
参数 值 含义 state= absent 删除远程机器上的指定文件或目录 state= directory 创建一个空目录 state= file 查看指定目录是否存在 state= touch 创建一个空文件 state= hard/link 创建链接文件
-
-
src :当state设置为link或者hard时,表示创建一个软链或硬链,则必须通过指明src参数即可指定链接源
-
force : 当state=link的时,使用force=yes 参数表示强制创建链接文件,该文件分为三种情况,
- 1.:当要创建的链接文件指向的源文件并不存在时,使用此参数,可以先强制创建出链接文件
- 2.当存储目录中已经存在与链接文件同名的文件时,会将同名文件覆盖为链接文件,相当于删除同名文件,创建链接文件。
- 3.当你要创建链接文件的目录中已经存在与链接文件同名的文件,并且链接文件指向的源文件也不存在,这时会强制替换同名文件为链接文件
-
owner :用于指定被操作文件的属主信息,属主对应的用户必须在远程主机中存在,否则会报错
-
group:用于指定被操作文件的属组,属组对应的组必须在远程主机中存在,否则会报错
-
mode:用于指定被操作文件的权限,如:
- 要将文件权限设置为: “rw-r-x—”,则可以使用mode=650进行设置,或者使用mode=0650
- 要设置特殊权限,如:为二进制文件设置suid,则可以使用mode=4700
-
recurse:当要操作的文件为目录时,recurse设置为yes可以递归的修改目录中文件的属性
1.2 案例
- 在所有远程主机上创建一个名为 data 的目录,如果存在则不做操作
ansible all -m file -a "path=/root/data state=directory"
ansible all -m command -a "ls chdir=/root" # 查看
- 在node1主机上创建一个名为testfile1的文件,如果testfile1文件已经存在,则会更新文件的时间戳,与touch命令的作用相同
ansible node1.example.com -m file -a "path=/root/data/testfile1 state=touch"
ansible node1.example.com -m command -a 'ls chdir=/root/data' # 查看
- 在node1上为testfile1文件创建软链接文件,软链接名为linkfile1
ansible node1.example.com -m file -a "path=/root/data/linkfile1 state=link src=/root/data/testfile1"
ansible node1.example.com -m command -a 'ls chdir=/root/data' # 查看
- 在node1上为 testfile1 文件创建硬链接文件,硬链接名为 hardfile1(类似于复制)
ansible node1.example.com -m file -a "path=/root/data/hardfile1 state=hard src=/root/data/testfile1"
- 在创建链接文件时,如果源文件不存在,或者链接文件与其他文件同名时,强制覆盖同名文件或者创建链接文件,参考上述force参数的解释
ansible node1.example.com -m file -a "path=/root/data/linkfile3 state=link src=/root/data/123 force=yes" # 注意:123不存在
ansible node1.example.com -m command -a 'ls chdir=/root/data'
- 删除node1上的/root/data目录
ansible node1.example.com -m file -a "path=/root/data state=absent"
ansible node1.example.com -m command -a 'ls chdir=/root' # 查看
- 创建文件或目录的时候指定属主,或者修改远程主机上的文件或目录的属主
ansible all -m file -a "path=/root/testfile1 state=touch owner=fox" # 新建文件指定为fox
ansible all -m file -a "path=/root/testfile2 state=touch" # 新建文件,默认为root
[root@server ~]# ansible 192.168.48.131 -m file -a "path=/root/testfile2 state=touch owner=fox group=fox"# 修改属主和工作组
- 创建文件或目录的时候指定权限,或者修改远程主机上的文件或目录的权限**
[root@server ~]# ansible 192.168.48.131 -m file -a "path=/root/testfile1 state=touch mode=777"
- 递归方式将目录中的文件的属主属组都设置为fox
ansible all -m file -a "path=/data/test/demo state=directory owner=student group=student recurse=yes"
2、copy 模块
作用:拷贝文件,将ansible主机上的文件拷贝到远程受控主机中
2.1 参数
参数 | 默认值 | 含义 |
---|---|---|
src | 用于指定需要copy的文件或目录 | |
backup | no、 yes | 当远程主机的目标路径中已存在同名文件,并且与ansible主机中的文件内容不同时,是否对远程主机的文件进行备份,设为yes时,会先备份远程主机中的文件,然后再拷贝到远程主机 |
content | 当不使用src指定拷贝的文件时,可以使用content直接指定文件内容,src与content两个参数必有其一,否则会报错 | |
dest | 用于指定文件将被拷贝到远程主机的哪个目录中,dest为必须参数 | |
group | 指定文件拷贝到远程主机后的属组,但是远程主机上必须有对应的组,否则会报错 | |
owner | 指定文件拷贝到远程主机后的属主,但是远程主机上必须有对应的用户,否则会报 | |
mode | 错文指定文件拷贝到远程主机后的权限,如果你想将权限设置为"rw-r–r–",则可以使用mode=0644表示,如果你想要在user对应的权限位上添加执行权限,则可以使用mode=u+x表示 | |
force | no、 yes | 当远程主机的目标路径中已经存在同名文件,并且与ansible主机中的文件内容不同时,是否强制覆盖,默认值为yes,表示覆盖,如果设置为no,则不会执行覆盖拷贝操作,远程主机中的文件保持不变 |
2.2 案例
- 将ansible主机中/testdir/copytest文件复制到远程主机的/opt目录下
mkdir /testdir
cd /testdir
touch copytest
cd ~
ansible all -m copy -a "src=/testdir/copytest dest=/opt/"
- 将ansible主机中/testdir/copytest文件修改后复制到远程主机的/opt目录中时,若已存在设置force=no/yes参数,查看文件是否覆盖
echo "hello world" > /testdir/copytest # 输入新内容ansible all -m copy -a "src=/testdir/copytest dest=/opt/ force=no" # 不会覆盖ansible all -m copy -a "src=/testdir/copytest dest=/opt/ force=yes" # 强制覆盖ansible all -m command -a "cat /opt/copytest" # 查看
- 创建文件编辑内容:在远程主机的/opt目录下生成文件test,test文件中有两行文本,第一行文本为aaa,第二行为bbb,注意:当使用content指定文件内容时,dest参数对应的值必须是一个文件,而不能是一个路径
ansible all -m copy -a 'content="aaa\nbbb\n" dest=/opt/test'
nsible all -m command -a "cat /opt/test" # 查看
- 将ansible主机中/testdir/copytest文件复制到远程主机的/opt目录中时,若文件同名但内容不同则会将远程主机中的原文件重命名,以作备份,然后再进行拷贝操作
# 设置实验环境
ansible all -m command -a "cat /opt/copytest" # 查看内容
echo "hello world" > /testdir/copytest # 重新修改本机文件
# 备份拷贝
ansible all -m copy -a "src=/testdir/copytest dest=/opt/ backup=yes"
ansible all -m command -a "ls /opt" # 查看
- 拷贝文件并指定文件属主属组权限,注意:远程主机上必须存在对应的用户
ansible all -m copy -a "src=/etc/hosts dest=/mnt owner=fox group=fox mode=777"
- 拷贝文件时,指定文件的权限
ansible all -m copy -a "src=/testdir/copytest dest=/opt/ mode=0755"
3、fetch 模块
作用:拉取远程主机的文件,并以主机IP地址或者主机名为目录,并保留了原来的目录结构
3.1 参数
- dest :目标地址
- src:源
- flat=yes :不按照src的目录来创建目录
3.2 案例
- 从被管理节点上拷贝文件到控制节点
[root@server ~]# ansible 192.168.48.131 -m fetch -a 'src=/etc/hosts dest=/opt'ls /opt
- 不采用默认的文件级结构
ansible node1.example.com -m fetch -a 'src=/etc/hosts dest=/opt/hosts flat=yes'ls /opt
三、软件包管理
1、yum/dnf 模块
作用:使用yum包管理器安装、升级、降级、删除和列出包和组
1.1 参数
-
name:必须参数,用于指定需要管理的软件包,比如 nginx。
-
state:用于指定软件包的状态
- 安装:present 或 installed 或 latest(安装 yum 中最新的版本)
- 删除:absent 或 removed
-
disable_gpg_check:用于禁用对 rpm 包的公钥 gpg 验证。
- disable_gpg_check=no,为默认值,表示启用
- disable_gpg_check= yes ,表示禁用,即不验证包直接安装
- 注意:在对应的 yum 源没有开启 gpg 验证的情况下,需要将此参数的值设置为 yes,否则会报错而无法进行安装
-
enablerepo:临时启用的 yum 源。若想要从A源中安装软件,但不确定A源是否启用,则可设置为 yes
-
disablerepo:临时禁用的 yum 源。某些场景下需要此参数,如:多个 yum 源中同时存在需要安装的软件包时,可以临时禁用某个源,此时安装软件包时则不会从对应的源中选择安装包,enablerepo 和 disablerepo 可以同时使用
-
download_only:yes \ no,默认no,只下载,不安装
-
list:等价于yum list
1.2 案例
- 注意:需要删除之前的repo文件
ansible all -m shell -a 'ls /etc/yum.repos.d' # 查看
ansible all -m shell -a 'rm -f /etc/yum.repos.d/redhat_dvd.repo' # 都删除
ansible all -m shell -a 'yum clean all' # 删除缓存
ansible all -m shell -a 'yum makecache' # 重新缓存
- 安装
ansible all -m yum -a "name=httpd disable_gpg_check=yes"
ansible all -m yum -a "name=ftp state=present"
ansible all -m dnf -a 'name=bind'
- 删除
ansible all -m yum -a "name=bind,ftp state=removed"
- 安装 telnet 时,确定多个源中都有 telnet,但是不想从 local 源中安装,则临时禁用 local 源。
ansible all -m yum -a 'name=telnet disable_gpg_check=yes disablerepo=local'
2、service/systemd 模块
作用:服务程序的管理
2.1 参数
参数 | 作用 |
---|---|
name | 操作的服务名称 |
state | 服务状态(started、stopped、restarted、reloaded) |
enabled | yes、no 开机启动 |
arguments | 给命令提供一些选项 |
runlevel | 运行等级 |
sleep | 设置停止时间 |
2.2 案例
ansible all -m systemd -a "name=httpd state=started enabled=yes "
ansible all -m shell -a 'systemctl is-active httpd' # 验证
四、压缩解压缩
unarchive 模块
作用:解包解压缩
参数
- copy:默认为copy=yes
- copy=yes:将ansible主机上的压缩包传到远程主机后解压缩至特定目录
- copy=no:不是ansible主机
- remote_src:和copy功能一样且互斥
- remote_src=yes:在远程主机
- remote_src=no:文件在ansible主机上
- src:源路径,可以是ansible主机上的路径,也可以是远程主机上的路径,如果是远程主机上的路径,则需要设置copy=no
- dest:远程主机上的目标路径
- mode:设置解压缩后的文件权限
- exec:列出需要排除的目录和文件
- owner:设置解压的属主
- group:设置解压的属组
- creates:在创建一个文件之前,先判断文件是否存在,如果存在则跳过前面的东西,如果不存在则执行前面的动作
案例:
- 从本地解压到远程主机
# server端本地打包,建立实验环境
tar -cvf testroot.tar.gz /root
ansible all -m unarchive -a 'src=/root/testroot.tar.gz dest=/tmp'
ansible all -m command -a "ls /tmp" # 查看
- 从远程主机解压到远程主机制定目录
# 建立实验环境,将上例压缩包copy到远程主机
ansible all -m copy -a "src=/root/testroot.tar.gz dest=/mnt"
ansible all -m unarchive -a 'src=/mnt/testroot.tar.gz dest=/usr copy=no mode=0777'
- 从网络下载解压缩
ansible all -m unarchive -a 'src=http://nginx.org/download/nginx-1.22.0.zip dest=/root copy=no' # 下载Nginx解压到远程主机
ansible all -m command -a "ls /root"