1.ansible批量管理服务概念

(1)是基于Python语言开发的自动化软件工具

(2)是基于SSH远程管理服务实现远程主机批量管理

2.ansible批量管理服务意义

(1)提高工作的效率

(2)提高工作准确度

(3)减少维护的成本

(4)减少重复性工作

3.ansible批量管理服务功能

(1)可以实现批量系统操作配置

(2)可以实现批量软件的部署

(3)可以实现批量文件数据分发

(4)可以实现批量系统信息收集

4.ansible批量管理的特点

  1. 管理端不需要启动服务程序(no server)
  2. 管理端不需要编写配置文件(/etc/ansible/ansible.cfg)
  3. 受控端不需要安装软件程序(libselinux-python)
    • 被管理端selinux服务没有关闭 — 影响ansible软件的管理
    • libselinux-python让selinux开启的状态也可以使用ansible程序
  4. 受控端不需要启动服务程序(no agent)
  5. 服务程序管理操作模块众多(module)
  6. 利用剧本编写来实现自动化(playbook)

5.ansible批量管理服务部署

管理端服务器
(1)安装部署软件
  1. [root@m01 ~]# yum install -y ansible --------需要配置epel

  2. /etc/ansible -----主程序目录
  3. /etc/ansible/ansible.cfg -----配置文件
  4. /etc/ansible/hosts ----- 主机清单配置文件
  5. /etc/ansible/roles ----- 角色目录
(2)编写主机清单文件(/etc/ansible/hosts)

a.根据主机进行编写

  1. [root@m01 ~]# vim /etc/ansible/hosts
  2. 172.16.1.61
  3. 172.16.1.7

b.主机分组编写

  1. [shuai]
  2. 172.16.1.41
  3. 172.16.1.31
  4. 172.16.1.7

c.主机名符号匹配编写

  1. [shuai]
  2. 172.16.1.[41:43] #代表172.16.1.42-43
  3. web[01:03] #代表web01-03,要做hosts做主机名解析

d.跟上非标准远程端口

  1. [shuai]
  2. 172.16.1.41:52113 #52113代表ssh远程管理端口

f.主机使用特殊的变量

  1. [shuai]
  2. 172.16.1.7 ansible_ssh_port=52113 ansible_ssh_user=root ansible_ssh_pass=123456
  3. [shuai]
  4. web01 ansible_ssh_host=172.16.1.7 ansible_ssh_port=52113 ansible_ssh_user=root ansible_ssh_pass=123456

g.主机组嵌入式配置

  1. #########################################################
  2. [rsync:children] --- 嵌入子组信息 #
  3. rsync_server #
  4. rsync_client #
  5. #
  6. [rsync_server] #
  7. 172.16.1.41 #
  8. #
  9. [rsync_client] #
  10. 172.16.1.31 #
  11. 172.16.1.7 #
  12. ###########################################################
  13. [shuai:vars] --- shuai组进行嵌入式变量 #
  14. ansible_ssh_host=172.16.1.7 #
  15. ansible_ssh_port=52113 #
  16. ansible_ssh_user=root #
  17. ansible_ssh_pass=123456 #
  18. [shuai] #
  19. web01 #
  20. ###########################################################

(3)测试是否可以管理主机

  1. [root@m01 ~]# ansible all -a "hostname"
  2. 172.16.1.31 | CHANGED | rc=0 >>
  3. nfs01
  4. 172.16.1.41 | CHANGED | rc=0 >>
  5. backup
  6. 172.16.1.7 | CHANGED | rc=0 >>
  7. web01
(4)ansible服务架构介绍

a.主机清单信息

b.软件模块信息

c.基于秘钥连接主机

d.软件剧本功能playbook

(5)ansible软件模块应用

模块应用的语法格式

  1. ansible 主机名称|主机组|主机地址信息|all -m(指定应用的模块信息) 模块名称 -a(指定动作信息) "动作执行"

  2. 例如:
  3. [root@m01 ~]# ansible all -m command -a "hostname"

6.ansible常用模块介绍

6.1 command模块

a.chdirChange into this directory before running the command #在命令执行之前切换目录

  1. [root@m01 ~]# ansible shuai -m command -a "chdir=/tmp touch shuai.txt"

b.creates If it already exists, this step won’t be run. #如果它已经存在,则不会运行此步骤 ,做判断

  1. [root@m01 ~]# ansible 172.16.1.7 -m command -a "creates=/tmp/shuai.txt chdir=/tmp touch shuai.txt"

  2. #如果172.16.1.7远程主机的/tmp目录没有shuai.txt文件则在/tmp文件下创建shuai.txt。如果有则跳过

c.removes If it already exists, this step will be run. #如果文件存在,这个步骤将执行

  1. [root@m01 ~]# ansible 172.16.1.7 -m command -a "removes=/tmp/shuai.txt chdir=/tmp touch shuai.txt"

command模块注意事项:

1.有些特殊符号无法识别: "<"">""|"";" "&"

2.命令模块后使用空格分隔的参数。

3.命令将在所有选定的节点上执行

6.2 shell模块(万能模块)

a.chdirChange into this directory before running the command #在命令执行之前切换目录

  1. [root@m01 ~]# ansible 172.16.1.7 -m shell -a "chdir=/tmp touch shuai01.txt"

b.creates If it already exists, this step won’t be run. #如果它已经存在,则不会运行此步骤 ,做判断

  1. [root@m01 ~]# ansible 172.16.1.7 -m shell -a "creates=/tmp/shuai01.txt chdir=/tmp touch shuai02.txt"

c.removes If it already exists, this step will be run. #如果文件存在,这个步骤将执行

  1. c.removes If it already exists, this step will be run. #如果文件存在,这个步骤将执行

6.3script脚本模块

a.chdirChange into this directory before running the command #在命令执行之前切换目录

b.creates If it already exists, this step won’t be run. #如果它已经存在,则不会运行此步骤 ,做判断

c.removes If it already exists, this step will be run. #如果文件存在,这个步骤将执行脚本

  1. [root@m01 /scripts]# ansible 172.16.1.7 -m script -a "/scripts/yum.sh"
6.3 copy模块 Copy files to remote locations拷贝文件到远程主机
  1. [root@m01 ~]# ansible 172.16.1.7 -m copy -a "src=/scripts/yum.sh dest=/tmp/"
  2. 172.16.1.7 | CHANGED => { ------->在那台主机上操作
  3. "ansible_facts": {
  4. "discovered_interpreter_python": "/usr/bin/python"
  5. },
  6. "changed": true, ------->是否做了改变(true|false
  7. "checksum": "de2b2980db930dc11f23774899494c89a8bc4206", ------->生成一个校验码
  8. "dest": "/tmp/yum.sh", ------->显示目标路径信息
  9. "gid": 0, ------->显示文件的gid信息
  10. "group": "root", ------->显示文件的用户组信息
  11. "md5sum": "840212375ea9201ff1aaed70575f1fd5", ------->生成一个校验码
  12. "mode": "0644", ------->显示复制后权限
  13. "owner": "root", ------->显示复制后拥有者
  14. "size": 33, ------->显示文件的大小
  15. "src": "/root/.ansible/tmp/ansible-tmp-1597911358.94-17987-220899881277199/source",
  16. "state": "file", ------->显示文件的类型信息
  17. "uid": 0 ------->显示文件的uid信息
  18. }

ownerName of the user that should own the file/directory, as would be fed to chown. #在传输文件是改变文件的属主

  1. [root@m01 ~]# ll 1000
  2. -rw-------. 1 root root 884 May 22 2019 1000
  3. [root@m01 ~]# ansible a -m copy -a "src=/root/1000 dest=/tmp/ owner=shuai"
  4. [root@web01 /tmp]# ll /tmp/1000
  5. -rw-r--r-- 1 shuai root 884 Aug 20 16:28 /tmp/1000

groupName of the group that should own the file/directory, as would be fed to chown. #在传输文件是改变文件的属组

  1. [root@m01 ~]# ll 1000
  2. -rw-------. 1 root root 884 May 22 2019 1000
  3. [root@m01 ~]# ansible a -m copy -a "src=/root/1000 dest=/tmp/ group=shuai"
  4. [root@web01 /tmp]# ll /tmp/1000
  5. -rw-r--r-- 1 shuai shuai 884 Aug 20 16:28 /tmp/1000

modeThe permissions of the destination file or directory. #在传输文件是改变文件的权限

  1. [root@m01 ~]# ansible a -m copy -a "src=/root/1000 dest=/tmp/1000 mode=755"

backupCreate a backup file including the timestamp information so you can get the original file back if you somehow clobbered it incorrectly #创建一个包含时间戳信息的备份文件,这样,如果不正确地删除了原始文件,就可以将其取回。 (默认为no)

  1. [root@m01 ~]# ansible a -m copy -a "src=/root/1000 dest=/tmp/ backup=yes"

content #在远程主机生成文件

  1. [root@m01 ~]# ansible a -m copy -a "content=你真帅! dest=/tmp/shuai0 2.txt"
  2. [root@web01 ~]# cat /tmp/shuai02.txt
  3. 你真帅!

remote_src #scr参数指定文件信息,会从远程主机上进行查找

  1. [root@m01 ~]# ansible a -m copy -a "src=/root/shuai.txt dest=/tmp/ remote_src=yes"
  2. [root@web01 ~]# echo "你真丑" >> shuai.txt
  3. [root@web01 ~]# cat /tmp/shuai.txt
  4. 你真丑

directory_mode #递归的设定目录的权限,默认为系统默认权限match

follow #当拷贝的文件夹内有link存在的时候,那么拷贝过去的也会有link

local_follow#是否遵循本地机器中的文件系统链接

6.4 file 文件模块
  1. [root@m01 ~/shuai]# ansible a -m file -a "dest=/tmp/shuai/ owner=shuai group=shuai mode=000"
  2. [root@web01 /tmp]# ll
  3. d--------- 7 shuai shuai 155 Aug 21 11:28 shuai

可以利用模块创建数据信息 (文件 目录 链接文件)

state 参数

=absent — 删除数据信息

=directory — 创建一个目录信息

=file — 检查创建的数据信息是否存在 (绿色存在 红色不存在)

=hard — 创建一个硬链接文件

=link — 创建一个软链接文件

=touch — 创建一个文件信息

  1. [root@m01 ~]# ansible a -m file -a "dest=/shuai/shuai.txt state=absent"
  2. [root@m01 ~]# ansible 172.16.1.31 -m file -a "dest=/shuai/ state=directory"
  3. [root@m01 ~]# ansible a -m file -a "dest=/shuai state=file"
  4. [root@m01 ~]# ansible a -m file -a "src=/shuai/shuai.txt dest=/shuai/shuai_hard.txt state=hard"
  5. [root@m01 ~]# ansible a -m file -a "src=/shuai/shuai.txt dest=/shuai/shuai_link.txt state=link"
  6. [root@m01 ~]# ansible a -m file -a "dest=/shuai/shuai.txt state=touch"

recurse归修改文件权限

  1. [root@m01 ~]# ansible a -m file -a "dest=/shuai/ owner=shuai mode=133 recurse=yes"
  2. [root@web01 /shuai]# ll
  3. total 0
  4. -rw-r--r-- 1 root root 0 Aug 21 16:09 shuai.txt
  5. [root@web01 /shuai]# ll -d
  6. d--x-wx-wx 2 shuai root 23 Aug 21 16:09 .
  7. [root@web01 /shuai]# ll
  8. total 0
  9. ---x-wx-wx 1 shuai root 0 Aug 21 16:09 shuai.txt
6.5 fetch模块 ( 拉取模块)
参数 详细信息
src 要拉取的目标文件位置
dest 本地存储目录文章
  1. [root@m01 ~/shuai]# ansible a -m fetch -a "src=/root/shuai/fatch.txt dest=/tmp"
  2. [root@m01 ~]# tree /tmp
  3. /tmp
  4. ├── 172.16.1.7
  5. │ └── root
  6. │ └── shuai
  7. │ └── fatch.txt
6.6 yum模块 批量安装软件
参数 详细信息
name 安装软件的名称
state installed 安装 present 安装 latest 安装 absent 卸载 removed 卸载
  1. [root@m01 ~]# ansible a -m yum -a "name=tcpdump state=installed"
  2. [root@m01 ~]# ansible a -m yum -a "name=tcpdump state=removed" ---卸载软件
6.7 service模块 (管理服务的运行状态)
参数 详细信息
name 服务名称
state started 开启 stopped 停止 restarted 重启 reloaded 平滑重启
enable

yes 是 no 否

  1. [root@m01 ~]# ansible a -m service -a "name=nfs state=started enable=yes"
6.8 cron模块 (批量设置多个主机的定时任务模块)
参数 详细信息
minute Minute when the job should run ( 0-59, *, */2, etc ) 分钟
month Month of the year the job should run ( 1-12, *, */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 ) 天
weekda Day of the week that the job should run ( 0-6 for Sunday-Saturday, *, etc ) 周
job 用于定义定时任务需要做的事情
name 定时任务添加注释信息
state absent 删除定时任务 present 新建定时任务(默认不用添加)
disabled yes 注释定时任务 no 开启定时任务

(1)批量创建定时任务

  1. [root@m01 ~]# ansible a -m cron -a "minute=*/2 job='/usr/sbin/ntpdate ntp1.aliyun.com > /dev/null 2>&1' name='sync time'"
  2. [root@web01 ~/shuai]# crontab -l
  3. #Ansible: sync time
  4. */2 * * * * /usr/sbin/ntpdate ntp1.aliyun.com > /dev/null 2>&1

(2)删除定时任务(只能是ansible创建的才可以批量删除)

  1. [root@m01 ~]# ansible a -m cron -a "name='sync time' state=absent

(3)注释定时任务(只能是ansible创建的才可以批量注释)

  1. [root@m01 ~]# ansible a -m cron -a "job='/usr/sbin/ntpdate ntp1.aliyun.com > /dev/null 2>&1' name='sync time' disabled=yes"
  2. [root@web01 ~/shuai]# crontab -l
  3. #Ansible: sync time
  4. #* * * * * /usr/sbin/ntpdate ntp1.aliyun.com > /dev/null 2>&1
6.9 mount模块 (批量挂载模块)
参数 详细信息
src 需要挂载的存储设备或文件信息
path 指定目标挂载点目录
fstype 指定挂载时的文件系统类型
state present 不会立即挂载,修改fstab,实现开机自动挂载 mounted 立即挂载,并且实现开机自动挂载 absent 立即卸载,fstab也会删除 unmounted 立即卸载,fstab不会被删除,下次开机自动挂载
  1. [root@m01 ~]# ansible a -m mount -a "src=172.16.1.7:/data path=/mnt fstype=nfs state=mounted"
  2. [root@web01 ~]# df -h | tail -1
  3. 172.16.1.7:/data 50G 1.7G 49G 4% /mnt

7. user模块

参数 详细信息
name 用户名
uid 用户uid
group 用户组
groups 附加到另一个组
password 密码
shell 解释器
create_home yes 创建家目录(默认是yes) no 不创建家目录
  1. [root@m01 ~]# ansible a -m user -a "name=rsync create_home=no shell=/sbin/nologin"
  2. [root@web01 ~]# id rsync
  3. uid=1002(rsync) gid=1002(rsync) groups=1002(rsync)

(1) 给指定用户创建密码 PS: 利用ansible程序user模块设置用户密码信息,需要将密码明文信息转换为密文信息进行设置 生成密文密码信息方法: 方法一:

  1. ansible all -i localhost, -m debug -a "msg={{ '密码信息123456' | password_hash('sha512', 'shuai') }}"
  1. ##演示
  2. [root@m01 ~]# ansible all -i localhost, -m debug -a "msg={{ '123456' | password_hash('sha512', 'shuai') }}"
  3. localhost | SUCCESS => {
  4. "msg": "$6$shuai$CcqYhe8GgtTbQFxbarWVJAAt.02WAVe03DQEc9tSH0oK0oW3bm6M0gsHUmaw4KDkgul2U9kLzN1Bde54J7nA7/"
  5. }

方法二:

  1. [root@m01 ~]# yum install -y python-pip
  2. [root@m01 ~]# pip install passlib
  3. [root@m01 ~]# python -c "from passlib.hash import sha512_crypt; import getpass; print(sha512_crypt.using(rounds=5000).hash(getpass.getpass()))"
  4. Password:
  5. $6$DUHCNOQ58xnSFVB/$Csd36ECimmg2DPgYfD5doEEfyD.ZBt5zKQeGXyA6fOTrWVinKp7Wy5mx6IEtTKlgIsABoKaRYsadcTKnFPvhG1
  1. [root@m01 ~]# ansible a -m user -a "name=shuai password='$6$oldboy$MVd3DevkLcimrBLdMICrBY8HF82Wtau5cI8D2w4Zs6P1cCfMTcnnyAmmJc7mQaE9zuHxk8JFTRgYMGv9uKW7j1'"

7.剧本的编写方法

7.1剧本的作用:一键化完成多个任务
7.2剧本的组成部分:
模块 含义
hosts 定义主机
tasks

任务

7.3剧本的编写规范(yaml)
7.3.1.合理的缩进信息,两个空格表示一个缩进关系
  1. 标题一
  2. 标题二
  3. 标题三

7.3.2.冒号的使用方法,冒号后边要跟空格

  1. hosts: 172.16.1.7
  2. tasks:
  3. yum: name=rsync
  4.  
  5. #ps:以冒号结尾,冒号信息出现在注释说明中,后面不需要加上空格

7.3.3.短横线的使用(-)列表功能

  1. - 张三

  2. - 打游戏
  3. - 运动
  4. - 李四

  5. 学习
  6. 北京
  7. - 王五

  8. 运动
  9. 天津
  10. #ps:使用短横线构成列表信息,短横线后面需要有空格
7.4开始编写剧本
rsync剧本
  1. [root@m01 /etc/ansible/ansible-playbook]# cat rsync_server.yaml
  2. - hosts: 172.16.1.7
  3. tasks:
  4. - name: 1.服务端安装rsync
  5. yum: name=rsync state=installed
  6. - name: 2.将配置文件推送到客户端
  7. copy: src=/tmp/rsyncd.conf dest=/etc/
  8. - name: 3.创建rsync虚拟用户
  9. user: name=rsync create_home=no shell=/sbin/nologin
  10. - name: 创建目录并修改目录属主属组
  11. file: dest=/backup state=directory owner=rsync group=rsync
  12. - name: 创建密码文件并修改权限
  13. copy: content='rsync_backup:shuai123' dest=/etc/rsync.password mode=600
  14. - name: 启动服务并设置为开机自启
  15. service: name=rsyncd.service state=started enabled=yes
  16. - hosts: 172.16.1.61
  17. tasks:
  18. - name: 创建密码文件并修改权限
  19. copy: content='shuai123' dest=/etc/rsync.password mode=600
  20. - name: 检查测试
  21. shell: rsync -avz /etc/hosts rsync_backup@172.16.1.7::backup --password-file=/etc/rsync.password

7.4.1执行剧本

  1. #######################检查剧本格式#################################
  2. [root@m01 /etc/ansible/ansible-playbook]# ansible-playbook --syntax-check rsync_server.yaml

  3. playbook: rsync_server.yaml
  1. ######################模拟执行######################################
  2. [root@m01 /etc/ansible/ansible-playbook]# ansible-playbook -C rsync_server.yaml
  1. ######################执行剧本#####################################
  2. [root@m01 /etc/ansible/ansible-playbook]# ansible-playbook rsync_server.yaml
NFS剧本
  1. [root@m01 /etc/ansible/ansible-playbook]# cat nfs_server.yaml
  2. - hosts: 172.16.1.7
  3. tasks:
  4. - name: 1.服务端安装软件
  5. yum:
  6. name=nfs-utils state=installed
  7. name=rpcbind state=installed
  8. - name: 2.推送nfs的配置文件
  9. copy: src=/tmp/exports dest=/etc/
  10. - name: 3.创建目录并修改权限
  11. file: dest=/data state=directory owner=nfsnobody group=nfsnobody
  12. - name: 4.启动服务并设置开机自启
  13. service:
  14. name=rpcbind.service enabled=yes
  15. name=nfs enabled=yes


  16. - hosts: 172.16.1.61
  17. tasks:
  18. - name: 1.客户端下载软件
  19. yum: name=nfs-utils state=installed
  20. - name: 2.远程挂载
  21. mount: src=172.16.1.7:/data path=/mnt fstype=nfs state=mounted

8.剧本功能实践介绍

8.1编写剧本的重要功能介绍
a. 在剧本中设置变量信息
  1. 方式一:直接在剧本文件编写
  2. vars:
  3. shuai01:data01
  4. shuai02:data02
  5.  
  6. 方式二:在命令行中进行指定
  7. ansible-playbook -e shuai01=data01 -e shuai02=data02 rsync_server.yaml

  8. 方式三:在主机清单文件编写
  9. [shuai]
  10. 172.16.1.61
  11. 172.16.1.7
  12. [shuai:vars]
  13. shuai01=/data


  14. ####三种方式的优先级####
  15. 最优先:命令行变量设置
  16. 次优先:剧本中变量设置
  17. 最后:主机清单变量设置
  18. 如何全局设置变量: roles 剧本整合

b. 在剧本中设置注册信息 (执行剧本时,可以显示输出命令结果信息)

  1. - hosts: 172.16.1.61
  2. tasks:
  3. - name: 1.客户端下载软件
  4. yum: name=nfs-utils state=installed
  5. - name: 2.远程挂载
  6. mount: src=172.16.1.7:/data path=/mnt fstype=nfs state=mounted
  7. - name: 3.查看结果
  8. shell: df -h|tail -1
  9. register: check_mount #将输出及结果定义变量
  10. - name: 显示结果
  11. debug: msg={{ check_mount.stdout_lines }} #调用变量
c. 在剧本中设置判断信息

获取内置变量的方法

  1. [root@m01 /etc/ansible/ansible-playbook]# ansible a -m setup -a "filter=ansible_hostname"
  2. 172.16.1.7 | SUCCESS => {
  3. "ansible_facts": {
  4. "ansible_hostname": "web01",
  5. "discovered_interpreter_python": "/usr/bin/python"
  6. },
  7. "changed": false
  8. }


  9. #########获取子项的方法#############
  10. ansible_eth0[ipv4] -----添加到剧本才生效
主机信息 含义
ansible_all_ipv4_addresses: 仅显示ipv4的信息。
ansible_devices: 仅显示磁盘设备信息
ansible_distribution: 显示是什么系统,例:centos,suse等
ansible_distribution_major_version: 显示是系统主版本
ansible_distribution_version: 仅显示系统版本。
ansible_machine: 显示系统类型,例:32位,还是64位。
ansible_eth0: 仅显示eth0的信息。
ansible_hostname: 仅显示主机名。
ansible_kernel: 仅显示内核版本
ansible_lvm: 显示lvm相关信息
ansible_memtotal_mb: 显示系统总内存。
ansible_memfree_mb: 显示可用系统内存
ansible_memory_mb: 详细显示内存情况。
ansible_swaptotal_mb: 显示总的swap内存
ansible_swapfree_mb: 显示swap内存的可用内存
ansible_mounts: 显示系统磁盘挂载情况
ansible_processor: 显示cpu个数(具体显示每个cpu的型号)
ansible_processor_vcpus: 显示cpu个数(只显示总的个数)。

例子:

  1. - hosts: a
  2. remote_user: root
  3. tasks:
  4. - name: Check File
  5. file: path=/tmp/this_is_{{ ansible_hostname }}_file state=touch
  6. when: (ansible_hostname == "nfs") or (ansible_hostname == "backup")
  7.  
  8. - name: install httpd
  9. yum: name=httpd state=installed
  10. when: (系统情况 == "CentOS")
  11.  
  12. - name: install httpd2
  13. yum: name=httpd2 state=installed
  14. when: (系统情况 == "ubuntu")

d. 在剧本中设置循环信息

  1. vim test04.yml
  2. - hosts: all
  3. remote_user: root
  4. tasks:
  5. - name: Add Users
  6. user: name={{ item.name }} groups={{ item.groups }} state=present
  7. with_items:
  8. - { name: 'testuser1', groups: 'bin' }
  9. - { name: 'testuser2', groups: 'root' }
  10. vim test05.yml
  11. - hosts: all
  12. remote_user: root
  13. tasks:
  14. - name: Installed Pkg
  15. yum: name={{ item }} state=present
  16. with_items:
  17. - wget
  18. - tree
  19. - lrzsz

f. 在剧本中设置错误忽略

  1. vim test06.yml
  2. - hosts: all
  3. remote_user: root
  4. tasks:
  5. - name: Ignore False
  6. command: /bin/false
  7. ignore_errors: yes #忽略错误继续执行
  8. - name: touch new file
  9. file: path=/tmp/oldboy_ignore state=touch

g 在剧本中设置标签信息(单独调试某一个name时需要)

  1. - hosts: oldboy
  2. ignore_errors: yes
  3. remote_user: root
  4. tasks:
  5. - name: Check File
  6. file: path=/tmp/this_is_{{ ansible_hostname }}_file state=touch
  7. when: (ansible_hostname == "nfs01") or (ansible_hostname == "backup")
  8. tags: t1
  9.  
  10. - name: bad thing
  11. command: ech 123
  12. #ignore_errors: yes
  13. tags: t2
  14.  
  15. - name: install httpd
  16. yum: name=httpd state=installed
  17. when: (ansible_all_ipv4_addresses == ["172.16.1.7","10.0.0.7"])
  18. tags: t3
  19.  
  20. - name: install httpd2
  21. yum: name=httpd2 state=installed
  22. when: (ansible_distribution == "ubuntu")
  23. tags: t4
  24.  
  25. #指定执行哪个标签任务: ansible-playbook --tags=t2 test05.yml
  26. #跳过指定标签任务: ansible-playbook --skip-tags=t2 test05.yml

h.在剧本中设置触发信息

  1. - hosts: backup
  2. remote_user: root
  3. tasks:
  4. - name: 01 Install rsync
  5. yum: name=rsync state=present
  6.  
  7. - name: 02 push config file
  8. copy: src=./file/{{ item.src }} dest=/etc/{{ item.dest }} mode={{ item.mode }}
  9. with_items:
  10. - { src: "rsyncd.conf", dest: "rsyncd.conf", mode: "0644" }
  11. - { src: "rsync.password", dest: "rsync.password", mode: "0600" }
  12. notify: restart rsync server #触发条件

  13. handlers:
  14. - name: restart rsync server #触发器
  15. service: name=rsyncd state=restarted

i. 在剧本中进行剧本整合

  1. 方式一:include_tasks: f1.yml
  2. - hosts: all
  3. remote_user: root
  4. tasks:
  5. - include_tasks: f1.yml
  6. - include_tasks: f2.yml

  7. 方式二:include: f1.yml
  8. - includef1.yml
  9. - includef2.yml

  10. 方式三:- import_playbook:
  11. [root@m01 ansible-playbook]# cat main.yml
  12. - import_playbook: base.yml
  13. - import_playbook: rsync.yml
  14. - import_playbook: nfs.yml
  15. - import_playbook: oxxx.yml
  16. - import_playbook: rsync.yml
  17. - import_playbook: nfs.yml
9.ansible角色配置roles
  1. 第一个历程: 规范目录结构
  2. cd /etc/ansible/roles
  3. mkdir {rsync,nfs} --- 创建相应角色目录
  4. mkdir {nfs,rsync}/{vars,tasks,templates,handlers,files} --- 创建角色目录下面的子目录
  5. [root@m01 roles]# tree
  6. .
  7. ├── nfs
  8. ├── files --- 保存需要分发文件目录
  9. ├── handlers --- 保存触发器配置文件信息
  10. ├── tasks --- 保存要执行的动作信息文件
  11. ├── templates --- 保存需要分发模板文件 模板文件中可以设置变量信息
  12. └── vars --- 保存变量信息文件
  13. └── rsync
  14. ├── files
  15. ├── handlers
  16. ├── tasks
  17. ├── templates
  18. └── vars

  19. 第二个历程: roles目录中创建相关文件
  20. 编写文件流程图:
  21. 1) 编写tasks目录中的main.yml文件
  22. vim main.yml
  23. - include_tasks: copy_info.yml
  24. - include_tasks: create_dir.yml
  25. - include_tasks: boot_server.yml

  26. vim copy_info.yml
  27. - name: 01-copy conf file
  28. copy: src=exports dest=/etc
  29. notify: restart nfs server

  30. vim create_dir.yml
  31. - name: 02-create data dir
  32. file: path={{ Data_dir }} state=directory owner=nfsnobody group=nfsnobody

  33. vim boot_server.yml
  34. - name: 03-boot server
  35. service: name={{ item }} state=started enabled=yes
  36. with_items:
  37. - rpcbind
  38. - nfs

  39. 2) 编写vars目录中的main.yml文件
  40. [root@m01 vars]# vim main.yml
  41. Data_dir: /data

  42. 3) 编写files目录中的文件
  43. [root@m01 files]# ll
  44. total 4
  45. -rw-r--r-- 1 root root 29 May 17 15:23 exports

  46. 4) 编写handlers目录中的main.yml文件
  47. vim main.yml
  48. - name: restart nfs server
  49. service: name=nfs state=restarted

  50. 目录中文件编写好汇总结构
  51. [root@m01 nfs]# tree
  52. .
  53. ├── files
  54. └── exports
  55. ├── handlers
  56. └── main.yml
  57. ├── tasks
  58. └── main.yml
  59. ├── templates
  60. └── vars
  61. └── main.yml
  62.  
  63. 第三个历程: 编写一个主剧本文件
  64. [root@m01 roles]# cat site.yml
  65. - hosts: nfs_server
  66. roles:
  67. - nfs-server

  68. - hosts: rsync_server
  69. roles:
  70. - rsync

补充:ansible学习帮助手册如何查看

  1. [root@m01 ~]# ansible-doc -l --模块简介
  2. [root@m01 ~]# ansible-doc -s command --模块的详细信息
  3. [root@m01 ~]# ansible-doc file --查询模块在剧本中的应用方法

Linux-ansible批量管理的更多相关文章

  1. Linux(11):期中架构(3)--- SSH远程管理服务 & ansible 批量管理服务

    SSH远程管理服务 1. 远程管理服务知识介绍 # 1.1 SSH远程登录服务介绍说明 SSH是Secure Shell Protocol的简写,由 IETF 网络工作小组(Network Worki ...

  2. 六.ansible批量管理服务

    期中集群架构-第六章-ansible批量管理服务介绍====================================================================== 01. ...

  3. Ansible 批量管理Windows Server服务器

    Ansible批量管理Windows Server         Ansible是一款为类Unix系统开发的自由开源的配置和自动化工具,  它用Python写成,类似于saltstack和Puppe ...

  4. ansible批量管理服务 上

    1 ansible简介 1.1 ansible批量管理服务概述 (1)是基于python语言开发的自动化软件工具(2)是基于SSH远程管理服务实现远程主机批量管理(3)并行管理,部署简单,应用也简单方 ...

  5. windows下运行的linux服务器批量管理工具(带UI界面)

    产生背景: 由于做服务器运维方面的工作,需要一人对近千台LINUX服务器进行统一集中的管理,如同时批量对LINUX服务器执行相关的指令.同时批量对LINUX服务器upload程序包.同时批量对LINU ...

  6. 使用ansible批量管理远程服务器

    使用ansible批量管理远程服务器 背景 本地需要管理远程的一批服务器,主要执行以下任务: 1) 将本地的文件复制到远端所有服务器: 2) 需要在远程服务器中执行一个个命令: 远端服务器路径并非完全 ...

  7. Linux下批量管理工具pssh安装和使用

    Linux下批量管理工具pssh安装和使用 pssh工具包 安装:yum -y install pssh pssh:在多个主机上并行地运行命令 pscp:把文件并行地复制到多个主机上 prsync:通 ...

  8. Linux中ansible批量管理软件部署及剧本编写

    服务器版本信息: Centos6.9 [root@db02 ~]# uname -a Linux db02 2.6.32-696.el6.x86_64 #1 SMP Tue Mar 21 19:29: ...

  9. Linux系统——Ansible批量管理工具

    批量管理工具: (1)ansible 操作简单(适用于500台以下服务器) (2)saltstack 比较复杂(一般适用于1000-4w台服务器) (3)puppet超级复杂 systemctl(统一 ...

  10. linux运维、架构之路-ansible批量管理

    一.ansible软件 1.介绍 ①ansible是一个基于Python开发的自动化运维工具 ②其功能实现基于SSH远程连接服务 ③ansible可以实现批量系统配置.批量软件部署.批量文件拷贝.批量 ...

随机推荐

  1. CSS元素的盒类型

    一.css简介 CSS是Cascading Style Sheet的缩写,中文称层叠样式表.HTML中的元素都有着自己的属性和默认样式,CSS控制HTML内标签显示不同布局样式.控制对应html标签颜 ...

  2. Ubuntu20.04 网络配置

    Ubuntu20.04 网络配置 设置 ROOT 密码 先设置 root 密码,后面直接使用 root 用户操作 it@it:~$ sudo passwd root [sudo] password f ...

  3. component: resolve => require(['../pages/home.vue'], resolve)-装载

    import Vue from 'vue'import VueRouter from 'vue-router'// "@"相当于".."import Detai ...

  4. C++对象内存分布详解(包括字节对齐和虚函数表)

    转自:https://www.jb51.net/article/101122.htm 1.C++对象的内存分布和虚函数表: C++对象的内存分布和虚函数表注意,对象中保存的是虚函数表指针,而不是虚函数 ...

  5. System Verilog MCDF(一)

  6. node.js学习(4)事件

    1 导入事件库

  7. GPU特征处理技术

    GPU特征处理技术 GPU和CPU有何不同? 现代片上系统(SoC)通常集成中央处理器(CPU)和图形处理器(GPU).设计不同,这可能更取决于处理的数据集的类型. CPU经过优化,可以一次对几块数据 ...

  8. 旷视MegEngine核心技术升级

    旷视MegEngine核心技术升级 7 月 11 日,旷视研究院在 2020 WAIC · 开发者日「深度学习框架与技术生态论坛」上围绕 6 月底发布的天元深度学习框架(MegEngine)Beta ...

  9. 毫米波RADAR与LIDAR探秘

    毫米波RADAR与LIDAR探秘 说起激光雷达和毫米波雷达,相信业内人士并不陌生,激光雷达是以发射激光束探测目标的位置.速度等特征量的雷达系统.而毫米波雷达是指工作在毫米波波段探测的雷达.毫米波实质上 ...

  10. C#中关于Cookie的理解

    本文链接出自:https://www.cnblogs.com/xiangzhe-C/p/4230042.html 1.Cookie简介 Cookie 提供了一种在 Web 应用程序中存储用户特定信息的 ...