Ansible批量管理服务

01. ansible批量化管理服务概述
    01. 基于python语言开发的自动化软件工具
    02. 基于ssh远程管理服务实现远程主机批量管理
    
02. ansible批量化管理服务意义
    01. 提高工作效率
    02. 提高工作准确度
    03. 减少维护的成本
    04. 减少重复性工作
    
03. ansible批量化管理服务功能
    01. 可以实现批量系统操作配置
    02. 可以实现批量软件服务部署
    03. 可以实现批量文件数据分发
    04. 可以实现批量系统信息收集
    
04. ansible批量化管理服务特点
    01. 管理端不需要启动服务程序(no server)
    02. 管理端不需要编写配置文件(/etc/ansible/ansible.conf)
    03. 受控端不需要安装软件程序(libselinux-python)
        被管理端selinux服务没有关闭    -- 影响ansible软件管理
        libselinux-python让selinux开启状态也能使用ansible程序
    04. 受控端不需要启动服务程序(no agent)
    05. 服务管理程序操作模块众多(modlule)
    06. 利用剧本编写实现自动化(playbook)
    
补充说明:远程主机无法管理问题分析
    1)管理端没有分发好公钥
    2)被管理端远程服务出现问题
    3)被管理端进程出现僵死状况,杀死管理端root@notty的进程
root       1455        /usr/sbin/sshd -D    -- 负责建立远程连接
root       1961        sshd: root@pts/0     -- 用于维护远程连接(windows--linux)
root       5176        sshd: root@notty     -- 用于维护远程连接(ansible--被管理端
        
05. ansible批量化管理服务部署  
第一步骤:安装部署软件
yum -y install ansible          -- 需要依赖epel的yum源
/etc/ansible/ansible.conf       -- ansible服务配置文件
/etc/ansible/hosts              -- ansible主机清单文件,定义可以管理的主机信息
/etc/ansible/roles              -- ansible角色目录
 
第二步骤:需要编写主机清单文件
rpm -ql ansible|grep -v "^/usr/(share|lib)"      -- 查看安装文件路径
    
vim /etc/ansible/hosts
#定义可以管理的主机清单
10.10.10.11
10.10.10.12
 
第三步骤:测试前准备 -- 公钥分发
ssh-keygen -t rsa                    -- 生成密钥对
ssh-copy-id [email protected]        -- 将密钥传到服务器端
    
第四步骤:测试是否能够管理多个主机
[root@ZHManager ~]# ansible all -a "hostname"
10.10.10.12 | SUCCESS | rc=0 >>
LVS2.localdomain
10.10.10.11 | SUCCESS | rc=0 >>
LVS1.localdomain
 
06. ansible服务架构
    01.主机清单配置
    02. 软件模块信息
    03. 基于密钥连接主机
    04. 主机需要关闭selinux
    05. 软件剧本功能
 
ansible软件模块应用
官方网站:https://docs.ansible.com/
    
ansible服务管理模块
    command    (默认模块)
    shell    (万能模块)
    script    (脚本模块)
    copy    (批量分发文件)
    fetch    (批量拉取文件)    从被管理端拉取到管理端
ansible 10.10.10.11 -m fetch -a "src=/tmp/123.txt dest=/test"
    file    ()
 
补充说明:ansible学习帮助手册如何查看
        ansible-doc -l                  -- 列出模块使用简介
        ansible-doc -s copy        -- 列出指定模块详细说明
        ansible-doc fetch            -- 查询模块在剧本中的应用方法
    
命令类型模块:    
第一个模块:command(默认模块)
    模块应用的语法格式:
    ansible 主机名称/主机组名称/主机地址信息发/all  -m(指定应用的模块信息) 模块名称 -a(指定动作信息) 执行什么动作
ansible 10.10.10.11 -m command -a hostname
[root@ZHManager ~]# ansible 10.10.10.11 -m command -a hostname
10.10.10.11 | SUCCESS | rc=0 >>
LVS1.localdomain
 
扩展应用:
1)chdir  Change into this directory before running the command.
    在执行命令之前切换目录
[root@ZHManager ~]# ansible 10.10.10.11 -m command -a "chdir=/root ls -a"
10.10.10.11 | SUCCESS | rc=0 >>
anaconda-ks.cfg
.ansible
 
2)creates     If it already exists, this step won't be run.
    如果文件已经存在,不执行命令操作
[root@ZHManager ~]# ansible 10.10.10.11 -m command -a "creates=/test/cy.txt chdir=/test touch y.txt"
10.10.10.11 | SUCCESS | rc=0 >>
skipped, since /test/cy.txt exists
 
3)removes     If it already exists, this step will be run.
     如果文件已经存在,则执行命令操作
[root@ZHManager ~]# ansible 10.10.10.11 -m command -a "removes=/test/cy.txt chdir=/test touch y.txt"
10.10.10.11 | SUCCESS | rc=0 >>
[root@LVS1 test]# ls
cy.txt  y.txt
 
4)free_form        The command module takes a free form command to run.
     使用command模块时,-a后面必须写上一个合法linux命令信息
    
    注意事项:
    有些符号信息无法识别:"<"         ">"         "|"        ";"         "&"
 
第二个模块:shell(万能模块)
    模块应用的语法格式:
ansible 主机名称/主机组名称/主机地址信息发/all  -m(指定应用的模块信息) 模块名称 -a(指定动作信息) 执行什么动作
ansible 10.10.10.11 -m shell -a hostname
[root@ZHManager ~]# ansible 10.10.10.11 -m shell -a hostname
10.10.10.11 | SUCCESS | rc=0 >>
LVS1.localdomain
 
扩展应用:
1)chdir  Change into this directory before running the command.
     在执行命令之前切换目录
[root@ZHManager ~]# ansible 10.10.10.11 -m shell -a "chdir=/root ls -a"
10.10.10.11 | SUCCESS | rc=0 >>
anaconda-ks.cfg
.ansible
 
2)creates     If it already exists, this step won't be run.
     如果文件已经存在,不执行命令操作
[root@ZHManager ~]# ansible 10.10.10.11 -m shell -a "creates=/test/cy.txt chdir=/test touch y.txt"
10.10.10.11 | SUCCESS | rc=0 >>
skipped, since /test/cy.txt exists
 
3)removes     If it already exists, this step will be run.
     如果文件已经存在,则执行命令操作
[root@ZHManager ~]# ansible 10.10.10.11 -m shell -a "removes=/test/cy.txt chdir=/test touch y.txt"
10.10.10.11 | SUCCESS | rc=0 >>
[root@LVS1 test]# ls
cy.txt  y.txt
 
4)free_form        The command module takes a free form command to run.
使用command模块时,-a后面必须写上一个合法linux命令信息
    
实践应用:利用shell执行脚本 
一、编写一个脚本
二、将脚本发送到远程主机
三、将脚本权限进行修改(添加执行权限)
四、运行ansible命令执行脚本
[root@ZHManager ~]# ansible 10.10.10.11 -m shell -a "/usr/yum.sh"
 
第三个模块:script(万能模块) 主要用于执行脚本
    一、编写一个脚本
    二、运行ansible命令执行脚本
    
    ps: script模块参数功能和command模块类似
  
  文件类型模块
  1)copy – Copy files to remote locations
        将数据信息进行批量分发
        
基本用法:
ansible 10.10.10.11 -m copy -a "src=/etc/hosts dest=/test/"
[root@ZHManager ~]# ansible 10.10.10.11 -m copy -a "src=/etc/hosts dest=/test/"
10.10.10.11 | SUCCESS => {            -- 对哪台主机进行操作
    "changed": true,                  -- 是否对主机信息进行改变
    "checksum": "7335999eb54c15c67566186bdfc46f64e0d5a1aa",     -- 生成一个文件校验码==MD5数值
    "dest": "/test/hosts",            -- 显示目标路径     
    "gid": 0,                         -- 显示复制后文件gid信息
    "group": "root",                 -- 显示复制后文件属主信息
    "md5sum": "54fb6627dbaa37721048e4549db3224d",     -- 生成一个文件校验码==MD5数值
    "mode": "0644",                 -- 显示复制后文件权限信息
    "owner": "root",                 -- 显示复制后文件属主信息
    "size": 158,                     -- 显示文件的大小信息
    "src": "/root/.ansible/tmp/ansible-tmp-1582605727.51-233083028119662/source",
    "state": "file",                 -- 显示文件的类型信息
    "uid": 0                        -- 显示复制后文件uid信息
}
    补充说明:ansible软件输出颜色说明
    01. 绿色信息:查看主机信息/对主机未做改动
    02. 黄色信息:对主机数据信息做了修改
    03. 红色信息:命令执行出错了
    04. 粉色信息:忠告信息
    05. 蓝色信息:显示ansible命令执行的过程
[root@LVS1 test]# ls
cy.txt  hosts  y.txt
 
 扩展应用:
 01. 在传输文件时修改文件的属主和属组信息(owner    group)
[root@ZHManager ~]# ansible 10.10.10.11 -m copy -a "src=/etc/hosts dest=/test owner=chenyun group=chenyun"
10.10.10.11 | SUCCESS => {
    "changed": true,
    "checksum": "7335999eb54c15c67566186bdfc46f64e0d5a1aa",
    "gid": 1000,
    "group": "chenyun",
    "mode": "0644",
    "owner": "chenyun",
    "path": "/test/hosts",
    "size": 158,
    "state": "file",
    "uid": 1000
}
    
02. 在传输文件时修改文件的权限信息(mode)        
[root@ZHManager ~]# ansible 10.10.10.11 -m copy -a "src=/etc/hosts dest=/test mode=666"
10.10.10.11 | SUCCESS => {
    "changed": true,
    "checksum": "7335999eb54c15c67566186bdfc46f64e0d5a1aa",
    "gid": 1000,
    "group": "chenyun",
    "mode": "0666",
    "owner": "chenyun",
    "path": "/test/hosts",
    "size": 158,
    "state": "file",
    "uid": 1000
}
 
03. 在传输数据文件信息时对远程主机源文件进行备份(backup)
[root@ZHManager ~]# ansible 10.10.10.11 -m copy -a "src=/etc/hosts dest=/test backup=yes"
10.10.10.11 | SUCCESS => {
    "backup_file": "/test/hosts.3020.2020-02-25@13:48:46~",
    "changed": true,
    "checksum": "37ff7ddf80c60973ffd3b65aaf5e1990c5d5455a",
    "dest": "/test/hosts",
    "gid": 1000,
    "group": "chenyun",
    "md5sum": "775247b1ec87e2ee00f04a9d8c61e302",
    "mode": "0666",
    "owner": "chenyun",
    "size": 180,
    "src": "/root/.ansible/tmp/ansible-tmp-1582609715.14-17647142721932/source",
    "state": "file",
    "uid": 1000
}
[root@LVS1 test]# ll
total 8
-rw-rw-rw- 1 chenyun chenyun 180 Feb 25 13:48 hosts
-rw-rw-rw- 1 chenyun chenyun 158 Feb 25 12:42 hosts.3020.2020-02-25@13:48:46~
 
04. 创建一个文件并直接编辑文件信息(content)
[root@ZHManager ~]# ansible 10.10.10.11 -m copy -a "content='cy123' dest=/test/chenyun"
10.10.10.11 | SUCCESS => {
    "changed": true,
    "checksum": "231f9492b9566c9bbf6e59ccefa3a22df9352cf0",
    "dest": "/test/chenyun",
    "gid": 0,
    "group": "root",
    "md5sum": "4bcbe7892d76569a894260d16728f62f",
    "mode": "0644",
    "owner": "root",
    "size": 5,
    "src": "/root/.ansible/tmp/ansible-tmp-1582610294.53-1387491334145/source",
    "state": "file",
    "uid": 0
}
[root@LVS1 test]# ls
chenyun  hosts  hosts.3020.2020-02-25@13:48:46~
[root@LVS1 test]# vim chenyun
cy123
自行研究:remote_src    directory_mode        local_follow
ps:ansible软件copy模块复制目录信息
    
[root@ZHManager chenyun]# touch chen01{01..03}.txt
[root@ZHManager chenyun]# ls
chen0101.txt  chen0102.txt  chen0103.txt
    
[root@ZHManager ~]# ansible 10.10.10.11 -m copy -a "src=/test/chenyun dest=/test"
[root@LVS1 test]# tree
.
└── chenyun
    ├── chen0101.txt
    ├── chen0102.txt
    └── chen0103.txt
[root@ZHManager ~]# ansible 10.10.10.11 -m copy -a "src=/test/chenyun/ dest=/test"
    
[root@LVS1 test]# tree
.
├── chen0101.txt
├── chen0102.txt
└── chen0103.txt
    ansible 10.10.10.11 -m copy -a "src=/test/chenyun dest=/test"
    src后面目录没有/:将目录本身以及目录下面的内容都进行远程传输复制
    
    ansible 10.10.10.11 -m copy -a "src=/test/chenyun/ dest=/test"
    src后面目录有/:只将目录下面的内容都进行远程传输复制
    
  2)file – Manage files and file properties
        管理文件属性信息
    
 基本用法:
ansible 10.10.10.11 -m file -a "dest=/test/hosts owner=chenyun group=chenyun mode=666"
[root@LVS1 test]# ll
total 4
-rw-rw-rw-. 1 chenyun chenyun 158 Jun  7  2013 hosts
 
扩展用法:
1. 可以利用模块创建数据信息(文件    目录    链接文件)
    state 参数
        =absent            -- 删除数据信息
        =directory        -- 创建一个目录信息
        =file                  -- 检测创建的信息是否存在(绿色存在    红色不存在)
        =hard                -- 创建一个硬链接文件
        =link                  -- 创建一个软链接文件
        =touch              -- 创建一个文件信息
    
    创建目录信息:(state=directory)
[root@ZHManager ~]# ansible 10.10.10.11 -m file -a "dest=/test/cy/cy1/cy2 state=directory"
[root@LVS1 test]# tree
.
└── cy
    └── cy1
        └── cy2
 
    创建文件信息:(state=touch)
[root@ZHManager ~]# ansible 10.10.10.11 -m file -a "dest=/test/chenyun.txt state=touch"
[root@LVS1 test]# ls
chenyun.txt  cy
 
创建链接文件信息:
1.创建硬链接:inode相同为硬链接
[root@ZHManager ~]# ansible 10.10.10.11 -m file -a "src=/test/chenyun.txt dest=/test/chenyun_hard.txt state=hard"
[root@LVS1 test]# ll -i
total 0
51633040 -rw-r--r-- 2 root root  0 Feb 25 15:18 chenyun_hard.txt
51633040 -rw-r--r-- 2 root root  0 Feb 25 15:18 chenyun.txt
 
2.创建软链接
[root@ZHManager ~]# ansible 10.10.10.11 -m file -a "src=/test/chenyun.txt dest=/test/chenyun_link.txt state=link"
[root@LVS1 test]# ll -i
total 0
51638107 lrwxrwxrwx 1 root root 17 Feb 25 15:28 chenyun_link.txt -> /test/chenyun.txt
51633040 -rw-r--r-- 1 root root  0 Feb 25 15:18 chenyun.txt
   
删除文件信息:
1.删除文件
[root@ZHManager ~]# ansible 10.10.10.11 -m file -a "dest=/test/chenyun.txt state=absent"
[root@LVS1 test]# ll
total 0
lrwxrwxrwx 1 root root 17 Feb 25 15:28 chenyun_link.txt -> /test/chenyun.txt
 
2.删除目录
[root@ZHManager ~]# ansible 10.10.10.11 -m file -a "dest=/test/cy state=absent"
自行研究:recurse (递归赋权)
 
Ansible模块说明
命令行模块:
1)yum模块
    yum – Manages packages with the yum package manager
        name        -- 指定安装软件名称
        state         -- 指定是否安装软件
        installed    -- 安装软件
        latest
        present
        absent        -- 卸载软件
        removed
[root@ZHManager ~]# ansible 10.10.10.11 -m yum -a "name=iotop state=installed"
[root@LVS1 test]# rpm -qa iotop
iotop-0.6-4.el7.noarch
[root@ZHManager ~]# ansible 10.10.10.11 -m yum -a "name=iotop state=absent"
 
2)service模块   管理服务器的运行状态    停止 启动    重启
    service – Manage services
        name              -- 指定管理服务名称
        state               -- 指定服务状态
        reloaded        -- 重新加载
        restarted        -- 重启服务
        started           -- 启动服务
        stopped         -- 停止服务
        enabled         -- 指定服务是否开机自启
[root@ZHManager ~]# ansible 10.10.10.11 -m service -a "name=httpd state=started enabled=yes"
[root@ZHManager ~]# ansible 10.10.10.11 -m service -a "name=httpd state=stopped"
 
3)cron模块        批量设置多个主机的定时任务
cron – Manage cron.d and crontab entries
crontab -e(单台设置)
 
*    *    *    *    *    定时任务动作
分   时   日   月   周
     
minute:     # Minute when the job should run ( 0-59, *, */2, etc )
            设置分钟信息
hour:       # Hour when the job should run ( 0-23, *, */2, etc )
            设置小时信息
day:        # Day of the month the job should run ( 1-31, *, */2, etc )
            设置日期信息
month:      # Month of the year the job should run ( 1-12, *, */2, etc )
            设置月份信息
weekday:    # Day of the week that the job should run ( 0-6 for Sunday-Saturday, *, etc )
            设置周信息
                        
job         用于定义定时任务需要干的事情
name        设置定时任务的注释信息
state       指定是否删除定时任务,absent删除    present默认
disabled    是否注释定时任务,yes注释掉  no取消注释
    
基本用法:
[root@ZHManager ~]# ansible 10.10.10.11 -m cron -a "minute=0 hour=2 job='/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1'"
[root@LVS1 ~]# crontab -l
#Ansible: None
0 2 * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1
 
扩展用法:
1、给定时任务设置注释信息(name)
[root@ZHManager ~]# ansible 10.10.10.11 -m cron -a "name='set time' minute=0 hour=2 job='/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1'"
[root@LVS1 ~]# crontab -l
#Ansible: set time
0 2 * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1
 
2、如何删除指定定时任务
[root@ZHManager ~]# ansible 10.10.10.11 -m cron -a "name='set time' state=absent"
 
 注:ansible可以删除定时任务,但只删除ansible设置的定时任务
    
3、如何注释定时任务(disabled)
[root@ZHManager ~]# ansible 10.10.10.11 -m cron -a "name='set time' minute=0 hour=2 job='/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1' disabled=yes"
[root@LVS1 ~]# crontab -l
#Ansible: set time
#0 2 * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1
 
[root@ZHManager ~]# ansible 10.10.10.11 -m cron -a "name='set time' minute=0 hour=2 job='/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1' disabled=no"
[root@LVS1 ~]# crontab -l
#Ansible: set time
0 2 * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1
 
4)mount模块    批量进行挂载操作
        src                    -- 指定需要挂载的存储设备或文件信息
        path                 -- 指定目标挂载点目录
        fstype               -- 指定挂载时的文件系统类型
        state                 --
        present/mounted        -- 进行挂载
        present:   -- 不会立即挂载,修改fstab文件,实际开机自动挂载
        mounted:    -- 立即挂载,并且会修改fstab文件,开机自动进行挂载
        absent/unmounted    -- 进行卸载
        absent:         -- 会实现立即卸载,并且删除fstab文件里的信息,禁止自动挂载
        unmounted:  -- 会立即卸载,但不会删除fstab文件里的信息,重启后还会自动挂载
    
1、如何实现网盘挂载,开机自动进行挂载
[root@ZHManager ~]# ansible 10.10.10.11 -m mount -a "src=10.10.10.22:/mnt/data path=/mnt/nfs fstype=nfs state=mounted"
[root@LVS1 ~]# df -h
Filesystem             Size  Used Avail Use% Mounted on
/dev/sda3               18G  1.8G   16G  10% /
/dev/sda1              297M  125M  173M  42% /boot
10.10.10.22:/mnt/data   18G  1.9G   16G  11% /mnt/nfs
[root@LVS1 ~]# cat /etc/fstab
UUID=7144da8a-89e3-4a00-a9a8-d99efb36629d /                       xfs     defaults        0 0
UUID=1fa42406-4c1f-44c4-b05e-f94dd7ac57fc /boot                   xfs     defaults        0 0
10.10.10.22:/mnt/data                     /mnt/nfs                nfs     defaults        0 0
    
2、如何实现网盘卸载
[root@ZHManager ~]# ansible 10.10.10.11 -m mount -a "src=10.10.10.22:/mnt/data path=/mnt/nfs fstype=nfs state=absent"
 
    5)user模块        实现批量创建用户
    基本用法:
    ansible 10.10.10.11 -m user -a "name=chenxuan"
    
扩展用法:
1、指定用户UID信息和用户组信息
[root@ZHManager ~]# ansible 10.10.10.11 -m user -a "user=test uid=668 group=chenyun"
[root@LVS1 ~]# id test
uid=668(test) gid=1000(chenyun) groups=1000(chenyun)
[root@ZHManager ~]# ansible 10.10.10.11 -m user -a "user=test01 uid=669 groups=chenyun"
[root@LVS1 ~]# id test01
uid=669(test01) gid=1002(test01) groups=1002(test01),1000(chenyun)
 
2、批量创建虚拟用户(不创建家目录、不允许用户登录,相当于useradd rsync -M -s /sbin/nologin)
[root@ZHManager ~]# ansible 10.10.10.11 -m user -a "user=rsync create_home=no shell=/sbin/nologin"
[root@LVS1 ~]# cat /etc/passwd | grep rsync
rsync:x:1003:1004::/home/rsync:/sbin/nologin
[root@LVS1 ~]# ll /home/rsync -d
ls: cannot access /home/rsync: No such file or directory
   
 3、给指定用户创建密码
[root@ZHManager ~]# ansible 10.10.10.11 -m user -a 'user=test05 password=$6$rounds=656000$test$58yqsHiYk6d24CaZk53e6wvTsFgLtYg3qlQOUOKLsxropECbzRAzTCZrbmQQxiU9tJ8OMVg2SIR/tO..YT75Z1'
 
 生成密文密码的方法
    方法一:
[root@ZHManager ~]# ansible all -i localhost, -m debug -a "msg={{ '123456' | password_hash('sha512', 'test') }}"
    LVS1 | SUCCESS => {
        "changed": false,
        "msg": "$6$rounds=656000$test$58yqsHiYk6d24CaZk53e6wvTsFgLtYg3qlQOUOKLsxropECbzRAzTCZrbmQQxiU9tJ8OMVg2SIR/tO..YT75Z1"
    }
 
    方法二:(忽略)
    mkpasswd --method=sha-512
    
    方法三:
[root@ZHManager ~]# yum -y install python-pip
[root@ZHManager ~]# pip install passlib
[root@ZHManager ~]# python -c "from passlib.hash import sha512_crypt; import getpass; print(sha512_crypt.using(rounds=5000).hash(getpass.getpass()))"
 
  剧本的编写方法:
    剧本的作用:
    自动化部署rsync服务:
    服务端操作:
第一个历程:安装软件
ansible 10.10.10.11 -m yum -a "name=rsync state=installed"
    
第二个历程:编写文件
ansible 10.10.10.11 -m copy -a "src=/etc/rsyncd.conf dest=/etc/"
    
第三个历程:创建用户
ansible 10.10.10.11 -m user -a "user=rsync create_home=no shell=/sbin/nologin"
    
第四个历程:创建目录
ansible 10.10.10.11 -m file -a "dest=rsync state=directory owner=rsync group=rsync"
    
第五个历程:创建密码文件
ansible 10.10.10.11 -m copy -a "content='rsync_backup:mima' dest=/etc/rsync.password mode=600"
    
第六个历程:启动服务
ansible 10.10.10.11 -m service -a "name=rsyncd state=started enabled=yes"
    
客户端操作:
第一个历程:创建密码文件
ansible 客户端地址 -m copy -a "content='rsync_backup:mima' dest=/etc/rsync.password mode=600"
 
剧本编写规范:pyyaml    -- 三点要求
1. 合理的信息缩进
    标题一
        标题二
            标题三
注:在ansible中一定不能用tab进行缩进
2. 冒号的使用方法
hosts:10.10.10.11
tasks:
yum:name=httpd
 
注:使用冒号时后面要有空格
    以冒号结尾,冒号信息出现在注释说明中,后面不需要加上空格
 
3. 短横线应用 - (列表功能)
- hosts: 10.10.10.11
  tasks:
   - yum: name=rsync state=installed
   - yum: name=iotop state=installed
   - copy: src=/root/yum.sh dest=/tmp/
 
  - hosts: 10.10.10.12
  tasks:
   - yum: name=rsync state=installed
    注:使用短横线构成列表信息,短横线后面需要有空格
    
开始编写剧本:
说明:剧本文件扩展名尽量写为yaml
   1. 方便识别文件是一个剧本文件
   2. 文件编写时会有颜色提示
[root@ZHManager ~]# mkdir -p /etc/ansible/ansible-playbook
[root@ZHManager ~]# cd /etc/ansible/ansible-playbook/
[root@ZHManager ansible-playbook]# vim rsync-service.yaml
- hosts: 10.10.10.11
  tasks:
   - yum: name=rsync state=installed
   - yum: name=iotop state=installed
   - copy: src=/root/yum.sh dest=/tmp/
 
如何执行脚本:
第一个步骤:检查剧本语法格式
ansible-playbook --syntax-check rsync-service.yaml
 
第二个步骤:模拟执行剧本
ansible-playbook -C rsync-service.yaml
 
第三个步骤:直接执行脚本
ansible-playbook  rsync-service.yaml

猜你喜欢

转载自www.cnblogs.com/chenyun1/p/12609775.html