2.Ansible中常用模块

1.ansible实现管理的方式##

Ad-Hoc ##利用ansible命令直接完成管理,主要用于临时命令使用场景
playbook ##ansible脚本,主要用于大型项目场景,需要前期的规划

vim test.yml
在这里插入图片描述
在这里插入图片描述

2.Ad-Hoc执行方式中如何获得帮助

ansible-doc ##显示模块帮助的指令

格式:ansible-doc [参数] [模块…]

常用参数

-l ##列出可用模块在这里插入图片描述

-s ##显示指定模块的playbook片段
在这里插入图片描述

3.ansible命令运行方式及常用参数

格式:
ansible 清单 -m 模块 -a 模块参数

常用参数
–version显示版本
-m module指定模块,默认为command模块
-v详细过程 -vv -vvv更详细过程
–list显示主机列表,也可以用–list-hosts
-k提示输入ssh连接密码,默认key认证
-C预执行检测
-T执行命令的超时时间,默认10s
-u指定远程执行的用户
-b执行sudo切换身份操作
-become-user=USERNAME指定sudo的用户
-K提示输入sudo密码

4.ansible的基本颜色代表信息

绿色执行成功未为对远程主机做任何改变
黄色执行成功并对远程主机做改变
红色执行失败

5.ansible中的常用模块

1.command

功能:在远程主机执行命令,此模块为默认模块

常用参数
chdir执行命令前先进入到指定目录
cmd运行命令指定
creates如果文件存在将不运行
removes如果文件存在在将运行
free_form在远程主机中执行的命令,此参数不需要加

实例:

ansible westos1 -m command -a “touch /mnt/file”
在这里插入图片描述ansible westos1 -m command -a “chdir=/mnt rm -fr file”
在这里插入图片描述ansible westos1 -m command -a “touch /mnt/file”
ansible westos1 -m command -a “creates=/mnt/file touch /mnt/file1”
在这里插入图片描述ansible westos1 -m command -a “removes=/mnt/file touch /mnt/file2”
在这里插入图片描述

注意:Linux中的很多通配符在command模块中不支持

2.shell

功能:和command功能类似

常用参数
chdir执行命令前先进入到指定目录
cmd运行命令指定
creates如果文件存在将不运行
removes如果文件存在在将运行
free_form在远程主机中执行的命令,此参数不需要加
executable指定执行环境,默认为sh

实例:

ansible westos1 -m shell -a " ps ax | grep $$ "
在这里插入图片描述

ansible westos1 -m shell -a " executable=bash ps ax | grep $$ "
在这里插入图片描述

3.script

功能:在ansible主机中写好的脚本在受控主机中执行

vim test.sh
#!/bin/bash
rm -fr /mnt/*

实例:

ansible westos1 -m script -a “./test.sh”
在这里插入图片描述

4.copy

功能:从ansible主机复制文件到受控主机

常用参数
src源文件
dest目的地文件
owner指定目的地文件所有人
group指定目的地文件所有组
mode指定目的地文件权限
backup=yes当受控主机中存在文件时备份原文件
content指定文本内容直接在受控主机中生成文件

实例

ansible westos1 -m copy -a ‘src=test.yml dest=/mnt/test.yml’
在这里插入图片描述ansible westos1 -m copy -a ‘src=test.yml dest=/mnt/test.yml mode=1777’
在这里插入图片描述ansible westos1 -m shell -a ‘ls -l /mnt’
在这里插入图片描述ansible westos1 -m copy -a ‘src=test.yml dest=/mnt/test.yml mode=755 owner=gy group=gy’
在这里插入图片描述ansible westos1 -m shell -a ‘echo hello westos > /mnt/westosfile’
在这里插入图片描述ansible westos1 -m copy -a ‘src=test.yml dest=/mnt/westosfile mode=755 owner=gy group=gy backup=yes’
在这里插入图片描述ansible westos1 -m copy -a 'dest=/mnt/westos content=“hello westos” ’
在这里插入图片描述

5.fetch

功能:从受控主机把文件复制到ansible主机,但不支持目录

常用参数
src受控主机的源文件
dest本机目录
flat基本名称功能

实例

ansible westoss1 -m fetch -a “src=/mnt/westos dest=~/.ansible”
在这里插入图片描述
ansible all -m fetch -a “src=/mnt/westos dest=~/.ansibleflat=yes”
在这里插入图片描述

6.file

功能:设置文件的属性

常用参数
path指定文件名称
state指定操作状态
state=touch建立
state=absent删除
state=directory递归
state=link建立软链接
state=hard建立硬链接
mode设定权限
owner设定文件用户
group设定文件组
src源文件
dest目标文件
recurse=yes递归更改

测试:

ansible westos1 -m file -a ‘path=/mnt/westos state=touch’
在这里插入图片描述
ansible westos1 -m file -a ‘path=/mnt/westos1 state=directory’在这里插入图片描述
ansible westos1 -m file -a ‘path=/mnt/westos state=absent’
ansible westos1 -m file -a ‘path=/mnt/westos1 state=absent’
在这里插入图片描述
ansible westos1 -m file -a ‘path=/mnt/westos state=touch’
ansible westos1 -m file -a ‘path=/mnt/westos.link state=link src=/mnt/westos’
在这里插入图片描述
ansible westos1 -m file -a ‘path=/mnt/westos.hard state=hard src=/mnt/westos’
在这里插入图片描述ansible westos1 -m file -a ‘path=/mnt/westosdir state=directory’
ansible westos1 -m file -a ‘path=/mnt/westosdir/westos state=touch’
在这里插入图片描述ansible westos1 -m file -a ‘path=/mnt/westosdir mode=777’
在这里插入图片描述ansible westos1 -m shell -a ‘ls -lR /mnt’
在这里插入图片描述ansible westos1 -m file -a 'path=/mnt/westosdir mode=777 recurse=yes’发现/westosdir中的文件权限也已改变
在这里插入图片描述

7.archive

作用:压缩

常用参数
path打包目录名称
dest声称打包文件名称
format打包格式
owner指定文件所属人
mode指定文件权限

实例:

ansible westos1 -m unarchive -a ‘path=/etc dest=/mnt/etc.tar.gz format=gz mode=777 owner=gy’
在这里插入图片描述ansible westos1 -m shell -a ‘ls -l /mnt’
在这里插入图片描述

8.unarchive

功能:解压缩

常用参数
copy默认为yes 从ansible主机复制文件到受控主机;设定为no 从受控主机中寻找src源文件
remote_src功能同copy且相反。设定为yes 表示包在受控主机;设定为no表示包在ansible主机
src包路径,可以使ansible主机也可以使受控主机
dest受控主机目录
mode加压后文件权限 <copy=yes>

实例:

ansible westos1 -m archive -a ‘src=/mnt/etc.tar.gz dest=/mnt copy=no’
在这里插入图片描述ansible westos1 -m shell -a ‘ls -l /mnt’
在这里插入图片描述

9.hostname

作用:管理主机名称

常用参数
name指定主机名称

实例:
ansbile westos1 -m hostname -a ‘name=westoslinux.westos.org’
ansible westos1 -m shell -a ‘hostname’
在这里插入图片描述

10.cron

作用:计划任务

常用参数
minute分钟
hour小时
day
month
weekday
name任务名称
job任务脚本或命令
disabledyes 禁用计划任务;no 启动计划任务
stateabsent 删除计划任务

实例:

ansible westos1 -m cron “job=‘echo hello westos’ name=westoscron weekday=5”
ansible westos1 -m shell -a ‘cat /var/spool/cron/root’
在这里插入图片描述ansible westos1 -m cron "job=‘echo hello westos’ name=westoscron weekday=5 hour=/2"
ansible westos1 -m shell -a ‘cat /var/spool/cron/root’
在这里插入图片描述ansible westos1 -m cron “job=‘echo hello westos’ name=westoscron disable=yes”
ansible westos1 -m shell -a ‘cat /var/spool/cron/root’
在这里插入图片描述ansible westos1 -m cron “job=‘echo hello westos’ name=westoscron disable=no”
ansible westos1 -m shell -a ‘cat /var/spool/cron/root’
在这里插入图片描述ansible westos1 -m cron "job=‘echo hello westos’ name=westoscron weekday=5 hour=
/5 state=absent "
ansible westos1 -m shell -a ‘cat /var/spool/cron/root’
在这里插入图片描述

11.yum_repository

作用:配置系统软件仓库源文件

常用参数
name指定仓库名称
baseurl指定源路径
description指定仓库描述
file指定仓库文件名称
enabled仓库是否启用
gpgcheck仓库是否检测gpgkey
state默认值present 建立;absent 为删除

实例:
ansible all -m file -a 'path=/etc/yum.repos.d/westos.repo state=absent’删除原有软件仓库

在这里插入图片描述ansible all -m shell -a ‘ls -l /etc/yum.repos.d/’
在这里插入图片描述
ansible all -m yum_repository -a ‘name=“AppStream” baseurl=http://172.25.18.254/westos/AppStream desciription=AppStream enabled=yes gpgcheck=no’

在这里插入图片描述ansible all -m yum_repository -a ‘name=“BaseOS” baseurl=http://172.25.18.254/westos/BaseOS desciription=BaseOS enabled=yes gpgcheck=no’

在这里插入图片描述
ansible all -m shell -a ‘cat /etc/yum.repos.d/westos.repo’
在这里插入图片描述ansible all -m
在这里插入图片描述

12.dnf

作用:管理系统中的dnf仓库及管理软件

常用参数
name指定包
state指定动作:present安装 ; latest更新;absent删除
list列出指定信息:httpd;installed;all;available
disable_gpg_check禁用gpgkey检测
enablerepo指定安装包来源
disablerepo禁用安装包来源

实例:

ansible all -m dnf -a ‘name=httpd state=present disable_gpg_check=yes’
在这里插入图片描述
ansible all -m dnf -a “name=httpd state=latest”
在这里插入图片描述
ansible all -m dnf -a ‘name=“mariadb-server” state=present’
在这里插入图片描述ansible all -m dnf -a ‘name=mariadb-server state=absent’
在这里插入图片描述
ansible all -m dnf -a 'name=mariadb-server state=absent autoremove=no’在这里插入图片描述ansible all -m dnf -a ‘name=mariadb-server state=present enablerepo=BaseOS’
在这里插入图片描述
ansible all -m dnf -a ‘name="*" state=latest’在这里插入图片描述

13.service

作用:管理系统服务状态

常用参数
name指定服务名称
state指定对服务的动作started;stoped;restarted;reloaded
enabled设定服务开机是否启动。yes开机启动,no开机不启动

实例
ansible all -m service -a “name=httpd state=started enabled=yes” -k
ansible all -m service -a “name=httpd state=restarted enabled=yes” -k

14.firewalld

常用参数
zone火墙的域
service服务名称
permanent永久生效
stateenabled允许; disabled拒
immediate立即生效

15.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 all -m user -a ‘name=lee’
ansible all -m user -a ‘name=lee state=absent’
ansible all -m user -a ‘name=lee remove=yes state=absent’
ansible all -m user -a ‘name=lee group=888’
ansible all -m user -a ‘name=lee group=888 groups=“user1,user2”’
ansible all -m user -a ‘name=lee groups=“user3”’
ansible all -m user -a ‘name=lee groups=“user1,user2” append=yes’
openssl passwd -6 ‘westos’
ansible all -m user -a ‘name=lee password=“ 6 6 6F4OBwqoXAigDV.dn$I2OgEPB3kfyl8CPmdh3Y8vKDqewZKrVMIDPPIt8GKnhs/DW4gZHfxrZX5ziQN7rVjISX7l14KwDQHEd.uprlV/”’
ansible all -m user -a ‘name=lee generate_ssh_key=yes’

16.group

作用:group 模块可以帮助我们管理远程主机上的组。

常用参数
name用于指定要操作的组名称。
state用于指定组的状态。present 建立;absent 删除
gid用于指定组的gid。

实例
ansible all -m group -a ‘name=westoslee’
ansible all -m group -a ‘name=westoslee state=absent’
ansible all -m group -a ‘name=westoslee gid=8888’

17.lineinfile

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 all -m lineinfile -a ‘path=/mnt/westos line=“hello westos”’
ansible all -m lineinfile -a ‘path=/mnt/westos regexp="^westos" line=“hello westos” ’
ansible all -m lineinfile -a ‘path=/mnt/westos regexp="^test" line=“westos test”’
ansible all -m lineinfile -a ‘path=/mnt/westos regexp=’^test’ line=“westos test new” backrefs=yes’
ansible all -m lineinfile -a ‘path=/mnt/westos regexp="(h.{4}).*(w.{5})" line="\1" backrefs=yes’
ansible all -m lineinfile -a ‘path=/mnt/westos line="###### westos end #####" insertafter=EOF’
ansible all -m lineinfile -a ‘path=/mnt/westos line="###### westos end lee #####" insertafter=“hello”’
ansible all -m lineinfile -a ‘path=/mnt/westos line="###### westos test #####" insertbefore=BOF’
ansible all -m lineinfile -a ‘path=/mnt/westos line="###### westos test lee #####" insertbefore=“hello”’

##line |+
lineinfile:
path: /mnt/test
line: |+
westos
linux
lee
create: yes

18.replace

作用:replace 模块可以根据我们指定的正则表达式替换文件中的字符串,文件中所有被匹配到的字符串都会被替换

常用参数
path指定要操作的文件
regexp指定一个正则表达式,文件中与正则匹配的字符串将会被替换。
replace指定最终要替换成的字符串。
backup是否在修改文件之前对文件进行备份,最好设置为yes。

实例
ansible all -m replace -a ‘path=/mnt/westos regexp=“WESTOS” replace=“westos_lee” backup=yes’

19.setup

作用:setup模块用于收集远程主机的一些基本信息

常用参数
filter ##用于进行条件过滤。如果设置,仅返回匹配过滤条件的信息。

实例
ansible all -m setup -k
ansible all -m setup -a “filter=‘ansible_all_ipv4_addresses’” -k

20.debug

作用:调试模块,用于在调试中输出信息

常用参数:
msg:调试输出的消息
var:将某个任务执行的输出作为变量传递给debug模块 debug会直接将其打印输出
verbositydebug的级别(默认是0级,全部显示)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

热到想喝冰阔落

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值