4.9ansible中常用模块

1.ansible实现管理的方式

Ad-Hoc利用ansible命令直接完成管理,主要用于临时命令使用场景
playbook

ansible脚本,主要用于大型项目场景,需要前期的规划

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

ansible-doc	  显示模块帮助的指令格式
ansible-doc [参数] [模块...]常用参数
-l		列出可用模块
-s		显示指定模块的playbook片段

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

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

                                                                    常用参数

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

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

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

5.ansible中的常用模块

1)command

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

                                                                   常用参数

chdir执行命令前先进到指定目录
cmd运行命令指定
creates如果文件存在将不运行
removes如果文件存在将运行
ansible all -m command -a 'useradd wl' -u root
ansible all -m command -a 'userdel -r wl' -u root
ansible all -m command -a 'chdir=/etc cat passwd' -u root
ansible all -m command -a 'chdir=/etc creates=/etc/passwd cat passwd' -u root
ansible all -m command -a 'chdir=/etc removes=/etc/passwd cat passwd' -u root

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

2)shell

功能:
和command功能类似

                                                                 常用参数

chdir执行命令前先进入到指定目录
cmd运行命令指定
creates如果文件存在将不运行
removes如果文件存在将运行
free_form 在远程主机中执行的命令,此参数不需要加
executable指定执行环境,默认为sh
ansible all -m shell -a "executable=sh ps ax | grep $$ " -k

$$: 脚本运行的当前进程ID号

3、script

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

vim /mnt/westos.sh#!/bin/bash
echo $HOSTNAMEansible all -m script -a '/mnt/westos.sh'

4、copy

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

                                                                    常用参数

src源文件
dest目的地文件
owner指定目的地文件所有人
group指定目的地文件所有组
mode指定目的地文件所有权限
backup=yes当受控主机中存在文件时备份源文件
content指定文本内容直接在受控主机中
ansible all -m copy -a 'src=/root/westos dest=/mnt/westos owner=wl group=wl mode=644 backup=yes'ansible all -m copy -a 'content="hello linux" dest=/mnt/westos owner=wl group=wl mode=777 backup=yes'

5.fetch

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

                                                                        常用参数

src受控主机的源文件
dest本机目录
flat基本名称功能
ansible all -m fetch -a 'src=/etc/hostname dest=/mnt/wl'
ansible all -m fetch -a 'src=/etc/hostname dest=/mnt/wl flat=yes'

6.file

功能:设置文件属性

                                                                        常用参数

path指定文件名称
state指定操作状态:touch(建立) absent(删除) directory(递归) link(建立软链接) hard(建立硬链接)
mode设定权限
owner设定文件用户
group设定文件组
src源文件
dest目标文件
recurse递归更改
ansible all -m file -a 'path=/mnt/test.sh state=touch'
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 'path=/mnt/westos state=directory mode=777 recurse=yes'
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'

8.archive

作用;压缩

                                                                    常用参数

path打包目录名称
dest打包后的文件名称
format打包格式
owner指定文件所属人
mode指定文件权限
ansible all -m archive -a 'path=/etc dest=/mnt/etc.tar.gz format=gz owner=wl mode=700'

9.unarchive

作用:解压缩

                                                                        常用参数

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

10.hostname

作用:管理主机名 

ansible all -m hostname -a 'name=wl.westos.org'

11.cron

作用:计划任务

minute分钟hour小时
daymonth
weekdayname任务名称
job任务脚本或命令disabled若=yes 禁用计划任务 no 启动计划任务
stateabsent(删除计划任务)----------------------------------------------------
ansible all -m cron -a 'job="echo haha" name=test minute=*'
ansible all -m cron -a 'job="echo haha" name=test minute=* disabled=yes'
ansible all -m cron -a 'job="echo haha" name=test mintue=* state=absent'

12.yum_repository

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

                                                                       常用参数

name指定仓库名称
baseurl指定源路径
description指定仓库描述
file指定仓库文件名称
enabled仓库是否启用
gpgcheck仓库是否检测gpgcheck
stateabsent 为删除
ansible all -m yum_repository -a 'file=westos name=AppStream description=AppStream baseurl=http://172.25.254.250/rhel8.2/AppStream gpgcheck=0'ansible all -m yum_repository -a 'file=westos name=BaseOS description=BaseOS baseurl=http://172.25.254.250/rhel8.2/BaseOS gpgcheck=0'

13.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=latest"
ansible all -m dnf -a 'name="httpd,mariadb-server" state=present'
ansible all -m dnf -a 'name=httpd state=absent'
ansible all -m dnf -a 'name=httpd state=absent autoremove=no'
ansible all -m dnf -a 'name=httpd state=present enablerepo=AppStream'
ansible all -m dnf -a 'name="*" state=latest'
ansible all -m dnf -a 'name=http://172.25.254.250/software/wps-office-xxx.rpm state=present'
ansible all -m dnf -a 'name="@Virtual Tools" state=present'

14.service

管理系统服务状态

                                                                 常用参数

name指定服务名称
state指定对服务的动做:started stoped restarted reload
enabled 设定服务开机是否启动 yes:开机启动 no:开机不启动
ansible all -m service -a "name=httpd state=started enabled=yes" 
ansible all -m service -a 'name=httpd state=stopped'	
ansible all -m service -a "name=httpd state=restarted enabled=yes" 

15.firewall

                                                                常用参数

zone火墙的域
service服务名称
permanent永久生效
stateenabled 允许   disabled 拒绝
immediate立即生效
ansible all -m firewalld -a 'service=http zone=public state=enabled immediate=yes'

16.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 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$F4OBwqoXAigDV.dn$I2OgEPB3kfyl8CPmdh3Y8vKDqewZKrVMIDPPIt8GKnhs/DW4gZHfxrZX5ziQN7rVjISX7l14KwDQHEd.uprlV/"'
ansible all -m user -a 'name=lee generate_ssh_key=yes'

17.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'

18.lineinfile

作用:修改、书写文件内容,相当于vim

                                                                常用参数

path                    指定要操作的文件
line           指定文本内容, "|+" 表示格式化输入 
regexp        使用正则表达式匹配对应的行当替换文本时,
        如果有多行文本都能被匹配,
        则只有最后面被匹配到的那行文本才会被替换,
        当删除文本时,如果有多行文本都能被匹配
        这么这些行都会被删除。
state       当想要删除对应的文本时需要将state参数的值设置为absent,
        state的默认值为present。
backrefs        当内容无匹配规则时不对文件做任何更改,默认值为no
insertafter         向后引用regexp变量信息,借助insertafter参数可以将文本插 入到“指定的行”之后,insertafter参数的值可以设置为EOF或者 正则表达式
insertbefore       借助insertbefore参数可以将文本插入到“指定的行”之前
        insertbefore参数的值可以设置为BOF或者正则表达式
backup            是否在修改文件之前对文件进行备份
create       当要操作的文件并不存在时,是否创建对应的文件
ansible all -m lineinfile -a 'path=/mnt/westos line="hello westos" create=yes'(若没有文件要先建立)
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"'

19.replace

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

                                                              常用参数

path指定要操作的文件
regexp指定一个正则表达式,文件中与正则匹配的字符串将会被替换。
replace指定最终要替换成的字符串
backup

是否在修改文件之前对文件进行备份,最好设置为yes

ansible all -m replace -a 'path=/mnt/westos regexp="WESTOS" replace="westos_lee" backup=yes'

20.set up

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

                                                                  常用参数

filter用于进行条件过滤,如果设置,仅返回匹配过滤条件的信息
ansible all -m setup -kansible all -m setup -a "filter='ansible_all_ipv4_addresses'" -k

21.debug

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

                                                                   常用参数

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


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部