"用途:从ansible本机将文件复制到其他被控制的机器 src:源。控制端的文件路径 dest:目标。被控制端的文件路径 content:内容。需要写到文件中的内容 [root@Rocky9 ansible] ansible all -m copy -a 'src=/etc/hostname dest=/root/' #拷贝本机文件到目标机 [root@Rocky9 ansible] ansible all -m copy -a 'content="hahahhha"dest=/opt/abc'#直接写入内容到目标机文件
(6)fetch模块
"用途:将文件从被控制主机取回到ansible主机 src:源。被控制端的文件路径 dest:目标。控制端的文件路径[root@Rocky9 ansible]ansible all -m fetch -a'src=/etc/hostname dest=/root/'#将所有目标机的hostname文件保存到本机[root@Rocky9 ansible]ls/root anaconda-ks.cfg ansible client1 good web1 web2#每台主机的文件独立的目录
(7)lineinfile模块
"用途:用于确保存目标文件中有某一行内容,也可以替换一行 path:待修改的文件路径 line:写入文件的一行内容 regexp:正则表达式,用于查找文件中的内容 [root@Rocky9 ansible] ansible all -m lineinfile -a 'path=/etc/issue line="hello world"' #webservers组中的主机,/etc/issue中一定要有一行Hello World。如果该行不存在,则默认添加到文件结尾 [root@Rocky9 ansible] ansible all -m lineinfile -a 'path=/etc/issue line="ci le mei" regexp="hello"'#查找包含hello的行,将整行内容更换为ci le mei
(8)replace模块
"用途:replace可以替换关键词"path:待修改的文件路径 replace:将正则表达式查到的内容,替换成replace的内容 regexp:正则表达式,用于查找文件中的内容[root@Rocky9 ansible]ansible all -m replace -a'path=/etc/issue regexp="ci" replace="ccccccc"'#issue文件内的ci替换为ccccccc
(9)user模块
name:待创建的用户名 uid:用户ID group:设置主组 groups:设置附加组 home:设置家目录 password:设置用户密码 state:状态。present表示创建,它是默认选项。absent表示删除 remove:删除家目录、邮箱等。值为yes或true都可以。[root@Rocky9 ansible]ansible all -m user -a'name=abc state=present groups=bin'#创建用户,附加组设置为bin[root@Rocky9 ansible]ansible all -m user -a'name=user66 password={{"123"|password_hash("sha512") }}'[root@Rocky9 ansible]ansible webservers -m user -a"name=lisi uid=1010 group=adm groups=daemon,root home=/home/lisi"#创建用户指定名称、uid、基本组、附加组、家目录[root@Rocky9 ansible]ansible webservers -m user -a"name=zhangsan state=absent"#删除用户[root@Rocky9 ansible]ansible webservers -m user -a"name=lisi state=absent remove=yes"#删除用户同时删除家目录
(10)group模块
name:待创建的组名 gid:组的ID号 state:present表示创建,它是默认选项。absent表示删除[root@AAAAA ansible]ansible all -m group -a'name=group01 state=present'[root@R9 an]ansible all -m group -a'name=group01 state=absent'
(11)yum_repository模块
用于配置yum[root@R9 an]ansible all -mfile-a'path=/etc/yum.repos.d/rocky-lan.repo state=absent'#删除原来的yum源[root@R9 an]ansible all -m yum_repository -a'file=dvd.repo name=dvd description=appstream baseurl=file:///mnt/AppStream gpgcheck=no enabled=yes'[root@R9 an]ansible all -m yum_repository -a'file=dvd.repo name=dvd1 description=baseos baseurl=file:///mnt/BaseOS gpgcheck=no enabled=yes'#配置第二个库时,文件名一致[root@R9 an]ansible all -mmount-a'path=/mnt src=/dev/sr0 state=mounted fstype=iso9660'#永久挂载[root@R9 an]ansible all -a'yum repolist'client1|CHANGED|rc=0>>仓库id仓库名称 dvd appstream dvd1 baseos web2|CHANGED|rc=0>>仓库id仓库名称 dvd appstream dvd1 baseos web1|CHANGED|rc=0>>仓库id仓库名称 dvd appstream dvd1 baseos
(12)yum模块
用于安装软件 name:包名 state:状态。present表示安装,如果已安装则忽略;latest表示安装或升级到最新版本;absent表示卸载。[root@R9 an]ansible all -m yum -a'name=tar state=present'[root@R9 an]ansible all -m yum -a'name=net-tools,wget'[root@R9 an]ansible all -m yum -a'name=wget state=absent'
(13)service模块
用于控制服务。启动、关闭、重启、开机自启。 常用选项: name:控制的服务名 state:started表示启动;stopped表示关闭;restarted表示重启 enabled:yes表示设置开机自启;no表示设置开机不要自启。[root@R9 an]ansible all -mservice-a'name=httpd state=started enabled=yes'
(14)创建卷组lvg模块
创建、删除卷组,修改卷组大小 常用选项: vg:定义卷组名。vg:volume group pvs:由哪些物理卷构成。pvs:physical volumes[root@R9 an]ansible all -m yum -a'name=lvm2'[root@R9 an]ansible all -m lvg -a'vg=vg1 pvs=/dev/nvme0n2'#创建卷组[root@R9 an]ansible all -a'vgs'[root@R9 an]ansible all -m lvg -a'vg=vg1 pvs=/dev/nvme0n2,/dev/nvme0n3'#扩容卷组[root@R9 an]ansible all -m lvg -a'vg=vg1 state=absent'#删除卷组
(15)创建逻辑卷lvol模块
创建、删除逻辑卷,修改逻辑卷大小 常用选项: vg:指定在哪个卷组上创建逻辑卷 lv:创建的逻辑卷名。lv:logical volume size:逻辑卷的大小,不写单位,以M为单位[root@R9 an]ansible all -m lvol -a'vg=vg1 lv=lv1 size=2G'#创建逻辑卷[root@R9 an]ansible all -a'lvs'[root@R9 an]ansible all -m lvol -a'vg=vg1 lv=lv1 size=6G'#扩容逻辑卷[root@R9 an]ansible all -m lvol -a'vg=vg1 lv=lv1 state=absent force=yes'#删除逻辑卷
(16)格式化filesystem模块
用于格式化,也就是创建文件系统 常用选项: fstype:指定文件系统类型 dev:指定要格式化的设备,可以是分区,可以是逻辑卷[root@R9 an]ansible all -m filesystem -a'fstype=xfs dev=/dev/vg1/lv1'#格式化[root@R9 an]ansible all -a'blkid /dev/vg1/lv1'
(17)挂载mount模块
用于挂载文件系统 常用选项: path:挂载点。如果挂载点不存在,自动创建。 src:待挂载的设备 fstype:文件系统类型 state:mounted,表示永久挂载[root@R9 an]ansible all -mmount-a'path=/data src=/dev/vg1/lv1 state=mounted fstype=xfs'#挂载[root@R9 an]ansible all -mmount-a'path=/data state=absent'#卸载
# 使用inventory变量。[root@pubserver ansible]# vim inventory[webservers]web1 web2[dbs]db1 username="wangwu"# 定义主机变量的方法[webservers:vars]# 定义组变量的方法,:vars是固定格式username="zhaoliu"# 通过变量创建用户[root@pubserver ansible]# vim var1.yml----name:webservers create userhosts:webserverstasks:-name:create useruser:name:"{{username}}"state:present-name:create user in dbshosts:dbstasks:-name:create some usersuser:name:"{{username}}"state:present
(3)playbook定义变量
# 在webservers组中的主机上创建用户jack,他的密码是123456[root@pubserver ansible]# vim user_jack.yml----name:create userhosts:webserversvars:# 固定格式,用于声明变量username:"jack"# 此处引号可有可无mima:"123456"# 此处引号是需要的,表示数字字符tasks:-name:create some usersuser:name:"{{username}}"# {}出现在开头,必须有引号state:presentpassword:"{{mima|password_hash('sha512')}}"
(4)在文件中定义变量
# 将变量定义在文件中[root@pubserver ansible]# vim vars.yml # 文件名自定义---yonghu:rosemima:abcd[root@pubserver ansible]# vim user_rose.yml----name:create userhosts:webserversvars_files:vars.yml# vars_files用于声明变量文件tasks:-name:create some usersuser:name:"{{yonghu}}"# 这里的变量来自于vars.ymlstate:presentpassword:"{{mima|password_hash('sha512')}}"
5、进阶语法
(1)错误处理
# 编辑myerr.yml,如果myslqd服务无法启动,则忽略它[root@pubserver ansible]# vim myerr.yml----name:my errorshosts:webserverstasks:-name:start mysqld serviceservice:name:mysqldstate:startedenabled:yesignore_errors:yes# 即使这个任务失败了,也要继续执行下去-name:touch a filefile:path:/tmp/service.txtstate:touch#通过全局设置,无论哪个任务出现问题,都要忽略[root@pubserver ansible]# vim myerr.yml----name:my errorshosts:webserversignore_errors:yestasks:-name:start mysqld serviceservice:name:mysqldstate:startedenabled:yes-name:touch a filefile:path:/tmp/mysql.txtstate:touch
只有满足某一条件时,才执行任务 常用的操作符: ==:相等!=:不等>:大于 <:小于 <=:小于等于>=:大于等于 多个条件或以使用and或or进行连接 when表达式中的变量,可以不使用{{}}# 当dbs组中的主机内存大于2G的时候,才安装mysql-server[root@pubserver ansible]# vim when1.yml----name:install mysql-serverhosts:dbstasks:-name:install mysql-server pkgyum:name:mysql-serverstate:presentwhen:ansible_memtotal_mb>2048# 如果目标主机没有2GB内存,则不会安装mysqld-server# 多条件。系统发行版是Rocky8才执行任务# /etc/motd中的内容,将会在用户登陆时显示在屏幕上[root@pubserver ansible]# vim motd_____________ < hello world>-------------\ ^__^ \ (oo)\_______ (__)\ )\/\||----w|||||[root@pubserver ansible]# vim when2.yml----name:when conditionhosts:webserverstasks:-name:modify /etc/motdcopy:dest:/etc/motdsrc:motdwhen:># 以下三行合并成一行ansible_distribution == "Rocky" and ansible_distribution_major_version == "8"
(4)regitster注册变量
register是一个关键字,可以将任务执行的结果赋值给指定的变量名称。这个变量可以在后续任务中使用。 register模块可以捕获各种类型的输出,包括stdout、stderr、rc、changed等。它可以与其他模块一起使用,例如“when”条件、“loop”循环等。# 在web1组的主机上执行任务,创建/tmp/regfile1.txt,并打印创建结果[root@pubserver ansible]# vim reg1.yml----name:create file /tmp/regfile1.txthosts:web1tasks:-name:create filefile:path:/tmp/rgefile1.txtstate:touchregister:result-name:display outputdebug:msg:"{{result}}"# 在web1主机上执行任务,创建文件/tmp/ademo/abc。如果创建不成功,则通过debug输出create failed[root@pubserver ansible]# vim reg2.yml----name:create file /tmp/ademo/abchosts:web1ignore_errors:yestasks:-name:create filefile:path:/tmp/ademo/abcstate:touchregister:result-name:debug outputdebug:msg:"create failed"when:result.failed
(5)block任务快
可以通过block关键字,将多个任务组合到一起 可以将整个block任务组,一起控制是否要执行# 如果webservers组中的主机系统发行版是Rocky,则安装并启动nginx[root@pubserver ansible]# vim block1.yml----name:block taskshosts:webserverstasks:-name:define a group of tasksblock:-name:install nginx# 通过yum安装nginxyum:name:nginxstate:present-name:start nginx# 通过service启动nginx服务service:name:nginxstate:startedenabled:yeswhen:ansible_distribution=="Rocky"# 条件为真才会执行上面的任务