提示:文章
目录
1.在node1上文件ansible_text文件中写入内容 ansible
然后使用blockinfile模块,在文件中插入内容 blockinfile insert content
2.然后插入内容 blockinfile with marker 且使用指定标记: marker=#{mark}test
3.在blockinfile insert content之前插入 insertbefore
4.在blockinfile insert content之后插入 insertafter
1.向node节点上文件 ansible_text2文件如插入内容 lineinfile insert content
3.重新插入lineinfile insert content
文件中的"Hello ansible,Hiiii"替换成"Hiiii"(使用正则表达式和backrefs)
2.将server主机上的包解压到node主机且设置权限为644
1.在node上为student用户设置周一到周五早上的9:00输出闹钟到/root/alarm_cron
1.设置两个软件仓库BaseOS和APPStream(本地yum源的配置)到文件my.repo
2.添加富规则:允许192.168.xxx.0/24来访问http的80端口
1.在node上添加一块网卡,设置IP,gw, method, dns,type,和自动连接
1.新增一块儿1GB的磁盘然后对磁盘进行分区: 分区1: 400,分区2: 200M, 分区3:200M,且设置分区1和分区2类型为LVM
一、blockinfile模块使用:
blockinfile模块可以帮助我们在指定的文件中插入"一段文本",这段文本是被标记过的,换句话说就是,我们在这段文本上做了记号,以便在以后的操作中可以通过"标记"找到这段文本,然后修改或者删除它.
1.在node1上文件ansible_text文件中写入内容 ansible
然后使用blockinfile模块,在文件中插入内容 blockinfile insert content
#ansible rhce -m blockinfile -a 'path=/root/ansible_test block="blockinfile insert content"'
2.然后插入内容 blockinfile with marker 且使用指定标记: marker=#{mark}test
ansible rhce -m blockinfile -a 'path=/root/ansible_test block="blockinfile with marker" marker="#{mark} test" '
3.在blockinfile insert content之前插入 insertbefore
ansible rhce -m blockinfile -a 'path=/root/ansible_test block="insertbefore" marker="#{mark} test" insertbefore=blockinfile insert content'
4.在blockinfile insert content之后插入 insertafter
ansible test70 -m blockinfile -a 'path=/root/ansible_test block="insertafter" marker="#{mark} test" insertafter=blockinfile insert content'
5.删除其中一行内容
#ansible rhce -m blockinfile -a 'path=/root/ansible_test marker="#{mark} test" state=absent'
二、lineinfile模块使用
lineinfile模块,确保"某一行文本"存在于指定的文件中,或者确保从文件中删除指定的"文本"(即确保指定的文本不存在于文件中),还可以根据正则表达式,替换"某一行文本"。
1.向node节点上文件 ansible_text2文件如插入内容 lineinfile insert content
#ansible rhce -m lineinfile -a 'path=/root/ansible_test2 line=" lineinfile insert content"'
2.删除lineinfile insert content
ansible rhce -m lineinfile -a 'path=/root/ansible_test2 line="lineinfile insert content" state=absent'
3.重新插入lineinfile insert content
在之前插入: insertbefore
ansible rhce -m blockinfile -a 'path=/root/ansible_test2 block="insertbefore" marker="#{mark} test" insertbefore=lineinfile insert content '
4.在它之后插入: insertafter
ansible rhce -m blockinfile -a 'path=/root/ansible_test2 block="insertafter" marker="#{mark} test" insertbefore=lineinfile insert content '
5.插入:Hello ansible,Hiiii
文件中的"Hello ansible,Hiiii"替换成"Hiiii"(使用正则表达式和backrefs)
ansible test70 -m lineinfile -a 'path=/root/ansible_test2 regexp="(H.{4}).*(H.{4})" line="\2" backrefs=yes'
三、unarchive模块使用
参数:
copy:1、将ansible主机上的压缩包传到远程主机后解压缩至特定目录,设置copy=yes
2、将远程主机上的某个压缩包解压缩到指定路径下,设置copy=no
remote_src:和copy功能一样且互斥,yes表示在远程主机,不在ansible主机,no表示文件在ansible主机上
src:源路径,可以是ansible主机上的路径,也可以是远程主机上的路径,如果是远程主机上的路径,则需要设置copy=no
dest:远程主机上的目标路径
mode:设置解压缩后的文件权限
1.将node主机上的包解压
#ansible node -m unarchive -a 'src=/data/foo.tgz copy=no dest=/var/lib/foo'
2.将server主机上的包解压到node主机且设置权限为644
#ansible rhce -m unarchive -a 'src=/tmp/foo.zip dest=/data copy=yes mode=0644'
四、archive模块使用
1.将rhce上的目录进行压缩
ansible rhce -m archive -a 'path=/var/log/ dest=/data/log.tar.bz2 format=bz2'
五、cron模块
1.在node上为student用户设置周一到周五早上的9:00输出闹钟到/root/alarm_cron
ansible rhce -m cron -a 'hour=9 weekday=1-5 user="student" job=/root/alarm_cron'
六、user模块
1.创建用户
ansible rhce -m user -a 'name=bob groups=rhce'
2.删除用户
ansible rhce -m user -a 'name=bob state=absent remove=yes'
七、group模块
1.创建组
ansible rhce -m group -a 'name=good'
2.删除组
ansible rhce -m group -a 'name=good state=absent'
八、yum_repository
常用选项
name参数:必须参数,用于指定要操作的唯一的仓库ID,也就是”.repo”配置文件中每个仓库对应的”中括号”内的仓库ID。
baseurl参数:此参数用于设置 yum 仓库的 baseurl。
description参数:此参数用于设置仓库的注释信息,也就是”.repo”配置文件中每个仓库对应的”name字段”对应的内容。
file参数:此参数用于设置仓库的配置文件名称,即设置”.repo”配置文件的文件名前缀,在不使用此参数的情况下,默认以 name 参数的仓库ID作为”.repo”配置文件的文件名前缀,同一个”.repo” 配置文件中 可以存在多个 yum 源。
enabled参数:此参数用于设置是否激活对应的 yum 源,此参数默认值为 yes,表示启用对应的 yum源,设置为 no 表示不启用对应的 yum 源。
gpgcheck参数:此参数用于设置是否开启 rpm 包验证功能,默认值为 no,表示不启用包验证,设置为 yes 表示开启包验证功能。
gpgkey参数:当 gpgcheck 参数设置为 yes 时,需要使用此参数指定验证包所需的公钥。 state参数:默认值为 present,当值设置为 absent 时,表示删除对应的 yum 源
1.设置两个软件仓库BaseOS和APPStream(本地yum源的配置)到文件my.repo
AppStream仓库:
#ansible rhce -m yum_repository -a 'name=AppStream description="AppStream" baseurl="http://classrom.example.com/my/AppStream" gpgcheck=no file=my'
BaseOS仓库:
#ansible rhce -m yum_repository -a 'name=BaseOS description="BaseOS" baseurl="http://classrom.example.com/my/BaseOS" gpgcheck=no file=my'
九、yum/dnf模块
name参数:必须参数,用于指定需要管理的软件包,比如 nginx。
state参数:用于指定软件包的状态 ,默认值为。present,表示确保软件包已经安装,除了。present,其他可用值有 installed、latest、absent、removed,其中 installed 与present 等效,latest 表示安装 yum 中最新的版本,absent 和 removed 等效,表示删除对应的软件包。
disablegpgcheck参数:用于禁用对 rpm 包的公钥 gpg 验证。默认值为 no,表示不禁用验证,设置为 yes 表示禁用验证,即不验证包,直接安装。在对应的 yum 源没有开启 gpg 验证的情况下,需要将此参数的值设置为 yes,否则会报错而无法进行安装。
enablerepo参数:用于指定安装软件包时临时启用的 yum 源。假如你想要从A源中安装软件,但是你不确定A源是否启用了,你可以在安装软件包时将此参数的值设置为 yes,即使A源的设置是未启用,也可以在安装软件包时临时启用A源。
disablerepo参数:用于指定安装软件包时临时禁用的 yum 源。某些场景下需要此参数,比如,当多个 yum 源中同时存在要安装的软件包时,你可以使用此参数临时禁用某个源,这样设置后,在安装软件包时则不会从对应的源中选择安装包。
enablerepo 参数和 disablerepo 参数可以同时使用。
1.安装软件 lrzsz
ansible rhce -m yum -a 'name=lrzsz disable_gpg_check=yes enablerepo=local'
十、service/systemd模块
name参数:此参数用于指定需要操作的服务名称,比如 nginx。
state参数:此参数用于指定服务的状态,比如,我们想要启动远程主机中的 nginx,则可以将 state 的值设置为 started;如果想要停止远程主机中的服务,则可以将 state 的值设置为 stopped。此参数的可用值有 started、stopped、restarted、reloaded。
enabled参数:此参数用于指定是否将服务设置为开机 启动项,设置为 yes 表示将对应服务设置为开机启动,设置为 no 表示不会开机启动
arguments: 给命令提供一些选项
ansible all -m service -a "name=network state=restarted args=eth0"
runlevel : 运行等级
sleep: 如果运行看restarted 则stop and start 之间沉睡几秒中
1.关闭防火墙
ansible rhce -m systemd -a "name=firewall state=stopped enabled=no "
2.重启防火墙
ansible rhce -m systemd -a "name=firewall state=restarted enabled=no "
十一、firewalld模块
- State 指防火墙策略状态enable disable
- service 指定服务
- immediate 立即启用
- port 端口 port=80/tcp
- permanent 永久生效
-
1.添加端口22, 添加服务 http
nsible rhce -m firewalld -a 'service=http permanent=true immediate=true state=enabled port=22/tcp'
2.添加富规则:允许192.168.xxx.0/24来访问http的80端口
ansible rhce -m firewalld -a 'rich_rule="rule family=ipv4 source address=192.168.109.0/24 service name=http accept" permanent=true immediate=true state=enabled port=80/tcp'
3.删除富规则
ansible rhce -m firewalld -a 'rich_rule="rule family=ipv4 source address=192.168.109.0/24 service name=http accept" permanent=true immediate=true state=disabled port=80/tcp'
十二、selinux模块
1.设置selinux工作模式为permissive
ansible rhce -m selinux -a 'state=permissive policy=targeted'
十三、nmcli模块
1.在node上添加一块网卡,设置IP,gw, method, dns,type,和自动连接
ansible rhce -m nmcli -a 'conn_name=ens224 ip4=192.168.109.100/24 gw4=192.168.109.2 dns4=114.114.114.114 state=present type=ethernet autoconnect=yes'
十四、get_url模块
1.去梨视频找个视频下载下来
ansible rhce -m get_url -a "url=http://example.com/path/file.conf dest=/etc/foo.conf"
十五、uri模块
url= 指明请求的url的路径,如:http://10.1.32.68/test.jpg
user= 如果请求的url需要认证,则认证的用户名是什么
password= 如果请求的url需要认证,则认证的密码是什么
method= 指明请求的方法,如GET、POST…
body= 指明报文中实体部分的内容,一般是POST方法或PUT方法时用到
HEADER_ 自定义请求报文中的添加的首部
return_content: 是否将返回主体作为字典中的"content"值返回
validate_certs: 如果“否”,则不会验证 ssl 证书。这应该只设置为“否”使用个人控制的网站使用自签名证书。
Forcebasicauth:根据初始请求强制发送基本身份验证标头。Uri 模块使用的库仅在 webservice 以401状态响应初始请求时发送身份验证信息。由于一些基本的认证服务没有正确地发送401,登录将失败
1.访问百度,并能获取到百度源码
ansible localhost -m uri -a 'url=http://www.baidu.com return_content=yes '
十六、parted模块
1.新增一块儿1GB的磁盘
然后对磁盘进行分区: 分区1: 400,分区2: 200M, 分区3:200M,且设置分区1和分区2类型为LVM
ansible rhce -m parted -a 'device=/dev/nvme0n1 number=1 part_end=400MB state=present flags=lvm'
ansible rhce -m parted -a 'device=/dev/nvme0n1 number=2 part_start=400MB part_end=600M state=present flags=lvm'
ansible rhce -m parted -a 'device=/dev/nvme0n3 number=1 part_start=600MB part_end=800M state=present'
十七、lvg模块
1.用上面parted建立的分区: 创建卷组
ansible rhce -m lvg -a 'pvs=/dev/nvme0n1p1 vg=vg1'
十八、lvol模块
1.在上面卷组的基础上创建逻辑卷:500M
ansible rhce -m lvol -a 'vg=vg1 lv=lv1 size=500 pvs=/dev/nvme0n1p3'
十九、filesystem模块
1.为逻辑卷和分区3设置文件系统类型为 xfs
ansible rhce -m filesystem -a 'dev=/dev/vg1/lv1 fstype=xfs force=yes'
ansible rhce -m filesystem -a 'dev=/dev/nvme0n3 fstype=xfs force=yes'
二十、mount模块
选项:
- fstype:必选项,挂载文件的类型
- path:必选项,挂载点
- opts:传递给mount命令的参数
- src:必选项,要挂载的文件系统
- state:必选项present:只处理fstab中的配置absent
- 挂载点mounted
- unmounted:卸载
-
1.为上面的逻辑卷和分区3进行挂载(分别使用mounted和present)
挂载逻辑卷: ansible rhce -m mount -a 'src=/dev/vg1/lv1 path=/tools fstype=xfs state=mounted' 挂载分区: ansible rhce -m mount -a 'src=/dev/nvme0n3 path=/mnt/dev fstype=iso9660 state=present'