1.ansible实现管理的方式

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

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

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

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

常用参数

  • -l ##列出可用模块
  • -s ###显示指定模块的playbook片段
ansible-doc -l   ##列出可用模块
ansible-doc command -s  ###显示指定模块的playubook片段

ansible synchronize 使用 ansibledoc_hive


ansible synchronize 使用 ansibledoc_hive_02

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

  • 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 --version  ##查看版本
ansible westos -m shell -a 'id'  ##指定模块shell
ansible westos -m ping -v  ##显示详细信息
ansible westos -m ping -vv   ##显示更具体信息
ansible all --list  ##显示所有受控主机
ansible westos -m ping -k  ##提示输入ssh密码
ansible westos -m shell -a 'useradd hahha' -C ##检测添加用户是否可以执行成功
ansible westos -m shell -a 'userdel hahha' -T 2
ansible westos -m shell -a 'id' -u root -k ##指定登陆用户
ansible westos -m shell -a 'id' -u root -k -b --become-user=westos  ##指定sudo用户
ansible westos -m shell -a 'id' -u root -k -b --become-user=westos -K  ##提示输入sudo密码

ansible synchronize 使用 ansibledoc_源文件_03


ansible synchronize 使用 ansibledoc_hive_04


ansible synchronize 使用 ansibledoc_linux_05


ansible synchronize 使用 ansibledoc_文件权限_06


ansible synchronize 使用 ansibledoc_源文件_07


ansible synchronize 使用 ansibledoc_文件权限_08


ansible synchronize 使用 ansibledoc_文件权限_09


ansible synchronize 使用 ansibledoc_文件权限_10


ansible synchronize 使用 ansibledoc_文件权限_11


ansible synchronize 使用 ansibledoc_文件权限_12

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

  • 绿色 ##执行成功但未对远程主机做任何改变
  • 黄色 ##执行成功但对远程主机进行了改变
  • 红色 ##执行失败
ansible westos -m ping
ansible westos -m shell -a 'useradd haha' 
ansible westos -m shell -a 'id'

ansible synchronize 使用 ansibledoc_文件权限_13


ansible synchronize 使用 ansibledoc_源文件_14


ansible synchronize 使用 ansibledoc_文件权限_15

5.ansible中的常用模块

1.command

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

常用参数

  • chdir ##执行命令前先进入到指定目录
  • cmd ##运行命令指定(可不写)
  • creates ##如果文件存在将不运行
  • removes ##如果文件存在将运行
  • free_form ##在远程主机中执行的命令,此参数不需要加
  • linux中许多通配符不支持
ansible westos -m command -a 'chdir=/etc cat passwd'  ##指定/etc,查看passwd文件
ansible westos -m command -a 'removes=/mnt/file touch /mnt/file'  ##/mnt/files不存在,无法执行
ansible westos -m command -a 'creates=/mnt/file touch /mnt/file'  ##/mnt/files不存在,创建
ansible westos -m command -a 'removes=/mnt/file touch /mnt/file'  ##
ansible westos -m command  -a "ps ax | grep $$" ##无法执行,command不支持通配符

ansible synchronize 使用 ansibledoc_linux_16


ansible synchronize 使用 ansibledoc_linux_17


ansible synchronize 使用 ansibledoc_文件权限_18


ansible synchronize 使用 ansibledoc_linux_19


ansible synchronize 使用 ansibledoc_文件权限_20

2.shell

功能:与command许多功能相似

  • chdir ##执行命令前先进入到指定目录
  • cmd ##运行命令指定(可不写)
  • creates ##如果文件存在将不运行
  • removes ##如果文件存在将运行
  • free_form ##在远程主机中执行的命令,此参数不需要加
  • executable ##指定执行环境,默认为shell
ansible westos -m shell  -a "ps ax | grep $$" ##支持通配符
ansible westos -m shell  -a "executable=/bin/bash ps ax | grep $$"  ##指定执行环境

ansible synchronize 使用 ansibledoc_文件权限_21


ansible synchronize 使用 ansibledoc_hive_22

3.script

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

实例:

vim /sh /mnt/westos.sh 
mnt/westos.sh
estos_linux.westos.org
ansible all -m script -a "/mnt/westos.sh" ##将脚本在受控主机中运行

ansible synchronize 使用 ansibledoc_hive_23


ansible synchronize 使用 ansibledoc_linux_24

4.copy

常用参数

  • src ##源文件
  • dest ##目的地文件
  • owener ##指定目的地文件所有人
  • gorup
  • mode ##指定目的地文件权限
  • backup=yes ##当受控主机中存在文件时备份源文件
  • content‘ ##指定文件内容直接在受控主机中生成文件

test:

ansible westos -m copy -a 'src=/mnt/westos1 dest=/mnt'  ##复制本机文件/mnt/westos1至受控主机/mnt下
ansible westos -m copy -a 'src=/mnt/westos1 dest=/mnt mode=755' ##指定权限为755
ansible westos -m copy -a 'src=/mnt/westos1 dest=/mnt mode=755 owner=westos group=westos'
##指定文件所有人所有组
ansible westos -m copy -a 'src=/mnt/westos1  dest=/mnt mode=755 owner=westos group=westos backup=yes'  ##指定文件需复制备份
ansible westos -m copy -a 'content="hello luqq\n"  dest=/mnt/lee mode=755 owner=westos group=westos'  ##直接将内容传送至受控主机文件

ansible synchronize 使用 ansibledoc_源文件_25


ansible synchronize 使用 ansibledoc_源文件_26


ansible synchronize 使用 ansibledoc_源文件_27


ansible synchronize 使用 ansibledoc_hive_28


ansible synchronize 使用 ansibledoc_源文件_29


ansible synchronize 使用 ansibledoc_linux_30


ansible synchronize 使用 ansibledoc_hive_31


ansible synchronize 使用 ansibledoc_hive_32


ansible synchronize 使用 ansibledoc_源文件_33


ansible synchronize 使用 ansibledoc_hive_34


ansible synchronize 使用 ansibledoc_hive_35

5.fetch

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

常用参数

  • src ##受控主机的源文件
  • dest ##本机目录
  • flat ##基本名称功能 (添加代表本机目的地为文件,而不是目录)
ansible westos -m fetch -a 'src=/etc/hostname dest=/mnt'  ##将其复制至/mnt目录下,会产生两个文件
ansible westos -m fetch -a 'src=/etc/hostname dest=/mnt/hostname flat=yes' ##指定文件,需加flat=yes,查看会发现只有一个结果,因为文件第二次执行会覆盖第一次结果
cat /mnt/hostname

ansible synchronize 使用 ansibledoc_源文件_36


ansible synchronize 使用 ansibledoc_hive_37


ansible synchronize 使用 ansibledoc_源文件_38


ansible synchronize 使用 ansibledoc_文件权限_39

ansible westos -m fetch -a 'src=/etc/hostname dest=/mnt/hello'  ##复制至目录
cat /mnt/hello/172.25.254.109/etc/hostname ##两结果均可查看
cat /mnt/hello/172.25.254.209/etc/hostname

ansible synchronize 使用 ansibledoc_linux_40


ansible synchronize 使用 ansibledoc_文件权限_41

6.file

功能:设置文件属性

  • 常用参数:
    path ##指定文件名称
  • state ##指定操作状态
    touch ##创建
    absent ##删除
    directory ##创建目录
    link ##建立连接
    hard ##建立硬连接
  • mode ##设定权限
    owner ##设定文件所有人
    group ##设定文件所有组
    src ##源文件
    dest ##目标文件
    recurse ##递归更改
cd .ansible/  ##操作需在ansible目录下执行
ansible westos -m shell -a 'rm -rf /mnt/*'  ##实验准备,清空受控主机/mnt目录

ansible synchronize 使用 ansibledoc_linux_42


ansible synchronize 使用 ansibledoc_源文件_43

ansible westos -m file -a 'path=/mnt/westos state=touch'  ##创建文件
ansible westos -m shell -a 'ls -l /mnt/' ##查看受控主机是否创建

ansible synchronize 使用 ansibledoc_文件权限_44

ansible westos -m file -a 'path=/mnt/westos state=absent'  ##删除文件
ansible westos -m shell -a 'ls -l /mnt/'  ##查看

ansible synchronize 使用 ansibledoc_文件权限_45

ansible westos -m file -a 'path=/mnt/westos state=touch mode=777 owner=westos group=westos'
  ##创建文件,权限为777所有人所有组均为westos
ansible westos -m shell -a 'ls -l /mnt/' ##查看

ansible synchronize 使用 ansibledoc_hive_46


ansible synchronize 使用 ansibledoc_linux_47

ansible westos -m file -a 'path=/mnt/westosfile state=touch'  ##创建文件
ansible westos -m shell -a 'ls -l /mnt/'  ##查看
ansible westos -m file -a 'src=/mnt/westosfile dest=/mnt/westoslu state=link'  ##文件创建软连接,文件号不与源文件一致
ansible westos -m shell -a 'ls -l /mnt/' ##查看

ansible synchronize 使用 ansibledoc_源文件_48


ansible synchronize 使用 ansibledoc_linux_49

ansible westos -m file -a 'path=/mnt/westosdir state=directory'  ##创建目录
ansible westos -m shell -a 'ls -l /mnt/'
ansible westos -m file -a 'path=/mnt/westosdir/file state=touch'  ##创建子目录文件
ansible westos -m shell -a 'ls -lR /mnt/'
ansible westos -m file -a 'path=/mnt/westosdir mode=777'   ##更改目录权限
ansible westos -m shell -a 'ls -lR /mnt/'  ##仅子目录权限更改,其下文件权限未更改
ansible westos -m file -a 'path=/mnt/westosdir mode=777 recurse=yes'  ##递归更改文件权限
ansible westos -m shell -a 'ls -lR /mnt/'  ##子目录与其下文件权限全部更改

ansible synchronize 使用 ansibledoc_文件权限_50


ansible synchronize 使用 ansibledoc_hive_51


ansible synchronize 使用 ansibledoc_hive_52


ansible synchronize 使用 ansibledoc_linux_53


ansible synchronize 使用 ansibledoc_源文件_54


ansible synchronize 使用 ansibledoc_文件权限_55


ansible synchronize 使用 ansibledoc_文件权限_56


ansible synchronize 使用 ansibledoc_linux_57

ansible westos -m file -a 'src=/mnt/westosfile dest=/mnt/westoslee state=hard'  ##创建硬链接
ansible westos -m shell -a 'ls -li /mnt/'  ##硬链接文件号与源文件一致

ansible synchronize 使用 ansibledoc_文件权限_58


ansible synchronize 使用 ansibledoc_hive_59


ansible synchronize 使用 ansibledoc_hive_60

7.unarchive

功能: 解压缩

常用参数

  • copy ##默认为yes 从ansible主机复制文件到受控主机
    ##设定为no 从受控主机寻找src文件
  • remote_src ##功能与copy相反,yes文件在受控主机中,no文件在ansible主机中
  • src ##包路径,可以是ansible主机,也可以是受控主机
  • dest ##受控主机目录
  • mode ##解压后文件权限,权限参数不可在源文件在受控主机的情况下使用,即在copy=no时不生效
ansible westos -m shell -a ' rm -rf /mnt/*'  ##删除/mnt下文件
ansible westos -m shell -a 'ls -l /mnt/'
 ansible westos -m unarchive -a 'src=/mnt/etc.tar.gz dest=/mnt copy=no'  ##解压源文件在受控主机中的压缩包
ansible westos -m shell -a 'ls -l /mnt/' ##查看,解压完成

ansible synchronize 使用 ansibledoc_hive_61

ansible westos -m  file -a 'path=/mnt/etc/ state=absent' ##删除解压文件
tar zcf /mnt/etc.tar.gz /etc  ##ansible主机打包文件
ansible westos -m unarchive -a 'src=/mnt/etc.tar.gz dest=/mnt  mode='1777''  ##将其解压至受控主机,可指定权限
ansible westos -m shell -a 'ls -l /mnt/'  ##查看权限

ansible synchronize 使用 ansibledoc_源文件_62


ansible synchronize 使用 ansibledoc_文件权限_63

ansible synchronize 使用 ansibledoc_文件权限_64


ansible synchronize 使用 ansibledoc_文件权限_65


ansible synchronize 使用 ansibledoc_hive_66


ansible synchronize 使用 ansibledoc_源文件_67

8.archive

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

ansible westos -m archive -a 'path=/etc dest=/mnt/etc.tar.gz format=gz owner=westos mode=755'
##打包受控主机/etc目录,指定打包格式及文件所有人及权限
ansible westos -m shell -a 'ls -l /mnt/'  ##查看

ansible synchronize 使用 ansibledoc_linux_68


ansible synchronize 使用 ansibledoc_hive_69

9.cron

功能:
计划任务

常用参数:

  • miunte ##分钟
  • hour ###小时
  • month ##月
  • day ##天
  • weekday ##周
  • name ##任务名称
  • job ##任务脚本或命令
  • disabled ##yes 禁用计划任务 no 启用计划任务
  • state ##absent删除计划任务
ansible westos -m shell -a 'crontab -l'  ##查看是否有定时任务
ansible westos -m cron -a 'job=date name=westostest minute=*/2'  ##创建定时任务
ansible westos -m shell -a 'crontab -l'  ##查看任务

ansible synchronize 使用 ansibledoc_源文件_70


ansible synchronize 使用 ansibledoc_hive_71

ansible westos -m cron -a 'job=date name=westostest disabled=yes'  ##禁用用任务
ansible westos -m shell -a 'crontab -l'
ansible westos -m cron -a 'job=date name=westostest disabled=no'  ##启用任务
ansible westos -m shell -a 'crontab -l'

ansible synchronize 使用 ansibledoc_文件权限_72


ansible synchronize 使用 ansibledoc_源文件_73


ansible synchronize 使用 ansibledoc_linux_74


ansible synchronize 使用 ansibledoc_hive_75

ansible ansible westos -m cron -a 'job=date name=westostest state=absent'  ##删除任务
westos -m shell -a 'crontab -l'  ##查看

ansible synchronize 使用 ansibledoc_文件权限_76


ansible synchronize 使用 ansibledoc_hive_77

10.hostname

功能
管理主机名称

常用参数;

  • name ##指定主机名称
ansible westos -m shell -a 'hostname'  ##查看名称
ansible 172.25.254.109 -m shell -a 'hostname'
ansible 172.25.254.109 -m hostname -a 'name=luqqq'  ##修改主机172.25.254.109名称
ansible 172.25.254.109 -m shell -a 'hostname'  ##查看名称
ansible 172.25.254.109 -m hostname -a 'name=westos_node1.westos.org'  ##修改名称
ansible 172.25.254.109 -m shell -a 'hostname'  ##查看名称

ansible synchronize 使用 ansibledoc_文件权限_78

文件后缀无需写,自动添加

11.yum_repository

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

常用参数

  • name ##指定仓库名称
  • baseurl ##指定源路径
  • description ##指定仓库描述
  • file ##指定仓库文件名称
  • enabled ##仓库是否启用
  • gpgcheck ##仓库是否检测gpgkey
  • state ##默认为present建立
    ##absent删除
ansible westos -m shell -a 'ls /etc/yum.repos.d/'  ##查看是否存在仓库文件
ansible  westos -m yum_repository -a "name=AppStream baseurl=http://172.25.254.9/lijiaxue/AppStream description=westosAppStream gpgcheck=0 file=westos"  ##创建AppStream仓库
ansible westos -m shell -a 'cat /etc/yum.repos.d/westos.repo'  ##查看仓库文件
ansible  westos -m yum_repository -a "name=BaseOS baseurl=http://172.25.254.9/lijiaxue/BaseOS description=westosBaseOS gpgcheck=0 file=westos"  ##创建BaseOS仓库
ansible westos -m shell -a 'cat /etc/yum.repos.d/westos.repo'  ##查看仓库文件

ansible synchronize 使用 ansibledoc_文件权限_79


ansible synchronize 使用 ansibledoc_linux_80


ansible synchronize 使用 ansibledoc_hive_81

ansible  westos -m yum_repository -a "name=BaseOS baseurl=http://172.25.254.9/lijiaxue/BaseOS description=westosBaseOS en
abled=0 file=westos"  ##禁用BaseOS仓库
ansible westos -m shell -a 'cat /etc/yum.repos.d/westos.repo'
ansible  westos -m yum_repository -a "name=BaseOS baseurl=http://172.25.254.9/lijiaxue/BaseOS description=westosBaseOS enabled=1 file=westos"  ##启用BaseOS文件
ansible westos -m shell -a 'cat /etc/yum.repos.d/westos.repo'
ansible  westos -m yum_repository -a "name=BaseOS baseurl=http://172.25.254.9/lijiaxue/BaseOS description=westosBaseOS enabled=1 file=westos state=absent"  ##删除BaseOs仓库
ansible westos -m shell -a 'cat /etc/yum.repos.d/westos.repo'

ansible synchronize 使用 ansibledoc_源文件_82

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=vsftpd state=latest'  ##更新vsftpd

ansible synchronize 使用 ansibledoc_linux_83

ansible westos -m dnf -a 'name="httpd,dhcp-server" state=latest disable_gpg_check=yes'  ##安装httpd,dhcp服务,禁用gpgcheck

ansible synchronize 使用 ansibledoc_文件权限_84

ansible westos -m dnf -a 'name=httpd state=absent autoremove=yes'  ##删除软件以及依赖性,不加autoremove=yes,仅删除安装包

ansible synchronize 使用 ansibledoc_linux_85

ansible westos -m dnf -a 'name="@Virtualization Tools" state=present disable_gpg_check=yes'
##安装组件

ansible synchronize 使用 ansibledoc_linux_86

ansible westos -m dnf -a 'name="http://172.25.254.250/software/linuxqq_2.0.0-b2-1084_x86_64.rpm" state=present disable_gpg_check=yes'  ##网络源安装

ansible synchronize 使用 ansibledoc_源文件_87

ansible westos -m dnf -a 'list=installed'  ##列出已安装软件

13.service

作用:
管理系统服务状态

常用参数:

  • name ##指定服务名称
  • state ##指定服务动作
    started
    stoped
    restarted
    reloaded
  • enabled ##设定服务开机是否启动
    ##yes开机启动
    ##no开机不启动
ansible westos -m dnf -a 'name="httpd,dhcp-server" state=latest disable_gpg_check=yes' ##安装http,dhcp服务
ansible westos -m service -a 'name=vsftpd state=started'  ##开启vsftpd服务
ansible westos -m shell -a 'systemctl status vsftpd'  ##查看服务状态
ansible westos -m service -a 'name=vsftpd state=restarted'  ##重启服务
ansible westos -m shell -a 'systemctl status vsftpd'   ##服务进程号改变

ansible synchronize 使用 ansibledoc_文件权限_88


ansible synchronize 使用 ansibledoc_linux_89

ansible westos -m service -a 'name=httpd state=stoped'  ##开启httpd服务
ansible westos -m shell -a 'systemctl status httpd'  ##查看服务状态

ansible synchronize 使用 ansibledoc_文件权限_90

ansible westos -m service -a 'name=httpd state=reloaded'   ##加载服务
ansible westos -m shell -a 'systemctl status httpd'  ##查看服务状态
ansible westos -m service -a 'name=httpd state=reloaded enabled=yes'  ##http服务开机自启
ansible westos -m shell -a 'systemctl status httpd' ##查看服务状态

14.firewalld

防火墙管理

常用参数

  • zone ##火墙的域
  • service ##服务名称
  • permanent ##永久生效
  • state
    enabled ##允许
    disabled ##拒绝
  • immediate ##立即生效
ansible westos -m firewalld -a 'zone=public service=http permanent=yes state=enabled immediate=yes'  ##添加防火墙策略,允许http服务通过
ansible westos -m shell -a 'firewall-cmd --list-all'  ##查看策略

ansible synchronize 使用 ansibledoc_源文件_91


ansible synchronize 使用 ansibledoc_hive_92

15.user

做用:
管理远程主机上的用户

常用参数

  • name ##指定用户名称
  • group #指定用户基本组
  • groups ##指定用户附加组
  • append ##添加附加组默认值为no
  • shell ##指定用户的默认shell
  • uid ##指定用户id
  • comment ##指定用户的注释信息
  • state ##指定用户是否存在于远程主机
    present ##创建
    absent ##删除
  • remove ##删除用户时删除用户家目录,默认为no
  • password ##指定用户密码,为明文
    可用openssl passwd -6 生产加密字符串
  • generate_ssh_key ##生成sshkey

受控段:监控指令

watch -n 1 "tail -n 3 /etc/passwd /etc/group /etc/shadow;echo =====;ls -l /home"

ansible synchronize 使用 ansibledoc_hive_93

ansible westos -m user -a 'name=test shell=/bin/sh uid=6666 group=72 groups=dhcpd comment="test user" state=present'  ##添加用户,指定名称,shell,uid,组,附加组,注释信息

ansible synchronize 使用 ansibledoc_源文件_94


ansible synchronize 使用 ansibledoc_源文件_95

openssl passwd -6
ansible westos -m user -a 'name=test shell=/bin/sh uid=6666 group=72 groups=dhcpd comment="test user" password="\$6$43bX2AkUPB6lk/VQ$FPvG.O51R3iCR3EhR8xB2iKM0LkY/Drzrw1lNinO79v3JVsf.feTHpmmddd4wr/2bHWl6qIAOjLaWHarQRBaT." state=present'  ##添加密码

ansible synchronize 使用 ansibledoc_源文件_96


ansible synchronize 使用 ansibledoc_源文件_97

ansible westos -m user -a 'name=test shell=/bin/sh uid=6666 group=72 groups=dhcpd comment="test user" password="$6$43bX2AkUPB6lk/VQ$FPvG.O51R3iCR3EhR8xB2iKM0LkY/Drzrw1lNinO79v3JVsf.feTHpmmddd4wr/2bHWl6qIAOjLaWHarQRBaT." state=present generate_ssh_key=true'   ##生成sshkey

ansible synchronize 使用 ansibledoc_linux_98

受控端:

cd /home/test/.ssh/

ansible synchronize 使用 ansibledoc_hive_99

ansible westos -m user -a 'name=test shell=/bin/sh uid=6666 group=72 groups=apache append=yes comment="test user" password="$6$43bX2AkUPB6lk/VQ$FPvG.O51R3iCR3EhR8xB2iKM0LkY/Drzrw1lNinO79v3JVsf.feTHpmmddd4wr/2bHWl6qIAOjLaWHarQRBaT." state=present generate_ssh_key=true'  ##添加附加组

ansible synchronize 使用 ansibledoc_linux_100

ansible synchronize 使用 ansibledoc_linux_101


ansible synchronize 使用 ansibledoc_hive_102

ansible westos -m user -a 'name=test state=absent remove=yes'  ##删除用户同时删除家目录

16.group

作用:
管理原程主机上的组

常用参数

  • name ##指定组名称
  • state ##指定组状态
    present ##创建
    absent ##删除
  • gid ##指定组id
ansible westos -m group -a 'name=lee state=present'  ##创建组
ansible westos -m group -a 'name=lee state=absent'  ##删除组
ansible westos -m group -a 'name=lee state=present gid=2000'  ##指定id创建组

ansible synchronize 使用 ansibledoc_hive_103


ansible synchronize 使用 ansibledoc_文件权限_104


ansible synchronize 使用 ansibledoc_linux_105


ansible synchronize 使用 ansibledoc_linux_106

17.lineinfile

  • path ##指定要操作的文件
  • line ##指定文本内容
  • regexp ##使用正则表达式匹配对应的行当替换文本时
    ##如果有多行文本都被匹配,则只有最后的那行文本才会被替换,当删除文本时,如果有多行文本被匹配,则都被删除
  • state ##删除对应文本时,参数为absent,默认为present
  • backrefs ##当内容无匹配规则时,不对文本做任何更改,默认为no,向后引用regexp变量信息
  • insertafter ##可将文本插入指定的行之后,参数值可设置为EOF或正则表达式
  • insertbefore ##将文本插入指定行之前。参数为BOF或正则表达式
  • backup ##是否在修改文件前进行备份
  • create ##当要操作的文件并不存在时,是否创建对应文件
vim /mnt/westos
hello westos
hello test 
hello linux

test:

ansible westos -m lineinfile -a 'path=/mnt/westos line="hello test" create=yes'  ##在/mnt/westos中创建行hello test
ansible westos -m shell -a "cat /mnt/westos"  ##查看

ansible synchronize 使用 ansibledoc_linux_107


ansible synchronize 使用 ansibledoc_源文件_108

ansible westos -m lineinfile -a 'path=/mnt/westos line="hello test2" regexp="test" '  ##将其中test修改为hello test2
ansible westos -m shell -a "cat /mnt/westos"

ansible synchronize 使用 ansibledoc_文件权限_109


ansible synchronize 使用 ansibledoc_源文件_110

ansible westos -m lineinfile -a 'path=/mnt/westos state=absent regexp="test2" '  ##删除含有test2的行
ansible westos -m shell -a "cat /mnt/westos"

ansible synchronize 使用 ansibledoc_linux_111

ansible westos -m lineinfile -a 'path=/mnt/westos regexp="hello lee" line="westos test"' ##将hello lee 的行修改为westos test
ansible westos -m shell -a "cat /mnt/westos"  ##只有最后一行被修改
ansible westos -m lineinfile -a 'path=/mnt/westos regexp="hello lee" line="westos test" backrefs=yes'  ##修改向后引用,均被修改,且若无匹配时,不对文本做任何更改
ansible westos -m shell -a "cat /mnt/westos"
ansible westos -m lineinfile -a 'path=/mnt/westos line="##### westos end #####" insertafter=EOF'  ##最后一行后添加内容
ansible westos -m shell -a "cat /mnt/westos"
ansible westos -m lineinfile -a 'path=/mnt/westos line="##### westos begin #####" insertafter=BOF' ##第一行后添加内容
ansible westos -m shell -a "cat /mnt/westos"
ansible westos -m lineinfile -a 'path=/mnt/westos regexp="(w.{5}).*({t.{3})" line="\1" '
 ##搜索到行以W开头后有五个字母,或以T开头后三个字符的行,在最后一行后添加\1
ansible westos -m shell -a "cat /mnt/westos"

ansible synchronize 使用 ansibledoc_源文件_112

ansible westos -m lineinfile -a 'path=/mnt/westos line="##### westos end again #####" insertafter=EOF backup=yes'  ##修改前备份
ansible westos -m shell -a "ls /mnt/"

ansible synchronize 使用 ansibledoc_文件权限_113


ansible synchronize 使用 ansibledoc_文件权限_114

18.replace

作用:
根据正则表达式字串,将文件爱你中所有被匹配的字串替换

常用参数:
path ##指定要操作的文件
regexp #3指定一个正则表达式
##文件终于正则表达式匹配的字符均会被替换
replace ##指定最终要替换成的字符
backup ##是否再修改前对源文件进行备份

ansible westos -m replace -a 'path=/mnt/westos regexp="hello" replace="westos" backup=yes'  ##对字符串hello进行替换,替换成westos,并进行备份
ansible westos -m shell -a "cat /mnt/westos"  ##查看文件
ansible westos -m shell -a "ls /mnt/"  ##查看是否备份

ansible synchronize 使用 ansibledoc_文件权限_115

19.setup

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

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

ansible westos -m setup | less  ##查看远程主机信息
ansible westos -m setup -a 'filter="ansible_all_ipv4_addresses"'  ##过滤受控主机ip进行查看

ansible synchronize 使用 ansibledoc_文件权限_116


ansible synchronize 使用 ansibledoc_hive_117

20.debug

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

  • msg ##调试输出的消息
  • var ##将某个任务执行的输出作为变量传递给debug模块
  • verbosity ##debug的级别(默认0级,全部显示)
ansible westos -m debug -a 'msg="hello world"' ##在远程主机输出hello world

ansible synchronize 使用 ansibledoc_源文件_118

ansible westos -m debug -a 'msg="hello world" verbosity=0'  ##debug级别0
ansible westos -m debug -a 'msg="hello world" verbosity=1'  ##debug级别1

ansible synchronize 使用 ansibledoc_hive_119

vim westos.yml
---
- name: test
  hosts: westos
  tasks:
    - debug:
        var: ansible_all_ipv4_addresses
ansible-playbook westos.yml  ##执行查看输出ip地址

ansible synchronize 使用 ansibledoc_hive_120


ansible synchronize 使用 ansibledoc_linux_121

vim westos.yml
---
- name: test
  hosts: westos
  tasks:
    - debug:
        var: ansible_all_ipv4_addres
ansible-playbook westos.yml  ##输出错误
vim westos.yml
---
- name: test
  hosts: westos
  tasks:
    - debug:
        msg: ansible_all_ipv4_addres
ansible-playbook westos.yml  ##输出msg信息

ansible synchronize 使用 ansibledoc_linux_122


ansible synchronize 使用 ansibledoc_源文件_123