https://www.junmajinlong.com/categories/Ansible/
也推荐马龙帅的ansible
先做ssh免密操作。
在centos7 yum安装ansible,其中136正常安装, 137 报了些莫名其妙的错误。最终复制全部repo到137解决问题,单独复制Base163.repo还是报错。
一、配置/etc/ansible/hosts文件。 执行ping操作
137 是 puppetmasert , 136作为客户端。
在137操作:vim /etc/ansible/hosts
[oldboy]
192.168.26.136
[oldboy2]
192.168.26.136
192.168.26.137
[oldboy3]
192.168.26.137
1.1 ping模块
ansible oldboy2 -m ping
#-vvv 查看详情
ansible oldboy2 -m ping -vvv
二、fetch模块
fetch模块的作用就是”Fetches a file from remote nodes”,即”从受管主机中拉取文件”。
src参数的作用就是指定从受管主机中拉取哪个文件。
dest参数的作用就是指定拉取文件到本地以后文件存放的位置。
pwd为: /root/shellDir/ansibleFile
ansible oldboy -m fetch -a "src=/etc/fstab dest=`pwd`"
列出ansible所支持的模块
ansible-doc -l
查看模块的详细帮助信息,比如查看fetch模块的帮助
ansible-doc -s fetch
三、copy模块
copy模块是将ansible主机上的文件拷贝到远程主机中。
3.1将ansible主机中/testdir/copytest文件复制到远程主机的/opt目录下
ansible oldboy -m copy -a "src=/testdir/copytest dest=/opt/"
3.2 在远程主机的/opt目录下生成文件test,test文件中有两行文本,第一行文本为aaa,第二行为bbb,当使用content指定文件内容时,dest参数对应的值必须是一个文件,而不能是一个路径。
#报错: "msg": "can not use content with a dir as dest"
ansible oldboy -m copy -a 'content="aaa\nbbb\n" dest=/opt/test1'
备份参数 backup=yes
ansible test70 -m copy -a "src=/testdir/copytest dest=/opt/ backup=yes"
四、file模块
file模块可对文件进行基本操作,比如,创建文件或目录、删除文件或目录、修改文件权限等
4.1在136主机上创建一个名为testfile的文件,如果testfile文件已经存在,则会更新文件的时间戳,与touch命令的作用相同。
ansible oldboy -m file -a "path=/testdir/testfile state=touch"
在136主机上创建一个名为testdir的目录,如果testdir目录已经存在,则不进行任何操作。
ansible oldboy -m file -a "path=/testdir/testDirectory state=directory"
\n space
在test70上为testfile文件创建软链接文件,软链接名为linkfile,执行下面命令的时候,testfile已经存在。
ansible test70 -m file -a "path=/testdir/linkfile state=link src=/testdir/testfile"
五、blockinfile 模块
blockinfile模块可以帮助我们在指定的文件中插入”一段文本”。
5.1 在test70主机中的/testdir/rc.local文件尾部插入如下两行
systemctl start mariadb
systemctl start httpd
ansible test70 -m blockinfile -a 'path=/testdir/rc.local block="systemctl start mariadb\nsystemctl start httpd" '
5.2 如果想要将文本块插入到文档的开头,可以使用insertbefore参数,将其值设置为BOF,BOF表示Begin Of File
这里注意下 单引号和双引号的问题,否则会报错,推荐-a 后面用单引号。
ansible test70 -m blockinfile -a 'path=/testdir/rc.local block="Begin of fiLE content" marker="#{mark} test" insertbefore=BOF'
5.3 如果使用如下命令,表示将文本块插入到文档的结尾,与默认操作相同,将insertafter参数设置为EOF表示End Of File
ansible test70 -m blockinfile -a 'path=/testdir/rc.local block="####blockinfile test####" marker="#{mark} test" insertafter=EOF'
使用如下命令表示使用正则表达式匹配行,将文本块插入到 “以#!/bin/bash开头的行” 之后
ansible test70 -m blockinfile -a 'path=/testdir/rc.local block="####blockinfile test####" marker="#{mark} test reg" insertafter="^#!/bin/bash" '
六、lineinfile 模块
文本内容: cat /testdir/test
Hello ansible,Hiiii
lineinfile -
Ensure a particular line is in a file,
lineinfile -
or replace an existing line using a back-referenced regular expression.
lineinfile模块,确保”某一行文本”存在于指定的文件中,或者确保从文件中删除指定的”文本”(即确保指定的文本不存在于文件中),还可以根据正则表达式,替换”某一行文本”。 (类似sed)
6.1确保指定的”一行文本”存在于文件中,如果指定的文本本来就存在于文件中,则不做任何操作,如果不存在,默认在文件的末尾插入这行文本,如下命令表示确保”test lineinfile”这行文本存在于/testdir/test文件中。
ansible test70 -m lineinfile -a 'path=/testdir/test line="test text"'
如下命令表示根据正则表达式替换”某一行”,如果不止一行能够匹配正则,那么只有最后一个匹配正则的行才会被替换,被匹配行会被替换成line参数指定的内容,但是如果指定的表达式没有匹配到任何一行,那么line中的内容会被添加到文件的最后一行。
ansible test70 -m lineinfile -a 'path=/testdir/test regexp="^line" line="test text" '
七、find模块
7.1在test70主机的/testdir目录中查找以.sh结尾的文件,只不过patterns对应的表达式为正则表达式,查找范围包括隐藏文件,包括所有文件类型,进行递归查找。
ansible test70 -m find -a 'path=/testdir recurse=yes patterns=".*\.sh" use_regex=yes file_type=any hidden=yes'
输出了一长串的东西。matched 4 个
查找atime在2星期以内的文件
age=-2w
查找mtime在4天以内的文件
age=-4d
查找大于2G的文件
size=2g
八、replace模块
replace模块可以根据我们指定的正则表达式替换文件中的字符串,文件中所有被正则匹配到的字符串都会被替换。
8.1 把test70主机中的/testdir/test文件中的所有ASM替换成asm,但是在操作文件之前进行备份。
ansible test70 -m replace -a 'path=/testdir/test regexp="ASM" replace=asm backup=yes'
来源:
https://www.zsythink.net/archives/2560 (五)
END