Ansible中常用的模块

1 ansible实现管理的方式Ad-Hoc #利用ansible命令直接完成管理,主要用于临时命令使用场景playbook #ansible脚本,主要用于大型项目场景,

1 ansible实现管理的方式

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

  • vim test.yml

运行命令:

  • ansible-playbook 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 westos -m shell -a ‘whoami’ -u root -k

  • ansible all -m command -a "chdir=/etc creates=/etc/passwd cat passwd " -u root -k

  • ansible all -m command -a "chdir=/etc cat passwd " -u root -k
  • 注意
    Linux中的很多通配符在command模块中不支持

2 shell

功能
和command功能类似

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

  • 示例

  • ansible all -m shell -a "executable=sh ps ax | grep $$ " -k

3 script

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

  • 示例
    vim /mnt/westos.sh
    写:
    #!/bin/bash
    echo $HOSTNAME

运行
ansible all -m script -a “/mnt/westos.sh” -k

4 copy

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

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

示例
主控机

  • cd /mnt

  • vim westostest
    写:
    hello westos
    hello linux

  • cd

  • cd .ansible

  • ansible all -m copy -a “src=/mnt/westos dest=/mnt/westos owner=westos mode=777 backup=yes”

  • ansible all -m copy -a “content=‘hello westos\nhello linux\n’ dest=/mnt/westosfile1 owner=westos mode=600”

受控机

  • cd /mnt
  • ls
  • cat westos
  • cat westos

    cat westosfile1

5 fetch

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

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

示例

  • ansible westos -m fetch -a ‘src=/etc/hostname dest=/mnt/test flat=yes’ -k

  • ansible 172.25.254.104 -m fetch -a ‘src=/etc/hostname dest=/mnt/hello flat=yes’

6 file

功能
设置文件的属性

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

示例

  • ansible westos -m file -a ‘path=/mnt/westosfile state=touch mode=777 owner=westos group=westos’

  • ansible westos -m shell -a ‘ls -l /mnt’

  • ansible westos -m file -a ‘path=/mnt/westosfile2 state=touch’

  • ansible westos -m shell -a ‘ls -l /mnt’

  • ansible westos -m file -a ‘path=/mnt/westosfile2 state=absent’

  • ansible westos -m shell -a ‘ls -l /mnt’

  • ansible westos -m file -a ‘src=/mnt/westosfile dest=/mnt/westoslxy state=link’

  • ansible westos -m shell -a ‘ls -l /mnt’

  • ansible westos -m file -a ‘src=/mnt/westosfile dest=/mnt/westoslinux state=hard’

  • ansible westos -m shell -a ‘ls -l /mnt’

7 unarchive

功能
解压缩

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

  • 示例:
  • ansible westos -m unarchive -a ‘src=/mnt/etc.tar.gz dest=/mnt copy=no mode=777’

8 archive

作用
压缩

常用参数
path #打包目录名称
dest #生成打包文件名称
format #打包格式
owner #指定文件所属人

示例

  • ansible all -m archive -a ‘path=/etc dest=/mnt/etc.tar.gz format=gz owner=westos mode=755’

9 hostanme

作用
管理主机名称

常用参数
name #指定主机名称

示例

  • ansible 172.25.254.101 -m hostname -a ‘name=westosa.westos’

  • ansible westos -m shell -a ‘hostname’

  • ansible 172.25.254.101 -m shell -a ‘hostname’

10 cron

作用
计划任务

常用参数
minute #分钟
hour #小时
day #天
month #月
weekday #周
name #任务名称
job #任务脚本或命令
disabled #yes 禁用计划任务
#no 启用计划任务
state #absent 删除计划任务

示例

  • ansible westos -m cron -a “job=date name=westostest minute=*/2”

  • ansible westos -m shell -a ‘crontab -l’

  • ansible westos -m cron -a “job=date name=westostest disabled=yes”

  • ansible westos -m shell -a ‘crontab -l’

11 yum_repository

作用

配置系统软件仓库源文件

常用参数

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

  • 示例
  • ansible westos -m yum_repository -a “name=AppStream baseurl=http://172.25.254.1/rhel8/AppStream description=westosAppStream gpgcheck=no file=westos”

  • ansible westos -m yum_repository -a “name=BaseOS baseurl=http://172.25.254.1/rhel8/BaseOS description=westosBaseOS gpgcheck=no file=westos”

  • ansible westos -n shell -a ‘cat /etc/yum.repos.d/westos.repo’ #关闭[BaseOS]
  • ansible westos -m shell -a ‘cat /etc/yum.repos.d/westos.repo’ #查看镜像配置文件

12 dnf

作用

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

常用参数

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

示例

  • ansible westos -m dnf -a ‘name=“httpd,dhcp-server” state=latest disable_gpg_check=yes’

  • ansible westos -m dnf -a ‘name=vsftpd state=latest’

  • ansible westos -m dnf -a ‘name="@Virtualization Tools" state=present disable_gpg_check=yes’

13 service

作用
管理系统服务状态

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

示例

  • ansible westos -m shell -a 'systemctl status httpd ’
  • ansible westos -m service -a ‘name=httpd state=restarted’

13 firewalld

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

  • ansible westos -m firewalld -a ‘zone=public service=http permanent=yes state=enabled immediate=yes’

  • ansible westos -m shell -a ‘firewall-cmd --list-all’

14 user

作用
模块可以帮助我们管理远程主机上的用户,比如创建用户、修改用户、删除用户、为用户创建密钥对等操作

常用参数
name #必须参数,用于指定要操作的用户名称
group #指定用户所在的基本组
groups #指定用户所在的附加组
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=lxy shell=/bin/sh uid=6666 state=present’
  • ansible westos -m shell -a ‘id lxy’
  • ansible westos -m shell -a ‘tail -n 1 /etc/passwd’
  • openssl passwd -6
  • ansible westos -m user -a ‘name=test shell=/bin/sh uid=3333 group=72 groups=‘westos’ append=yes comment=“test user” state=present password=“666cTvZY.FjpxJh4IH1$PC6DsfoHIJXb8U1XiZvHb6ipWNbKt838JPZ6o8hPeiIDKObjjC1XArWv69K2f1Z5vv8wa0f/SM5vongHMvXUQ1”’
  • ansible westos -m user -a ‘name=lxy1 state=present uid=3555 group=6666 comment=“test user” shell=/bin/sh groups=dhcpd append=yes generate_ssh_key=yes’

15 group

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

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

示例

  • ansible westos -m group -a ‘name=lxy state=present gid=2000’

  • ansible westos -m group -a ‘name=lxylxy’

  • ansible westos -m group -a ‘name=lxylxy state=absent’

16 lineinfile

  • path #指定要操作的文件

  • line #指定文本内容

  • regexp #使用正则表达式匹配对应的行当替换文本时,如果有多行文本都能被匹配,则只有最后面被匹配道德那行文本才会被替换;当删除文件时,如果有多行文本能被匹配,那么这些行都会被删除

  • state #当想要删除对应的文本时需要将state参数的值设置为 absent
    #state的默认值为present

  • backrefs #当内容无匹配规则时不对文件做任何更改,默认值为no,向后引用regexp变量信息

  • insertafter #借助insertafer参数可以将文本插入到“指定的行”之后,insertafer参数的值可以设置为EOF或者正则表达式

  • insertbefore #借助insertbefore参数可以将文本插入到“指定的行”之前,insertbefore参数的值可以设置为BOF或者正则表达式

  • backup #是否在修改文件之前对文件进行备份

  • create #当要操作的文件并不存在时,是否创建对应的文件

  • 示例:

  • ansible westos -m lineinfile -a ‘path=/mnt/westos line=“hello westos”’

  • ansible westos -m lineinfile -a ‘path=/mnt/westos regexp="^westos" line=“hello westos”’

  • ansible westos -m lineinfile -a ‘path=/mnt/westos regexp="^test" line=“westos test”’

  • ansible westos -m lineinfile -a ‘path=/mnt/westos regexp=’^test’ line=“westos test new” backrefs=yes’

  • ansible westos -m lineinfile -a ‘path=/mnt/westos regexp="(h.{4}).*(w.{5})" line="\1" backrefs=yes’

  • ansible westos -m lineinfile -a ‘path=/mnt/westos line="######## westos end ########" insertafter=EOF’

  • ansible westos -m lineinfile -a ‘path=/mnt/westos line="######## westos end lxy#####" insertafer=“hello”’

  • ansible westos -m lineinfile -a ‘path=/mnt/westos line="#### westos test#####" insertbefore=BOF’

  • ansible westos -m lineinfile -a ‘path=/mnt/westos line="#### westos test lxy#####" insertbefore=“hello”’

17 replace

作用

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

常用参数

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

  • 示例:
  • ansible westos -m replace -a ‘path=/mnt/westos regexp=“WESTOS” replace=“westos_lxy” backup=yes’

17 setup

作用

setup模块用于手机远程主机的一些基本信息

常用参数

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

  • 示例:
  • ansible westos -m setup -a “filter=‘ansible_all_ipv4_addresses’”

18 debug

作用

调试模块,用于在调试中输入信息

常用参数

msg: #调试输出的消息
var: #将某个任务执行的输出作为变量传递给debug模块
#debug会直接将其打印输出
verbosity: #debug的级别(默认是0级,全部显示)

发布者:admin,转转请注明出处:http://www.yc00.com/web/1754949683a5219751.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信