Ansible中常用的模块
目录
1.ansible实现管理的常用方式
2.Ad-Hoc执行方式中如何获得帮助
3.ansible命令运行方式及常用参数
4.ansible的基本颜色代表信
5.ansible中的常用模块
1.command模块
2.shell模块
3.script模块
4.copy模块
5.fetch模块
6.file模块
7.unarchive模块
8.archive模块
9.hostname模块
10.cron模块
11.yum_repository模块
12.dnf模块
13.service模块
14.firewalld模块
15.user模块
16.group模块
17.lineinfile模块
18.replace模块
19.setup
20.debug
Ad-Hoc 利用ansible命令直接完成管理,主要用于临时命令使用场景
Playbook ansible脚本,主要用于大型项目场景,需要前期的规划
ansible-doc 显示模块帮助的指令
格式:ansible-doc [参数] [模块]
常用参数
-l 列出可用模块
-s 显示指定模块的playbook片段
格式: ansible 清单 -m 模块 -a 模块参数
常用参数
--version
显示版本
-m(module可以替换)
指定模块,默认为command模块
-v
详细过程 -vv -vvv更详细过程
--list
显示主机列表,也可以用--list-hosts
-k
提示输入ssh连接密码,默认key认证
-C
预执行检测
-T
执行命令的超时时间,默认10s
-u
指定远程执行的用户
-b
执行sudo切换身份操作
-K
提示输入sudo密码
-become-user=USERNAME
指定sudo的用户
绿色
执行成功但为对远程主机做任何改变
黄色
执行成功并对远程主机做改变
红色
执行失败
功能: 在远程主机执行命令,此模块为默认模块,但是不能识别特殊字符
常用参数
chdir 执行命令前先进入到指定目录
cmd 运行命令指定
creates 如果文件存在将不运行
removes 如果文件存在在将运行
free_form 在远程主机中执行的命令,此参数不需要加
功能:
shell模块和command功能类似,但可以识别特殊字符
常用参数
chdir 执行命令前先进入到指定目录
cmd 运行命令指定
creates 如果文件存在将不运行
removes 如果文件存在在将运行
free_form 在远程主机中执行的命令,此参数不需要加
executable 指定执行环境,默认为sh
案例:ansible westos -m shell -a ‘executable=sh ps ax | grep $$’ 指定运行环境为sh
功能: 在ansible主机中写好的脚本可以在受控主机中执行
实例
vim test.sh
#!/bin/bash
touch /mnt/linux{ 1..4}
指令:ansible westos -m script –a ’test.sh’
功能:从ansible主机复制文件到受控主机
常用参数
src 源文件
dest 目的地文件
owner 指定目的地文件所有人
group mode 指定目的地文件权限
backup=yes 当受控主机中存在文件时备份原文件
content 指定文本内容直接在受控主机中生成文件
案例:
ansible westos -m copy -a ‘dest=/mnt/linux content=”hello westos”’
在/mnt/中创建linux文件,内容为hello westos
ansible westos -m copy -a ‘src=inventory dest=/mnt/file owner=westos group=devops mode=777 backup=yes’
将当期目录下的inventory文件复制到可控主机的/mnt/file中,并指定devops组,权限为777
功能 :从受控主机把文件复制到ansible主机,但不支持目录
常用参数
src 受控主机的源文件
dest 本机目录
flat 基本名称功能
实例
ansible westos -m fetch –a ‘src=/mnt/linux dest=/tmp/
将被控主机/mnt/中的linux文件复制到当前主机的/tmp/中
ansible westos -m fetch -a ‘src=/mnt/linux dest=/tmp/westos flat=yes’
将被控主机中/mnt/中的linux文件复制到当前主机/tmp/目录中并命名为westos
功能:设置文件的属性
常用参数
path 指定文件名称
state 指定操作状态
ouch 建立
absent 删除
directory 递归
link 建立链接
hard
mode 设定权限
owner 设定文件用户
group 设定文件组
src 源文件
dest 目标文件
recurse=yes 递归更改
ansible all -m file -a 'path=/mnt/test.sh state=absent' 删除文件
ansible all -m file -a 'path=/mnt/westos state=directory' 建立目录
ansible all -m file -a 'src=/mnt/file dest=/mnt/westos state=link' 建立软连接
ansible all -m file -a 'src=/mnt/file dest=/mnt/westos1 state=hard' 建立硬链接
ansible all -m file -a 'path=/mnt/file state=touch owner=lee group=westos mode=777'
更改所有人所有组和权限
ansible westos -m file -a 'path=/mnt/linuxfile state=touch'
在被控主机/mnt/中建立linuxfile文件
ansible westos -m file -a 'path=/mnt/linux mode=777 '
更改被控主机中/mnt/中linux文件权限为777、
功能:解压缩
常用参数
Copy 默认为yes 从ansible主机复制文件到受控主机 ##
设定为no 从受控主机中寻找src源文件
remote_src
功能同copy且相反
设定为yes 表示包在受控主机
设定为no表示包在ansible主机
src 包路径,可以使ansible主机也可以使受控主机
dest 受控主机目录
mode 加压后文件权限 #
案例:
ansible all -m unarchive -a 'copy=no src=/mnt/etc.tar.gz dest=/mnt mode=777’
解压/mnt/etc.tar.ge到/mnt中
作用:压缩
常用参数
path 打包目录名称
dest 声称打包文件名称
format 打包格式
owner 指定文件所属人
mode 指定文件权限 #
实例
ansible all -m archive -a 'path=/etc dest=/mnt/etc.tar.gz format=gz owner=devops mode=777'
压缩可控主机中/etc/目录,并设定压缩格式为gz,下所文件名为etc.tar.gz,权限为777
作用:管理主机名称
常用参数
name 指定主机名称
实例 ansbile 172.25.254.100 -m hostname -a 'name=lee.westos.com'
作用:计划任务
常用参数
minute 分钟
hour 小时
day 天
month 月
weekday 周
name 任务名称
job 任务脚本或命令
disabled yes 禁用计划任务
no 启动计划任务
state=absent 删除计划任务
案例:
ansible westos –m cron –a ‘job=”rm –rf /mnt/*” name=clear minute=*/5’
删除被控主机中/mnt/下的常用所有内容,每五分钟执行一次
ansible westos –m cron –a ‘job=”rm –rf /mnt/*” name=clear minute=*/5 state=absent’
删除该定时任务
作用:配置系统软件仓库源文
常用参数
name 指定仓库名称
baseurl 指定源路径
description 指定仓库描述
file 指定仓库文件名称
enabled 仓库是否启用
gpgcheck 仓库是否检测
gpgkey state 默认值
present 建立
absent 删除
ansible westos –m yum_repository –a ‘name=** file=westos state=absent’
删除名字为westos软件仓库中的名字为
**案例:搭建软件仓库
ansible westos -m yum_repository -a ‘name=AppStream baseurl=http://192.168.43.202/
rhel8.2/AppStream description=AppStream gpgcheck=0’
ansible westos -m yum_repository -a ‘name=BaseOS baseurl=http://192.168.43.202/
rhel8.2/BaseOS description=BaseOS gpgcheck=0’
作用:管理系统中的dnf仓库及管理软件
常用参数
name 指定包
state 指定动作
present 安装
latest 更新
absent 删除
list 列出指定信息
httpd
installed
all
available
disable_gpg_check=yes 禁用gpgkey检测
enablerepo 指定安装包来源
disablerepo 禁用安装包来源
实例
ansible all -m dnf -a 'name="httpd,vsftpd,lftpr" state=present' 安装软件
ansible all -m dnf -a 'name="httpd,vsftpd,lftpr" state=absent autoremove=yes' 卸载软件
作用:管理系统服务状态
常用参数
name 指定服务名称
state 指定对服务的动作
started
stoped
restarted
reloaded
enabled 设定服务开机是否启动
yes开启启动
no开机不启动
实例
ansible all -m service -a ‘name=httpd state=started enabled=yes’ 开启http
ansible all -m service -a ‘name=httpd state=restarted enabled=yes’ 重启httpd
常用参数
zone 火墙的域
service 服务名称
permanent 永久生效
state
enabled 允许
disabled 拒绝
immediate 立即生效
作用 模块可以帮助我们管理远程主机上的用户,比如创建用户、修改用户、常用删除用户、常用为用户创建密钥对等操作
常用参数
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=lee state=present uid=6666'
建立用户lee,常用并指定用户id为6666
ansible westos -m user -a 'name=lee remove=yes state=absent'
删除用户并且删除用户家目录
ansible all -m user -a 'name=lee group=21 uid=6666'
指定lee用户的组id为21,用户id为6666
作用:group 模块可以帮助我们管理远程主机上的组。
常用参数
name 用于指定要操作的常用组名称。
state 用于指定组的常用状态y
present 建立
absent 删除
gid 用于指定组的gid。
实例
ansible westos -m group -a 'name=westoslinux gid=8888'
指定westoslinux的常用组id为8888
ansible all -m group -a 'name=westoslinux state=absent'
删除westoslinux组
path 指定要操作的文件。
line 指定文本内容。 "|+" 表示格式化输入
regexp 使用正则表达式匹配对应的行当替换文本时
如果有多行文本都能被匹配
则只有最后面被匹配到的那行文本才会被替换
当删除文本时,如果有多行文本都能被匹配
这么这些行都会被删除。
state 当想要删除对应的文本时需要将state参数的值设置为absent
state的默认值为present。
backrefs 当内容无匹配规则时不对文件做任何更改,默认值为no
向后引用regexp变量信息
insertafter 借助insertafter参数可以将文本插入到“指定的行”之后
insertafter参数的值可以设置为EOF或者正则表达式
insertbefore 借助insertbefore参数可以将文本插入到“指定的行”之前
insertbefore参数的值可以设置为BOF或者正则表达式
backup 是否在修改文件之前对文件进行备份。
create 当要操作的文件并不存在时,是否创建对应的文件。
vim /mnt/westos
hello westos
hello test
hello linux
实例
ansible westos -m lineinfile -a 'path=/mnt/westos regexp="westos" line="hello"'
更改被控主机中/mnt/目录中westos文件中的westos为hello
作用:replace 模块可以根据我们指定的正则表达式替换文件中的字符串,文件中所有被匹配到的字符串都会被替换
常用参数
path 指定要操作的文件
regexp 指定一个正则表达式 #文件中与正则匹配的字符串将会被替换。
replace 指定最终要替换成的字符串。
Backup 是否在修改文件之前对文件进行备份,最好设置为yes。
实例
ansible westos -m replace -a 'path=/mnt/westos regexp="hello" replace="hi" backup=yes'
将被控主机中/mnt/westos文件中的所有hello改为hi
作用:setup模块用于收集远程主机的一些基本信息
常用参数
filter 用于进行条件过滤。如果设置,仅返回匹配过滤条件的信息。
实例
ansible westos -m setup -a "filter='ansible_all_ipv4_addresses'"
查看被控主机的IP地址
ansible westos -m setup -a 'filter="ansible_fqdn"'
查看被控主机的主机名
作用:调试模块,用于在调试中输出信息
常用参数:
msg: 调试输出的消息
var: 将某个任务执行的输出作为变量传递给debug模块
debug会直接将其打印输出 v
erbosity: debug的级别(默认是0级,全部显示)
(责任编辑:休闲)