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


创建软链接ansible webserver -m file -a 'dest=/var/tmp/chenyu src=https://www.huyubaike.com/var/tmp/bbb state=link'
创建硬链接ansible webserver -m file -a 'dest=/var/tmp/chenyu1 src=https://www.huyubaike.com/var/tmp/aaa state=hard'
案例6:lineinfile把abc开头的一行换成 bbbbbansible webserver -m lineinfile -a 'dest=/tmp/cy regexp=abc line=bbbbb'
在某一行前面插入一行新数据---insertbeforeansible webserver -m lineinfile -a 'dest=/tmp/cy insertbefore="aa(.*)" line=chenyu'
在某一行后面插入一行新数据---insertafteransible webserver -m lineinfile -a 'dest=/tmp/cy insertafter="aaaa(.*)" line=bbbb'
删除某一行ansible webserver -m lineinfile -a 'dest=/tmp/cy regexp="aaa(.*)" state=absent'
案例7:yum_repository模块-----配置yum仓库ansible webserver -m yum_repository -a 'file=server name=baseos description=rhel8 baseurl=file:///mnt/BaseOS enabled=yes gpgcheck=no'
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'

经验总结扩展阅读