Ansible中常用的模块
- Ansible中的模块
- 1.command模块
- 2.shell模块
- 3.script模块
- 4.copy模块(将主机的子与资源复制给被控主机)
- 5.fetch模块(将被控主机的文件采集到主机)从受控主机把文件复制到ansible主机,但不支持目录
- 6.file模块,专职于文件管理
- 7.archive压缩受控主机的文件
- 8.unarchive解压缩
- 9.cron定时任务模块
- 10.yum_repository仓库的搭建
- 11.dnf软件管理模块
- 12.service管理服务状态
- 13.firewalld火墙模块
- 14.user模块
- 15.group模块
- 16.lineinfiel模块
- 17.replace替换文件内容
- 18.setup模块用于收集远程主机的信息
- 19.debug模块用于调试中输出的信息
Ansible中的模块
在Ansible中查询模块的功能:
ansible-doc -s 模块名称 ##查看模块的功能,参数s指查看简要的用法
1.command模块
command模块中有三个常用的参数:
chdir ##指定工作目录
removes ##文件存在运行
creates ##文件存在不运行
示例:
ansibel all -m command -a ‘chair=/mnt touch file file1’
指定用户列表中的所有被控机在/mnt目录下,建立文件file、file1。
上面参数中讲到removes与creates的区别,下面是一套完整的示例:
ansible all -m command -a ‘creates=/mnt/file chdir=/mnt touch file file1’
因为,受控主机的/mnt目录下,有file这个文件,所以后面的删除语句不运行。
ansible all -m command -a ‘removes=/mnt/file chdir=/mnt touch file file1’
使用remoes即使受控主机的/mnt目录中存在file文件,他仍然运行删除两个文件。
2.shell模块
shell模块的用法与command几乎是一样的,区别在于shell模块多了一个新的参数executable,手动指定远程主机中执行命令的shell环境
示例:
ansible all -m shell -a ‘executable=/bin/bash ps ax | grep $$’
观察图片会发现,此时的运行环境是/bin/bash。
其他参数:
cadir、creates、removes的用法与command模块没有差别。
ansible 172.25.254.237 -m script -a “test.sh”
3.script模块
script模块的作用就是指定Ansible主机中的脚本在受控机中运行:
4.copy模块(将主机的子与资源复制给被控主机)
常用参数:
copy模块的的参数有:
src ##源文件(主机文件)
dest ##目的地(受控机)
owner ##设定目的地文件的拥有者
group ##设定目的地文件的拥有组
mode ##设定目的地文件的权限
backup=yes ##当受控主机中存在文件时备份原文件
conntent ##指定文本内容直接在受控主机中生成文件
示例:
ansible 172.25.254.237 -m copy -a ‘src=/home/xyf/ansible/test.sh owner=westos group=westos mode=755 dest=/mnt/test.sh backup=yes’
ansible 172.25.254.237 -m copy -a ‘content=“hello world” dest=/mnt/westos’
5.fetch模块(将被控主机的文件采集到主机)从受控主机把文件复制到ansible主机,但不支持目录
常用参数:
src ##受控主机的源文件
dest ##本机目录
flat ##基本名称功能,如果不设定的情况下,会将文件路径也采集。
ansible 172.25.254.237 -m fetch -a ‘src=/etc/sysconfig/network-scripts/ifcfg-ens3 dest=/home/xyf/ansible/ifcfg-ens3 flat=yes’
6.file模块,专职于文件管理
常用参数:
path ##指定文件名称
state ##指定操作状态
state=touch 建立
state=absent 删除
state=directory 递归
state=link 建立链接
state=hard
mode ##设定权限
owner ##设定文件用户
group ##设定文件组
src ##源文件
dest ##目标文件
recurse=yes ##递归更改
示例:
ansible 172.25.254.237 -m file -a ‘path=/mnt/westosfile state=touch’
ansible 172.25.254.237 -m file -a ‘src=/mnt/westosfile dest=/mnt/westoslinux state=link’
查看:
ansible 172.25.254.237 -m file -a ‘dest=/mnt/westosdir owner=westos recurse=yes’
查看:
7.archive压缩受控主机的文件
常用参数:
path ##打包目录名称
dest ##声称打包文件名称
format ##打包格式
owner ##指定文件所属人
mode ##指定文件权限
示例:
ansible 172.25.254.237 -m archive -a ‘path=/etc dest=/mnt/etc.tar.gz format=gz’
查看:
nsible 172.25.254.237 -m archive -a ‘path=/etc dest=/mnt/etc.tar.bz2 format=bz2 owner=westos group=westos mode=755’
查看;
8.unarchive解压缩
常用参数:
copy ##默认为yes 从ansible主机复制文件到受控主机
##设定为no 从受控主机中寻找src源文件
remote_src ##功能同copy且相反
##设定为yes 表示包在受控主机
##设定为no表示包在ansible主机
src ##包路径,可以使ansible主机也可以使受控主机
dest ##受控主机目录
mode ##加压后文件权限 <copy=yes>
示例;
ansible 172.25.254.237 -m unarchive -a 'src=/mnt/etc.tar.gz dest=/media copy=no’查看:
9.cron定时任务模块
常用参数:
minute ##分钟
hour ##小时
day ##天
month ##月
weekday ##周
name ##任务名称
job ##任务脚本或命令
disabled ##yes 禁用计划任务
##no 启动计划任务
state ##absent 删除计划任务
示例:
查看:
ansible 172.25.254.237 -m cron -a ‘job=echo"hello westos" name=test minute=*/2 disabled=yes’
查看:
值得注意的是,这里的禁用只是将定是任务给取消,但并没有删除。如果设定disabled=no时,定时任务会重新开启。
ansible 172.25.254.237 -m cron -a ‘job=echo"hello westos" name=test minute=*/2 state=absent’
查看:
10.yum_repository仓库的搭建
#作用
配置系统软件仓库源文件
#常用参数
name ##指定仓库名称
baseurl ##指定源路径
description ##指定仓库描述
file ##指定仓库文件名称
enabled ##仓库是否启用
gpgcheck ##仓库是否检测gpgkey
state ##默认值present 建立
#absent 为删除
示例:
ansible westos -m yum_repository -a ‘file=westos name=AppStream description=AppStream baseurl=http://172.25.254.250/rhel8.2/AppStream enabled=yes gpgcheck=no state=present’
ansible westos -m yum_repository -a ‘file=westos name=BaseOS description=BaseOS baseurl=http://172.25.254.250/rhel8.2/BaseOS enabled=yes gpgcheck=no state=present’
示例:
11.dnf软件管理模块
#作用
管理系统中的dnf仓库及管理软件
#常用参数
name ##指定包
state ##指定动作
#present 安装
#latest 更新
#absent 删除
list ##列出指定信息
# httpd
# installed
# all
# available
disable_gpg_check #禁用gpgkey检测
enablerepo ##指定安装包来源
disablerepo ##禁用安装包来源
示例:
安装php。
aansible westos -m dnf -a ‘name=“php” state=present’
卸载php:
ansible westos -m dnf -a ‘name=“php” state=absent’
下载时,忽略依赖:
ansible westos -m dnf -a ‘name=“php” state=present autoremove=no’
卸载时,将有依赖性的文件都卸载:
ansible westos -m dnf -a ‘name=“php” state=absent autoremove=yes’
这时我们发现,卸载的内容多了好多。
禁用软件仓库:
ansible westos -m dnf -a 'name=“bind” state=present disablerepo=“AppStream” ‘
禁用gpgkey的检测:
ansible westos -m dnf -a ‘name=“bind” state=present disable_gpg_check=yes’
12.service管理服务状态
#作用
管理系统服务状态
#常用参数
name ##指定服务名称
state ##指定对服务的动作
#started
#stoped
#restarted
#reloaded
enabled ##设定服务开机是否启动
#yes开启启动
#no开机不启动
示例:
ansible 172.25.254.137 -m service -a “name=httpd state=started enabled=yes” -k
ansible 172.25.254.137 -m service -a “name=httpd state=restarted enabled=yes” -k
13.firewalld火墙模块
#常用参数
zone ##火墙的域
service ##服务名称
permanent ##永久生效
state
enabled ##允许
disabled ##拒绝
immediate ##立即生效
示例:
nsible westos -m firewalld -a ‘zone=public service=http permanent=yes state=enabled immediate=yes’
发现挡火墙中已经加入了http,那我们再设置开机关闭:
ansible westos -m firewalld -a ‘zone=public service=http permanent=yes state=disabled immediate=yes’
这是我火墙模块的的动作生效。
14.user模块
#作用
模块可以帮助我们管理远程主机上的用户,比如创建用户、修改用户、删除用户、为用户创建密钥对等操作
#常用参数
name ##必须参数,用于指定要操作的用户名称。
group ##指定用户所在的基本组。
gourps ##指定用户所在的附加组。
append ##指定添加附加组默认值为no
shell ##指定用户的默认 shell。
uid ##指定用户的 uid 号。
comment ##指定用户的注释信息。
state ##用于指定用户是否存在于远程主机
#present 建立
#absent 删除
remove ##当删除用户是删除用户家目录,默认值为no
password ##此参数用于指定用户的密码。但密码为明文,
##可以用openssl password -6 '密码’生成加密字符
generate_ssh_key ##生成sshkey
示例:
在受控主机中建立用户
ansible westos -m user -a ‘name=testuser’
删除受控主机中的用户
ansible westos -m user -a ‘name=testuser state=absent’
下面这段语句的意思是:
给受控主机添加一个名为testuser的用户,组id为1002,附加组为nginx,(append的意思是不会覆盖掉他原本的附加组。如果设置为年no的话,想要添加的组会将原本的替换掉),用户说明名为testuser westos,运行shell的环境是/bin/sh,用户id是6666。
ansible westos -m user -a ‘name=testuser group=1002 groups=nginx append=yes comment=“testuser westos” shell=/bin/sh uid=9999’
15.group模块
#作用
group 模块可以帮助我们管理远程主机上的组。
#常用参数
name ##用于指定要操作的组名称。
state ##用于指定组的状态
#present 建立
#absent 删除
gid ##用于指定组的gid。
示例:
在受控主机建立一个id为8888,名为testgroup的组
ansible westos -m group -a ‘name=testgroup gid=8888 state=present’
更改testgroup组的组id
ansible westos -m group -a 'name=testgroup gid=6666 ’
删除用户组:
ansible westos -m group -a ‘name=testgroup gid=6666 state=absent’
给受控主机生成sshkey:
openssl passwd -6 ##生成密钥
ansible westos -m user -a ‘name=testuser group=1002 groups=nginx append=yes comment=“testuser westos” shell=/bin/sh uid=9999 password=“
6
6
6je.rIB0q6XEs8pwL$/i7pOm8.zb1ttk8W8a4Ru3y/.1xJxUSRdRPT.6F3ssEXHetG/fh0RZXQBqkAjK2vszzhytxIthJ3Se0HPb50Y1” generate_ssh_key=yes’
清单中的两个受控主机密钥都已经生成
16.lineinfiel模块
path ##指定要操作的文件。
line ##指定文本内容。 “|+” 表示格式化输入
regexp ##使用正则表达式匹配对应的行当替换文本时
##如果有多行文本都能被匹配
##则只有最后面被匹配到的那行文本才会被替换
##当删除文本时,如果有多行文本都能被匹配
##这么这些行都会被删除。
state ##当想要删除对应的文本时需要将state参数的值设置为absent
#state的默认值为present。
backrefs ##当内容无匹配规则时不对文件做任何更改,默认值为no
##向后引用regexp变量信息
insertafter ##借助insertafter参数可以将文本插入到“指定的行”之后
##insertafter参数的值可以设置为EOF或者正则表达式
insertbefore ##借助insertbefore参数可以将文本插入到“指定的行”之前
#insertbefore参数的值可以设置为BOF或者正则表达式
backup ##是否在修改文件之前对文件进行备份。
create ##当要操作的文件并不存在时,是否创建对应的文件。
示例:
编写test.sh:
ansible-playbook test.yml ##运行编写的test.sh
查看:
将指定内容hellow westos ,写如到文件westosfile中。
ansible westos -m lineinfile -a 'path=/mnt/westosfile line=“hellow westos” ’
查看;
替换文件中以hellow开头的内容,为hellow linux。在不指定字符的情况下,默认替换最后一个。
ansible westos -m lineinfile -a ‘path=/mnt/westosfile regexp="^hellow" line=“hellow linux”’
查看:
删除文件中的内容;
ansible westos -m lineinfile -a ‘path=/mnt/westosfile regexp="^hellow" state=absent’
查看:
我们重新在文件中写入一些内容,方便之后的操作:
ansible westos -m lineinfile -a 'path=/mnt/westosfile line=“hellow westos\nhellow linux\nhellow redhat” ’
backrefs默认关闭的情况下,使用lineinfile模块匹配、写入内容时,可以理解为不可思议使用通配符号,在匹配部分regexp=后面写入什么就匹配什么。但当其开启时,我们就可以使用通配符号来代替我们要匹配的内容。如backrefs=no时,想要匹配hellow,只能写成regexp=“hellow”;backrefs=yes时,则可以写成regexp=“h.{4}"”。
h.{4}的意思是以h开头的后面有四个字符的内容。
添加内容到第一行:
ansible westos -m lineinfile -a 'path=/mnt/westosfile line="#################BEGIN" insertbefore=BOF
添加内容到最后一行:
ansible westos -m lineinfile -a ‘path=/mnt/westosfile line="#################END" insertafter=EOF’
修改内容时给原文件备份,backup=yes:
ansible westos -m lineinfile -a ‘path=/mnt/westosfile line="#################END" insertafter=EOF backup=yes’
17.replace替换文件内容
#作用
replace 模块可以根据我们指定的正则表达式替换文件中的字符串,文件中所有被匹配到的字符串都会被替换
#常用参数
path ##指定要操作的文件
regexp ##指定一个正则表达式
#文件中与正则匹配的字符串将会被替换。
replace ##指定最终要替换成的字符串。
backup ##是否在修改文件之前对文件进行备份,最好设置为yes。
示例:
ansible westos -m replace -a ‘path=/mnt/westosfile regexp=“westos” replace=“WESTOS” backup=yes’
18.setup模块用于收集远程主机的信息
#作用
setup模块用于收集远程主机的一些基本信息
#常用参数
filter ##用于进行条件过滤。如果设置,仅返回匹配过滤条件的信息。
示例:
ansible westos -m setup -a ‘filter=ansible_all_ipv4_addresses’
19.debug模块用于调试中输出的信息
#作用
调试模块,用于在调试中输出信息
#常用参数:
msg: ##调试输出的消息
var: ##将某个任务执行的输出作为变量传递给debug模块
##debug会直接将其打印输出
verbosity: ##debug的级别(默认是0级,全部显示)