Ansible第二章:ansible中常用的模块

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密码

注意: 第一章实验生成的密钥是临时的,之后在执行命令的时候要-k输入密码。
     可以再次生成新的密钥(ssh-keygen)然后给被控机传输过去

实验:

su - devops
cd .ansible
vim test.yml
  - name: test
    hosts: westos
    tasks:
      - name: check hostname
        shell: hostname

ansible-playbook test.yml

ansible-playbook test.yml -v

ansible westos -m shell -a 'touch /mnt/test1'

ansible westos -m shell -a 'rm -rf /mnt/*'

ansible westos -m shell -a 'whoami'

vim ansible.cfg
  注释掉: #become=True

ansible westos -m shell -a 'whoami' -u westos -k

ansible westos -m shell -a 'whoami' -u devops -k

ansible westos -m shell -a 'whoami' -u devops -k -b

ansible westos -m shell -a 'whoami' -b -K

 

 

 4.ansible的基本颜色代表信
绿色        ##执行成功但为对远程主机做任何改变
黄色        ##执行成功并对远程主机做改变
红色        ##执行失败

5.ansible中的常用模块
5.1 command
功能: 在远程主机执行命令,此模块为默认模块

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

实验:

ansible westos -m command -a 'hostname'  #执行hostname命令

ansible westos -m command -a 'hostname creates=/mnt/file'  
#如果被控机/mnt/file文件存在的话就不运行hostname【反过来说,如果不存在就运行hostname】

ansible westos -m command -a 'hostname removes=/mnt/file'  
#如果被控机/mnt/file文件存在的话就运行hostname【反过来说如果文件不存在就不运行】

ansible westos -m command -a 'pwd' #显示所在目录

ansible westos -m command -a 'chdir=/mnt pwd' 
#执行命令的时候先进入/mnt目录【pwd最后显示所在目录是/mnt】

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

5.2 shell
功能:和command功能类似

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

实验:

ansible westos -m shell -a 'ps'   #查看当前正在运行的进程

ansible westos -m shell -a 'executable=/bin/bash ps ax | grep $$'    
#指定执行环境为/bin/bash,默认为sh

ansible westos -m shell -a ' ps ax | grep $$'  #查看当前目录所在的进程

 补充:
echo $$   #查看当前正在运行的进程id
ps ax | grep $$  #过滤当前进程id

 

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

实例:

vim /mnt/westos.sh  #要在超级用户中编写
#!/bin/bash
echo $HOSTNAME

su - devops

cd .ansible

ansible westos -m script -a "/mnt/westos.sh" -k      

 

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

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

实验:

ansible westos -m copy -a "src=./westos.sh dest=/mnt/westos.sh"

ansible westos -m copy -a "src=./westos.sh dest=/mnt/westos.sh mode=700 owner=westos group=westos" 
#执行完给权限和指定用户以及组名称后可以在nodeb被控机中:ll查看

ansible westos -m copy -a "content='hello westos' dest=/mnt/westos.sh mode=700 owner=westos group=westos"          
#执行后可以在nodeb被控机中cat westos.sh 查看

 

 

 

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

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

实验:

ansible westos -m fetch -a 'src=/mnt/westos.sh dest=/tmp' 
#执行后会把源文件的上级目录都保存过来

ls /tmp/

cat /tmp/westos.sh

cat /tmp/172.25.254.217/mnt/westos.sh

ansible westos -m fetch -a 'src=/mnt/westos.sh dest=/tmp/haha flat=yes'     
#执行本条命令不会把源文件的上级目录保存,haha是个新的文件名

5.6 file
功能 :  设置文件的属性

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

实验:

ansible westos -m file -a 'path=/mnt/test.sh state=touch'   #建立文件

ansible westos -m file -a 'path=/mnt/test.sh state=absent'   #删除文件

ansible westos -m file -a 'path=/mnt/westos state=directory'    #建立目录

ansible westos -m file -a 'path=/mnt/westos state=directory mode=777 recurse=yes' 
 #修改目录以及目录里文件的权限

ansible westos -m file -a 'src=/mnt/file dest=/mnt/westos state=link'  
#生成软连接【被控机用命令ll查看】

ansible westos -m file -a 'src=/mnt/file dest=/mnt/westos1 state=hard' 
 #生成硬连接【被控机ls -i查看】

ansible westos -m file -a 'path=/mnt/file state=touch owner=lee group=westos mode=777'  
#建立文件,给权限,设置所有人和所有组

 

 

 

5.7 archive
作用:压缩

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

#实例

ansible all -m archive -a 'path=/etc dest=/opt/etc.tar.gz format=gz owner=westos mode=700'
 #把受控机中/etc压缩到受控机/opt中,压缩文件名是etc.tar.gz。压缩类型是gz,压缩后所有人是westos

5.8 unarchive
功能:解压缩


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

#实例
 

ansible westos -m unarchive -a 'src=/opt/etc.tar.gz dest=/mnt owner=lee'  
#把主控机中/opt/etc.tar.gz解压到受控机/mnt里,解压后所有人是lee
【copy默认是yes】[把主控机文件解压到受控机中前提是主控机中必须有压缩文件]

ansible westos -m unarchive -a "src=/opt/etc.tar.gz dest=/mnt copy=no"   
#把受控机中/opt/etc.tar.gz解压到受控机/mnt里【copy=no等同于remote_src=yes】

 

5.9 hostname
作用  :管理主机名称

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

#实例
ansible westos -m hostname -a 'name=westos217.westos.com'

5.10 cron
作用:  计划任务

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

实例:

ansible westos -m cron -a 'job="touch /mnt/westosfile" name=test minute=54 hour=14 '  
 #在14:54分的时候在/mnt目录建立westosfile文件

ansible westos -m cron -a 'job="touch /mnt/westosfile" name=test minute=*/2 hour=14 '  
 #在14点时,每隔两分钟执行一次test任务--->建立/mnt/westosfile文件

ansible westos -m cron -a 'job="touch /mnt/westosfile" name=test minute=*/2 hour=14 disabled=yes'  
#禁止这个任务

ansible westos -m cron -a 'job="touch /mnt/westosfile" name=test minute=*/2 hour=14 disabled=no'  
#启用这个目录

ansible westos -m cron -a 'job="touch /mnt/westosfile" name=test  state=absent'   
#删除这个目录

 

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

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

#实例:

ansible westos -m yum_repository -a 'file=westos name=AppStream description="rhel8.2 
AppStream" baseurl=http://172.25.254.250/rhel8.2/AppStream gpgcheck=no enabled=yes 
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release'

ansible westos -m yum_repository -a 'file=westos name=BaseOS description="rhel8.2 BaseOS" 
baseurl=http://172.25.254.250/rhel8.2/BaseOS gpgcheck=no enabled=yes 
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release'

ansible westos -m yum_repository -a "name=AppStream  file=westos_test state=absent" -k

 

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

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


实例:

ansible westos -m dnf -a "name=httpd state=present" 
 #给被控机安装httpd服务

ansible westos -m dnf -a 'name=httpd state=absent'

ansible westos -m dnf -a 'name=httpd state=absent autoremove=no'   
 #卸载httpd服务,但不删除依赖关系

ansible westos -m dnf -a 'name=httpd state=absent autoremove=yes'  
 #卸载httpd服务,也卸载依赖关系

ansible westos -m dnf -a 'name=httpd state=present enablerepo=AppStream'
指定下载的源(通过AppStream来安装)

ansible westos -m dnf -a 'list=httpd'   
#列出httpd的相关信息

ansible westos -m dnf -a 'name="*" state=latest'    
#更新所有的软件

ansible westos -m dnf -a 'name="@Virtual Tools" state=present' 
 #下载隐藏组

 

5.13 service
作用:  管理系统服务状态

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

实例:

ansible westos -m shell -a 'rpm -qa | grep httpd'   
#查看httpd服务是否下载

ansible westos -m service -a "name=httpd state=started enabled=yes"  
 #开启httpd服务,并指定开机启动	

ansible westos -m service -a "name=httpd state=restarted enabled=yes"  
  #重启httpd服务【在nodeb中可以通过systemctl status httpd 可以看pid重启之后pid会变】

 

 

5.14 firewalld

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

实验:

ansible westos -m firewalld -a 'zone=public service=http permanent=yes 
state=enabled immediate=yes'   
#开启火墙并永久指定火墙的域为public 且立即生效,


在被控主机中:
firewall-cmd --list-all   #列出火墙的所有服务
在火狐中查看172.25.254.217 可以查看

 

 

5.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 westos -m user -a 'name=test'   
#建立test用户

ansible westos -m user -a 'name=test uid=6666'  
#指定test用户的uid为8888

ansible westos -m user -a 'name=test group=westos' 
  #指定test用户所在的组为westos

ansible westos -m user -a 'name=test group=test'     
#指定test用户所在的组为test

ansible westos -m user -a 'name=test groups=westos'   
#指定用户所在的附加组为westos

ansible westos -m user -a 'name=test groups=72'     
#改变test用户附加组为72

ansible westos -m user -a 'name=test groups=westos append=yes'  
#在原有附加组的基础上添加westos附加组

ansible westos -m user -a 'name=test comment="test user"'   
 指定test用户的注释信息【可在nodeb中 cat /etc/passwd 中查看;附加信息在第五列】
【以上每次对test用户的修改都可以在nodeb中用id test查看

openssl passwd -6    #设置密码

ansible westos -m user -a "name=test password=
'\$6\$9axXtqP62rfscsbb$keVFjxmJMfE35AGd10ytxFEMzt2ux8jWMNkINiof3cmvXc
E9B4UngwJC.ys2Bqp34DcVW/ccoIYhsHpAkshap.'"         
 #生成加密字符【$符是特殊字符,所有要用转译字符】

ansible westos -m user -a "name=test password=
'\$6\$9axXtqP62rfscsbb$keVFjxmJMfE35AGd10ytxFEMzt2ux8jWMNkINiof3cmv
XcE9B4UngwJC.ys2Bqp34DcVW/ccoIYhsHpAkshap.'generate_ssh_key" #生成密钥



在nodeb中:

cd .ssh/
ls #查看目录里文件
cat /etc/shadow  #查看密码,只能用超级用户登陆

 

 

 

 

 

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


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

gid          ##用于指定组的gid。

实例 

ansible westos -m group -a 'name=test'        
#添加test组

ansible westos -m group -a 'name=test gid=8888' 
 #指定test组的gid为8888

ansible westos -m group -a 'name=test state=absent'   
#删除test组





在nodeb中:
cat /etc/group  #查看组的信息

 

 

5.17 lineinfile

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

补充:多行匹配只能替换最后一行,删除是删除全部。

实例:

ansible westos -m copy -a 'content="hello westos\nhello test\nhello linux\n" dest=/mnt/westos '  
 #给被控机建立/mnt下的westos文件,并编写内容\n表示换行

ansible westos -m lineinfile -a 'path=/mnt/westos line="nihao"' 
 #在已经存在的文本(/mnt/westos)中写入nihao

ansible westos -m lineinfile -a 'path=/mnt/westos regexp="^hello" line="hello westos" '   
#把以hello开头的行替替换成hello westos 【匹配到多行的替换只能替换最后一行,其他行不进行替换】

ansible westos -m lineinfile -a 'path=/mnt/westos regexp="^hello" state=absent'    
#把以hello开头的行全部删除 【匹配到多行的删除会全部删除】

ansible westos -m lineinfile -a 'path=/mnt/westos regexp="(h.{4}.*(w.{5}))" line="\1" backrefs=no'  
#将westos文件中满足条件【h后边的任意四个字符,中间任意字符,w后任意五个字符】的行替换为字符\1  
{因为 backrefs=no就不向后引用regexp}

ansible westos -m lineinfile -a 'path=/mnt/westos regexp="(h.{4}).*(w.{5})" line="\1" backrefs=yes'    
#将westos文件中满足条件【h后边的任意四个字符,中间任意字符,w后任意五个字符】的行替换为regexp的第一部分条件

ansible westos -m lineinfile -a 'path=/mnt/westos line="#######ok##########" insertafter=EOF' 
 #在文件中最后一行后添加#######ok##########

ansible westos -m lineinfile -a 'path=/mnt/westos line="#######ok##########" insertafter=hello'   
#在hello字符前添加#######ok##########;【匹配到多行,则在最后一行有hello字符前添加】

ansible westos -m lineinfile -a 'path=/mnt/westos line="#######ok##########" insertbefore=BOF'   
#在第一行前添加#######ok##########

ansible westos -m lineinfile -a 'path=/mnt/westos line="#######ok##########" insertbefore=test'   
#在test字符前一行添加

 

 

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

实验:
vim test.yml
  - name: test
    hosts: westos
    tasks:
      - lineinfile:
          path: /mnt/westos
          line: |+
            123
            456
            789

ansible-playbook test.yml    #运行


在被控机nodeb中:
cat westos   #查看,有|+的会自动换行,没有的话,所有的都在一行

 

 

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

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

实例:

ansible all -m replace -a 'path=/mnt/westos regexp="hello" replace="lee" backup=yes'  
#把带有hello字符的全部替换成lee,并且备份westos原文件

 

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

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

ansible westos -m setup -a "filter='ansible_fqdn'"  
#显示被控机的主机名

ansible westos -m setup   -a "filter='ansible_all_ipv4_addresses'"    
#显示被控机的ip地址

 

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

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

实验:

ansible westos -m debug -a 'msg=hello'  #输出hello

vim test.yml
  - name: test
    hosts: westos
    tasks:
      - name: debug
        debug:
          var: ansible_facts['fqdn']   
 #输出被控机的主机名【不能用ansible命令,因为看不到结果,但是可以在playbook中看到效果】 

ansible-playbook test.yml  #运行

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值