彩票网站做任务拿佣金,住宅项目建设背景,建企业网站多少钱,甘肃嘉峪关建设局网站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 例25.3 script模块5.4 例3 6、raw模块6.1 参数6.2 例4 二、文件操作模块1、file 模块1.1 参数1.2 案例 2、copy … 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 例25.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 参数表
名称必选备注chdirno运行command命令前先cd到这个目录createsno如果这个参数对应的文件存在就不运行commandfree_formyes需要执行的脚本没有真正的参数为free_formexecutableno改变用来执行命令的shell是可执行文件的绝对路径removesno如果这个参数对应的文件不存在就不运行command与creates参数作用相反stdinno2.4后新的增将命令的stdin设置为指定的值
4.2 free_form参数
必须参数指定需要远程执行的命令。free_form 参数与其他参数如果想要使用一个参数那么则需要为这个参数赋值也就是namevalue模式并不相同。如需要在远程主机上执行 ls 命令时错误写法”free_formls” 因为并没有任何参数的名字是 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 createst1.sh
# 当文件t2.sh 不 存在则就不执行前面的命令
ansible all -m command -a ls /root removest2.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.sh5、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 参数
名称必选备注executableno改变用来执行命令的shell是可执行文件的绝对路径free_formyes需要执行的脚本没有真正的参数为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参数进行说明 参数值含义stateabsent删除远程机器上的指定文件或目录statedirectory创建一个空目录statefile查看指定目录是否存在statetouch创建一个空文件statehard/link创建链接文件 src 当state设置为link或者hard时表示创建一个软链或硬链则必须通过指明src参数即可指定链接源 force : 当statelink的时使用forceyes 参数表示强制创建链接文件该文件分为三种情况 1.当要创建的链接文件指向的源文件并不存在时使用此参数可以先强制创建出链接文件2.当存储目录中已经存在与链接文件同名的文件时会将同名文件覆盖为链接文件相当于删除同名文件创建链接文件。3.当你要创建链接文件的目录中已经存在与链接文件同名的文件并且链接文件指向的源文件也不存在这时会强制替换同名文件为链接文件 owner 用于指定被操作文件的属主信息属主对应的用户必须在远程主机中存在否则会报错 group用于指定被操作文件的属组属组对应的组必须在远程主机中存在否则会报错 mode用于指定被操作文件的权限,如: 要将文件权限设置为: “rw-r-x—”则可以使用mode650进行设置或者使用mode0650要设置特殊权限如为二进制文件设置suid则可以使用mode4700 recurse当要操作的文件为目录时recurse设置为yes可以递归的修改目录中文件的属性
1.2 案例
在所有远程主机上创建一个名为 data 的目录如果存在则不做操作
ansible all -m file -a path/root/data statedirectory
ansible all -m command -a ls chdir/root # 查看在node1主机上创建一个名为testfile1的文件如果testfile1文件已经存在则会更新文件的时间戳与touch命令的作用相同
ansible node1.example.com -m file -a path/root/data/testfile1 statetouch
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 statelink 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 statehard src/root/data/testfile1在创建链接文件时如果源文件不存在或者链接文件与其他文件同名时强制覆盖同名文件或者创建链接文件参考上述force参数的解释
ansible node1.example.com -m file -a path/root/data/linkfile3 statelink src/root/data/123 forceyes # 注意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 stateabsent
ansible node1.example.com -m command -a ls chdir/root # 查看创建文件或目录的时候指定属主或者修改远程主机上的文件或目录的属主
ansible all -m file -a path/root/testfile1 statetouch ownerfox # 新建文件指定为fox
ansible all -m file -a path/root/testfile2 statetouch # 新建文件默认为root
[rootserver ~]# ansible 192.168.48.131 -m file -a path/root/testfile2 statetouch ownerfox groupfox# 修改属主和工作组创建文件或目录的时候指定权限或者修改远程主机上的文件或目录的权限**
[rootserver ~]# ansible 192.168.48.131 -m file -a path/root/testfile1 statetouch mode777递归方式将目录中的文件的属主属组都设置为fox
ansible all -m file -a path/data/test/demo statedirectory ownerstudent groupstudent recurseyes2、copy 模块
作用拷贝文件将ansible主机上的文件拷贝到远程受控主机中
2.1 参数
参数默认值含义src用于指定需要copy的文件或目录backupno、 yes当远程主机的目标路径中已存在同名文件并且与ansible主机中的文件内容不同时是否对远程主机的文件进行备份设为yes时会先备份远程主机中的文件然后再拷贝到远程主机content当不使用src指定拷贝的文件时可以使用content直接指定文件内容src与content两个参数必有其一否则会报错dest用于指定文件将被拷贝到远程主机的哪个目录中dest为必须参数group指定文件拷贝到远程主机后的属组但是远程主机上必须有对应的组否则会报错owner指定文件拷贝到远程主机后的属主但是远程主机上必须有对应的用户否则会报mode错文指定文件拷贝到远程主机后的权限如果你想将权限设置为rw-r–r–则可以使用mode0644表示如果你想要在user对应的权限位上添加执行权限则可以使用modeux表示forceno、 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目录中时若已存在设置forceno/yes参数查看文件是否覆盖
echo hello world /testdir/copytest # 输入新内容ansible all -m copy -a src/testdir/copytest dest/opt/ forceno # 不会覆盖ansible all -m copy -a src/testdir/copytest dest/opt/ forceyes # 强制覆盖ansible all -m command -a cat /opt/copytest # 查看创建文件编辑内容在远程主机的/opt目录下生成文件testtest文件中有两行文本第一行文本为aaa第二行为bbb注意当使用content指定文件内容时dest参数对应的值必须是一个文件而不能是一个路径
ansible all -m copy -a contentaaa\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/ backupyes
ansible all -m command -a ls /opt # 查看拷贝文件并指定文件属主属组权限注意远程主机上必须存在对应的用户
ansible all -m copy -a src/etc/hosts dest/mnt ownerfox groupfox mode777拷贝文件时指定文件的权限
ansible all -m copy -a src/testdir/copytest dest/opt/ mode07553、fetch 模块
作用拉取远程主机的文件并以主机IP地址或者主机名为目录并保留了原来的目录结构
3.1 参数
dest 目标地址src源flatyes :不按照src的目录来创建目录
3.2 案例
从被管理节点上拷贝文件到控制节点
[rootserver ~]# ansible 192.168.48.131 -m fetch -a src/etc/hosts dest/optls /opt不采用默认的文件级结构
ansible node1.example.com -m fetch -a src/etc/hosts dest/opt/hosts flatyesls /opt三、软件包管理
1、yum/dnf 模块
作用使用yum包管理器安装、升级、降级、删除和列出包和组
1.1 参数 name必须参数用于指定需要管理的软件包比如 nginx。 state用于指定软件包的状态 安装present 或 installed 或 latest安装 yum 中最新的版本删除absent 或 removed disable_gpg_check用于禁用对 rpm 包的公钥 gpg 验证。 disable_gpg_checkno为默认值表示启用disable_gpg_check yes 表示禁用即不验证包直接安装注意在对应的 yum 源没有开启 gpg 验证的情况下需要将此参数的值设置为 yes否则会报错而无法进行安装 enablerepo临时启用的 yum 源。若想要从A源中安装软件但不确定A源是否启用则可设置为 yes disablerepo临时禁用的 yum 源。某些场景下需要此参数如多个 yum 源中同时存在需要安装的软件包时可以临时禁用某个源此时安装软件包时则不会从对应的源中选择安装包enablerepo 和 disablerepo 可以同时使用 download_onlyyes \ 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 namehttpd disable_gpg_checkyes
ansible all -m yum -a nameftp statepresent
ansible all -m dnf -a namebind删除
ansible all -m yum -a namebind,ftp stateremoved安装 telnet 时确定多个源中都有 telnet但是不想从 local 源中安装则临时禁用 local 源。
ansible all -m yum -a nametelnet disable_gpg_checkyes disablerepolocal2、service/systemd 模块
作用服务程序的管理
2.1 参数
参数作用name操作的服务名称state服务状态(started、stopped、restarted、reloaded)enabledyes、no 开机启动arguments给命令提供一些选项runlevel运行等级sleep设置停止时间
2.2 案例
ansible all -m systemd -a namehttpd statestarted enabledyes
ansible all -m shell -a systemctl is-active httpd # 验证四、压缩解压缩
unarchive 模块
作用解包解压缩
参数
copy默认为copyyes copyyes将ansible主机上的压缩包传到远程主机后解压缩至特定目录copyno不是ansible主机 remote_src和copy功能一样且互斥 remote_srcyes在远程主机remote_srcno文件在ansible主机上 src源路径可以是ansible主机上的路径也可以是远程主机上的路径如果是远程主机上的路径则需要设置copynodest远程主机上的目标路径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 copyno mode0777从网络下载解压缩
ansible all -m unarchive -a srchttp://nginx.org/download/nginx-1.22.0.zip dest/root copyno # 下载Nginx解压到远程主机
ansible all -m command -a ls /root