ansible使用临时命令通过模块来执行任务( 二 )


ansible webserver -m yum_repository -a 'file=server name=appstream description=RHEL8 baseurl=file:///mnt/AppStream enabled=yes gpgcheck=no'

ansible使用临时命令通过模块来执行任务

文章插图
案例8:yum模块----yum安装与卸载state:present、installed、latest安装absent、removed卸载ansible all -m yum -a 'name=httpd state=installed'        ----------------安装
ansible all -m yum -a 'name=httpd state=removed'----------------卸载
ansible使用临时命令通过模块来执行任务

文章插图
案例9:service模块
ansible使用临时命令通过模块来执行任务

文章插图
重启httpd服务并设置下次启动生效ansible all -m service -a 'name=httpd state=started enabled=yes'
案例10:fetch—拉取文件模块和copy工作方式类似,只不过是从远程主机将文件拉取到本地端,存储时使用主机名作为目录树,且只能拉取文件,不能拉取目录
ansible使用临时命令通过模块来执行任务

文章插图
将远程主机的/etc/fstab文件拉取到本地来 , 存储的名字为/tmp/node1(node2)/etc/fstabansible all -m fetch -a 'src=https://www.huyubaike.com/etc/fstab dest=/tmp'
将某台远程主机的/etc/fstab文件拉取到本地来,存储的名字为/tmp/fstabansible node1 -m fetch -a 'src=https://www.huyubaike.com/etc/fstab dest=/tmp/ flat=yes'
将远程主机的/etc/fstab文件拉取到本地来,存储的名字为/tmp/fstab-node1(node2)ansible all -m fetch -a 'src=https://www.huyubaike.com/etc/fstab dest=/tmp/fstab-{{inventory_hostname}} flat=yes'
案例11:firewalld模块允许http流量的传入ansible all -m firewalld -a 'service=http   permanent=yes state=enabled immediate=yes'
富规则 允许172.16.30.0/24主机http流量的传入ansible all -m firewalld -a ‘zone=public rich_rule="rule family=ipv4 source address=172.16.30.0/24 service name=http accept" permanent=yes state=enabled immediate=yes'
案例12:replace模块replace模块可以根据我们指定的正则表达式替换文件中的字符串,文件中所有被匹配的字符串都会被替换参数:path参数:2.3版本之前只能用dest、destfile、name指定操作文件,2.4版本中仍然可以用这些参数名,也可以用pathregexp参数:必须参数 , 指定一个python正则表达式,文件中与正则匹配的字符串将会被替换replace参数:指定最终要替换成的字符串backup参数:是否在修改文件之前对文件进行备份,最好设置为yes 。
将/tmp/cy文件中的“abc”替换成“yyy”ansible all -m replace -a 'path=/tmp/cy regexp="abc" replace="yyy"'
将/tmp/cy文件中的“yyy”替换成“iii”,且把替换前的/tmp/cy文件备份ansible all -m replace -a 'path=/tmp/cy regexp="yyy" replace="iii" backup=yes'
案例13:parted模块新建扩展分区ansible node1 -m parted -a 'device=/dev/sda number=4 part_type=extended part_start=46GiB part_end=49.8GiB state=present'
新建逻辑分区ansible node1 -m parted -a 'device=/dev/sda number=5 part_type=logical part_start=46.1GiB part_end=48.2GiB state=present'
案例14:filesystem—文件系统ansible node1 -m filesystem -a 'fstype=xfs dev=/dev/sda5'
案例15:mount---挂载新建挂载点/commonansible node1 -m file -a 'path=/common state=directory'
查看/dev/sda5的UUIDansible node1 -m shell -a 'blkid /dev/sda5'
将分区/dev/sda5挂载到/common目录ansible node1 -m mount -a 'path=/common src=https://www.huyubaike.com/biancheng/"UUID=d162b8b9-2326-4ee4-a559-80861461c4f0" fstype=xfs state=mounted'
卸载ansible node1 -m mount -a 'path=/common src=https://www.huyubaike.com/biancheng/"UUID=d162b8b9-2326-4ee4-a559-80861461c4f0" fstype=xfs state=absent'
案例16:lvg—新建卷组ansible node1 -m lvg -a 'vg=vg0 pesize=16M pvs=/dev/sda5'
案例17:lvol—新建逻辑卷ansible node1 -m lvol -a 'lv=lv0 size=1000M vg=vg0'在线扩容逻辑卷ansible node1 -m lvol -a 'lv=lv0 size=1600M vg=vg0 resizefs=yes'
案例18:sefcontext---修改context值ansible node1 -m file -a 'path=/share state=directory'修改context值ansible node1 -m sefcontext -a 'target="/share(/.*)?" setype=samba_share_t state=present'应用新的selinux 文件的context值ansible node1 -m command -a 'restorecon -irv /share'
案例19:debug用户输出自定义的信息,类似于echo、print等输出命令 。ansible中的debug主要用于输出变量值、表达式值,以及用于when条件判断时 。使用方式非常简单
ansible使用临时命令通过模块来执行任务

文章插图
案例20:cron---计划任务模块

推荐阅读