Ansible2--------ansible的常用模块

实验环境(在ansible命令运行时不需要输入密码)

1ansible主机lee用户中做好加密(ssh-keygen),将`钥匙给受控主机的remote_user

脚本

#!/bin/bash
for host in {
    
    1..3}
do
     ssh-copy-id -i /home/lee/.ssh/id_rsa.pub lee@192.168.3.$host 
done

注意:如果脚本执行不成功,很有可能是因为受控主机里面````的.ssh文件的影响,可以提前删掉受控主机的.ssh文件(因为ssh命````令远程连接时yes后建立认证文件,后自动生成~/.ssh````/kown_hosts记录文件,而每一次的.ssh文件内容是不同的)

也可以使用单次命令的方式在每个主机里依次进行
在这里插入图片描述

2.sudo node{1..3} westos ALL=(ALL) NOPASSWD: ALL
在受控主机(ansible清单里的所有主机)中提前做好sudo,以超级用户的身份在主机中进行操作
脚本

#! /bin/bash
for host in {
    
    1..2}
do
     ssh root@192.168.3.$host 'sed “100alee `hostname`=(ALL) NOPASSWD:  ALL" /etc/sudoers'

也可以手动进行操作
在这里插入图片描述

3 清单及ansible的配置文件
在这里插入图片描述
4写一个监控脚本,就可以不用进入受控主机中查看ansible命令的执行结果,比较方便
注意此脚本是在ansible主机的root用户下写的,(ssh-keygen)所以在连接时也需要给清单中的用户的remote_user给钥匙

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

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 详细过程 -vv -vvv更详细过程
–list 显示主机列表,也可以用–list-hosts
-k 提示输入ssh连接密码,默认key认证
-C 预执行检测
-T 执行命令的超时时间,默认10s
-u 指定远程执行的用户
-b 执行sudo切换身份操作
-become-user=USERNAME 指定sudo的用户
-K 提示输入sudo密码

注意所有参数实现的功能在ansible.cfg文件中可以配置相应的参数实现
permission denined: -K````可能没密码

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

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

5.ansible中的常用模块

所有模块的应用都类似,掌握其灵活用法

5.1.command

查看相关模块信息ansible-doc -s command

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

常用参数

chdir 执行命令前先进入到指定目录 相当于cd
cmd ##运行命令指定 cmd=命令的绝对路径
creates 如果文件存在不运行
removes 如果文件存在在将````运行.
free_form 在远程主机中执行的命令,(eg:touch file)此参数不需要加

实例

[lee@ansible ~]$ ansible all -m command -a 'pwd' 家目录
[lee@ansible ~]$ ansible all -m command -a 'chdir=/mnt pwd' 切换到/mnt目录

在这里插入图片描述
[lee@ansible ~]$ ansible all -m command -a 'chdir=/mnt creates=/mnt pwd' create:存在的时候执行pwd命令
[lee@ansible ~]$ ansible all -m command -a 'chdir=/mnt removes=/mnt pwd' removes:存在的时候执行pwd命令

在这里插入图片描述文件存在的时后再执行创建会改变文件的时间戳

在这里插入图片描述在这里插入图片描述cmd 存在一些错误

在这里插入图片描述

注意
Linux中的很多通配符command模块中不支持 shell可以

5.2.shell

功能
和command功能类似

常用参数

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

实例

echo $$ ps | grep $$ ps $$
查看当前程序的id
在这里插入图片描述[lee@ansible ~]$ ansible all -m shell -a 'executable=/bin/bash ps aux | grep $$'指定执行环境
在这里插入图片描述其他命令类似于command

5.3.script

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

实例

在ansible 主机中写好脚本

[lee@ansible ~]$ cat test.sh
#!/bin/bash
rm -fr /mnt/*
touch /mnt/westosfile

在受控主机中运行
[lee@ansible ~]$ ansible all -m script -a 'test.sh'

在这里插入图片描述

5.4.copy(相当于上传)

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

常用参数

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

实例

将ansible本机的westos文件复制到受控主机的/mnt/test.sh文件ansible all -m copy -a 'src=westos dest=/mnt/test.sh'复制的时候会覆盖源文件

在这里插入图片描述
在这里插入图片描述
复制的时候备份原文件 ansible all -m copy -a 'src=test.sh dest=/mnt/test.sh backup=yes'
在这里插入图片描述在这里插入图片描述指定组,权限ansible all -m copy -a 'src=test.sh dest=/mnt/test.sh owner=lee group=westos mode=752'

在这里插入图片描述
指定内容
ansible all -m copy -a 'content="hello westos " dest=/mnt/hellowestos'
ansible all -a 'cat /mnt/hellowestos'
在这里插入图片描述在这里插入图片描述

5.5.fetch

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

常用参数

src		##受控主机的源文件
dest		##本机目录

实例

ansible all -m fetch -a 'src=/etc/hostname dest=/home/lee'受控主机的/etc/hostname的内容复制到ansible主机的家目录

在这里插入图片描述在这里插入图片描述

5.6.file

功能

设置文件的属性

常用参数

path 指定文件名称
state 指定操作状态 =touch建立 ; = absent删除;=directory建立目录(在修改目录的某些属性时,其里面的文件的某些属性是否发生改变 加resurse=yes 时,递归完成) ;=link 建立链接(快捷方式);= hard 硬链接
mode 设定权限
owner 设定文件用户
group 设定文件组
src 源文件
dest 目标文件

不能识别*
不同版本的ansible在一些指令的应用时,存在一些差异,ansible-doc -s 模块能更好的应用指令。 ansible-doc file
ansible all -m file -a 'path=/mnt/westos state=directory建立目录
ansible all -m file -a 'path=/mnt/westos state=directory mode=777 resurse=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' 建立,管理文件的权限,所有组,

5.7.unarchive

功能
解压缩

常用参数

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

实例

cp /etc/{passwd,group,fstab} .
ll
tar zcf westos.tar.gz *      tar zcf westos.tar.gz passwd
tar tf westos.tar.gz  :查看

ansible all -m unarchive -a 'src=/mnt/westos.tar.gz dest=/mnt owner=lee mode=777'
copy默认值为yes

在这里插入图片描述

ansible all -m unarchive -a "src=/mnt/westos.tar.gz dest=/mnt copy=no mode=777"
包在受控主机,解压到受控主机
在这里插入图片描述

5.8.archive

作用
压缩

常用参数

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

实例
ansible all -m archive -a 'path=/etc dest=/mnt/test.tar.bz2 format=bz2 owner=lee mode=700' ###/etc所有内容打包到/mnt/test.tar.bz2

在这里插入图片描述

[lee@ansible ~]$ ll /mnt
total 4632
-rwxrwxrwx. 1 lee root 4740258 Sep 16 20:51 test.tar.bz2
[lee@ansible ~]$ 

5.9hostname

作用
管理主机名称

常用参数

name		指定主机名称

实例

ansbile 192.168.3.1 -m hostname -a 'name=lzy.westos.com' 修改node1的主机名为lzy.westos.com
watch -n 1 sh /mnt/test.sh hostname

在这里插入图片描述

5.10cron

作用
计划任务

常用参数

minute 分钟
hour 小时
day
month
weekday
name 任务名称
job 任务脚本或命令
disabled yes 禁用计划任务;no 启动计划任务
state absent 删除计划任务

实例

ansible list1 -m cron -a "job='echo hello' name=test disable=yes" 
ansible list1 -m cron -a "job='echo hello' name=test disabled=yes" -k
ansible list1 -m cron -a "job='echo hello' name=test state=absent" -k

修改监控脚本的用户为rootroot身份才能查看超级用户的定时任务
再把锁给受控主机的root用户ssh-copy-id -i .ssh/id_rsa.pub [email protected]

在这里插入图片描述sh /mnt/test.sh crontab -l -u lee
watch -n 1 sh /mnt/test.sh crontab -l
1ansible all -m cron -a 'job="echo westos" name="westos test"'

在这里插入图片描述2ansible all -m cron -a 'job="echo westos" name="westos test" minute=*/2 hour="09-18" day="1,4" month=6 weekday=7 ' 建立定时任务
在这里插入图片描述

3ansible all -m cron -a 'job="echo westos" name="westos test" minute=*/2 hour="09-18" day="1,4" month=6 weekday=7 state=absent ' 删除定时任务
在这里插入图片描述

5.11yum_repository

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

常用参数

name 指定仓库名称
baseurl 指定源路径
description 指定仓库描述
file 指定仓库文件名称默认后缀为.repo
enabled 仓库是否启用 yes=1 no=0
gpgcheck 仓库是否检测gpgkey
state 默认值present 建立;absent 为删除

实例

watch -n 1 "sh test.sh ls -l /etc/yum.repos.d/westos.repo; sh test.sh cat /etc/yum.repos.d/westos.repo"
1ansible all -m yum_repository -a 'file=westos name="AppStream" baseurl=http://192.168.3.12/rhel8/AppStream gpgcheck=0 description=AppStream_westos enabled=yes'
BaseOS也是相同的方式
在这里插入图片描述present 建立
absent 删除

5.12dnf

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

常用参数

name 指定包 name=httpd
state ##指定动作# present 安装#latest 最新版本;#absent 删除
list ##列出指定信息#list= httpd# list=installed list=all;# list=available
disable_gpg_check #禁用gpgkey检测
enablerepo ##指定安装包来源
disablerepo ##禁用安装包来源
autoremove yes:卸载软件,保留依赖性;no:软件和依赖性全部卸载掉

实例

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'

latest安装最新版本,即对系统内的此软件进行更新
在这里插入图片描述

5.13service

作用
管理系统服务状态

常用参数

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

ansible 192.168.3.1 -m service -a 'name=httpd state=started enabled=yes
在这里插入图片描述在这里插入图片描述

5.14user

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

常用参数

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 remove=yes 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'

此方式相当于更改/etc/passwd文件
1ansible 192.168.3.1 -m user -a 'name=haha1 group=1001 groups=lee state=present'建立 (删除也是相同的方法)

在这里插入图片描述
2
openssl passwd -6 'haha1'将haha1字符加密``

ansible 192.168.3.1 -m user -a 'name=haha1 password=$6$8zQ9lV5wdBGWazTn$vqgP0o5qMCj0TKkJPYTqUrALXckU4naDw4DQFlbc6gL/uyXVgbzR/H57n1HFVEkOWLD5xSRzzFdxz9CiHkI/C/'
将加密字符设为haha1用户的密码。即haha1用户的密码为haha1

在这里插入图片描述在这里插入图片描述

5.15group

作用
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'

ansible 192.168.3.1 -m group -a 'name=hahawestos gid=8888 state=present'在受控主机1上建立一个hahawestos组,组id为8888.

在这里插入图片描述删除也是同样的方式

5.16lineinfile

path 指定要操作的文件
line 指定文本内容。
regexp 1.使用正则表达式匹配对应的行当替换文本时 如果有多行文本都能被匹配,则只有最后面被匹配到的那行文本才会被替换2当删除文本时,如果有多行文本都能被匹配 这么这些行都会被删除。
state 当想要删除对应的文本时需要将state参数的值设置为absent state的默认值为present。
backrefs 当内容无匹配规则时不对文件做任何更改,默认值为no向后引用regexp变量信息
insertafter 借助insertafter参数可以将文本插入到“指定的行”之后insertafter参数的值可以设置为EOF或者正则表达式
insertbefore 借助insertbefore参数可以将文本插入到“指定的行”之前insertbefore参数的值可以设置为BOF或者正则表达式
backup 是否在修改文件之前对文件进行备份。
create 当要操作的文件并不存在时,是否创建对应的文件。

实例

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"'

1ansible 192.168.3.1 -m lineinfile -a 'path=/mnt/westos line="hello westos"' 给受控主机中插入hello westos

在这里插入图片描述2ansible 192.168.3.1 -m lineinfile -a 'path=/mnt/westos regexp="^westos" line="hello lee"' 将文件中以westos开头的内容改为hello lee
在这里插入图片描述3
ansible 192.168.3.1 -m lineinfile -a 'path=/mnt/westos line="###### westos end ######" insertafter=EOF' 文件的结尾插入内容
在这里插入图片描述
4

ansible 192.168.3.1 -m lineinfile -a 'path=/mnt/westos line="###### westos test #####" insertbefore=BOF' 插入内容到文件的最前面
在这里插入图片描述 5
backrefs=yes向后引用regexp匹配变量
不引用的时候无法识别\1. :regrxp匹配到的第一个字符
ansible 192.168.3.1 -m lineinfile -a 'path=/mnt/westos regexp="(h.{4}).*(w.{5})" line="\1" '

在这里插入图片描述ansible 192.168.3.1 -m lineinfile -a 'path=/mnt/westos regexp="(h.{4}).*(l.{2})" line="\1" backrefs=yes'
匹配到hello ,将匹配到的内容行,换为hello,

在这里插入图片描述

5.17replace

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

常用参数

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

实例
ansible 192.168.3.1 -m replace -a 'path=/mnt/westos regexp="hello" replace="westos_lee" backup=yes' 将受控主机1中的/mnt/westos文件里的hello内容换为westos_lee,并备份原文件

在这里插入图片描述

5.18setup

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

常用参数

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

实例

ansible all -m setup -k
ansible all -m setup   -a "filter='ansible_all_ipv4_addresses'" -k

1ansible 192.168.3.1 -m setup列出受控主机的基本信息
在这里插入图片描述2ansible 192.168.3.1 -m setup -a 'filter='ansible_virtualization_role''
列出指定信息

在这里插入图片描述

5.19debug

作用

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

常用参数:

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

猜你喜欢

转载自blog.csdn.net/ninimino/article/details/108542926