1 ansible-playbook 任务剧本

1.1 剧本文件概念

(1)playbook可以将多个批量操作模块功能整合,完成一件事情。
(2)简化运维工作复杂度
(3)playbook通过yaml语法识别描述的状态文件,扩展名是yaml

1.2 剧本文件组成部分

(1)剧本的角色(hosts)定义的是主机信息
(2)剧本的任务(tasks)定义的是具体任务信息
(3)一个剧本文件有多个hosts组成,一个hosts可以包含多个tasks任务

1.3 剧本文件优势特点

(1)实现自动化功能更加全面
(2)可以更好的控制逻辑关系
(3)剧本展现命令语法更直观
(4)拥有持久反复执行的特性

1.4 剧本文件编写规范

(1)缩进特点:  两个空格表示一个缩进关系
(2)冒号用法:  冒号后面需要有空格  冒号结尾不需要有空格
主机信息: 172.16.1.41   --- key: value (键值写法)
(3)列表用法:  利用短横线加空格构建列表清单

1.5 剧本执行使用方法

(1)检查剧本语法:ansible-playbook --syntax-check test.yaml
(2)剧本模拟执行:ansible-playbook -C test.yaml
(3)剧本真实运行:ansible-playbook  test.yaml

1.6 剧本编写扩展功能

(1)剧本变量编写功能
(2)剧本信息通知功能
(3)剧本信息判断功能
(4)剧本信息循环功能
(5)剧本编写忽略错误
(6)剧本标签设置功能
(7)剧本忽略采集功能
(8)剧本信息触发功能

1.6.1 剧本变量编写功能

设置变量方法一: 在剧本执行命令参数中设置变量,命令行最优先

  1. [root@m01 ansible_playbook]#ansible-playbook -e dir=/etc -e file=rsyncd.conf test_变量编写.yaml

设置变量方法二: 在剧本中设置变量,剧本变量其次优先

  1. [root@m01 ansible_playbook]#vim test_变量编写.yaml
  2. - hosts: 172.16.1.41
  3. vars:
  4. dir: /etc
  5. file: rsyncd.conf
  6. tasks:
  7. - name: copy file
  8. copy: src={{ dir }}/{{ file }} dest={{ dir }}/
  9. # {{}}调用变量

设置变量方法二: 在主机清单中设置变量,主机清单变量最不优先

  1. [root@m01 ansible_playbook]#vim /etc/ansible/hosts
  2. [sersync_server]
  3. 172.16.1.31
  4. [sersync_client]
  5. 172.16.1.41
  6. [sersync_server:vars]
  7. dir=/etc
  8. file=rsyncd.conf
  9. # 直接给主机组设置变量,这样主机组内的所有主机都可以调用变量了

1.6.2 剧本信息通知功能

编辑剧本

  1. [root@m01 ansible_playbook]#vim test_通知功能.yaml
  2. - hosts: 172.16.1.41
  3. tasks:
  4. - name: boot server
  5. service: name=rsyncd state=started
  6. - name: check server boot
  7. shell: netstat -lntup|grep 873
  8. register: oldboy
  9. - debug: msg={{ oldboy.stdout_lines }}
  10. # 将shell中命令执行结果通过register注册给oldboy,oldboy相当于一个变量,{{}}调取oldboy
  11. # debug类似echo,输出信息
  12. # stdout_lines 将输出的信息变得有格式

运行剧本

  1. [root@m01 ansible_playbook]#ansible-playbook test_通知功能.yaml
  2. PLAY [172.16.1.41] ***********************************************************************************
  3. TASK [Gathering Facts] *******************************************************************************
  4. ok: [172.16.1.41]
  5. TASK [boot server] ***********************************************************************************
  6. ok: [172.16.1.41]
  7. TASK [check server boot] *****************************************************************************
  8. changed: [172.16.1.41]
  9. TASK [debug] *****************************************************************************************
  10. ok: [172.16.1.41] => {
  11. "msg": [
  12. "tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 3708/rsync ",
  13. "tcp6 0 0 :::873 :::* LISTEN 3708/rsync "
  14. ]
  15. }
  16. PLAY RECAP *******************************************************************************************
  17. 172.16.1.41 : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

1.6.3 剧本信息判断功能

nfs服务客户端三台主机
centos7  10.0.0.7、centos6  10.0.0.8、centos7  10.0.0.9
此时在批量启动的时候需要进行判断,因为centos6,centos7启动命令不一样
判断的格式

  1. - hosts: nfs_client
  2. tasks:
  3. - name: boot centos7 nfs
  4. shell: systemctl start nfs
  5. 判断: 如果是centos7 ???
  6. - name: boot centos6 nfs
  7. shell: /etc/init.d/nfs start
  8. 判断: 如果是centos6 ???

setup模块:收集远程主机信息
语法:

  1. [root@m01 ansible_playbook]#ansible 172.16.1.41 -m setup -a "filter=ansible_hostname"
  2. 172.16.1.41 | SUCCESS => {
  3. "ansible_facts": {
  4. "ansible_hostname": "backup",
  5. "discovered_interpreter_python": "/usr/bin/python"
  6. },
  7. "changed": false
  8. }
  9. # filter 过滤 筛选

实现收集子信息的方法

  1. 问题: 获取主机信息,以及子信息
  2. 方法一:
  3. - hosts: rsync
  4. tasks:
  5. - name: touch file
  6. file: path=/etc/oldboy01.txt state=touch
  7. when: (ansible_eth1.ipv4.address == "172.16.1.41")
  8. 方法二:
  9. - hosts: rsync
  10. tasks:
  11. - name: touch file
  12. file: path=/etc/oldboy01.txt state=touch
  13. when: (ansible_eth1["ipv4"]["address"] == "172.16.1.41")

setup模块常用来收集的信息

根据 ip 地址进行判断创建目录

  1. [root@m01 ansible_playbook]#vim test_判断功能.yaml
  2. - hosts: nfs_client
  3. tasks:
  4. - name: create file for 41 host
  5. file: path=/tmp/172.16.1.41 state=directory
  6. when: (ansible_hostname == "backup")
  7. - name: create file for 7 host
  8. file: path=/tmp/172.16.1.7 state=directory
  9. when: (ansible_hostname == "web01")

运行剧本

  1. root@m01 ansible_playbook]#ansible-playbook -C test_判断功能.yaml
  2. PLAY [nfs_client] ************************************************************************************
  3. TASK [Gathering Facts] *******************************************************************************
  4. ok: [172.16.1.41]
  5. ok: [172.16.1.7]
  6. TASK [create file for 41 host] ***********************************************************************
  7. skipping: [172.16.1.7]
  8. changed: [172.16.1.41]
  9. TASK [create file for 7 host] ************************************************************************
  10. skipping: [172.16.1.41]
  11. changed: [172.16.1.7]
  12. PLAY RECAP *******************************************************************************************
  13. 172.16.1.41 : ok=2 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
  14. 172.16.1.7 : ok=2 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0

1.6.4 剧本信息循环功能

循环创建多个用户

  1. [root@m01 ansible_playbook]#vim test_循环功能.yaml
  2. - hosts: 172.16.1.41
  3. tasks:
  4. - name: create user
  5. user: name={{ item }}
  6. with_items:
  7. - oldgirl01
  8. - oldgirl02
  9. - oldgirl03
  10. - oldgirl04
  11. - oldgirl05

循环创建多个用户  多个用户uid数值是不同的

  1. [root@m01 ansible_playbook]#vim test_循环功能.yaml
  2. - hosts: 172.16.1.41
  3. tasks:
  4. - name: create user
  5. user: name={{ item.name }} uid={{ item.uid }}
  6. with_items:
  7. - {name: "oldgirl06", uid: "3006"}
  8. - {name: "oldgirl07", uid: "3007"}
  9. - {name: "oldgirl08", uid: "3008"}
  10. - {name: "oldgirl09", uid: "3009"}
  11. - name: check create user info
  12. shell: grep oldgirl0 /etc/passwd
  13. register: user_info
  14. - debug: msg={{ user_info.stdout_lines }}

1.6.5 剧本编写忽略错误功能

忽略功能主要用来调试剧本

  1. [root@m01 ansible_playbook]#vim test_h忽略功能.yaml
  2. - hosts: 172.16.1.41
  3. tasks:
  4. - name: create rsync user
  5. shell: useradd rsync -M -s /sbin/nologin
  6. ignore_errors: yes
  7. - name: create backup dir
  8. shell: mkdir /backup
  9. ignore_errors: yes
  10. - name: boot server
  11. shell: systemctl start rsyncd
  12. ignore_errors: yes

在使用shell进行一些操作时,shell产生的结果已经存在时,会导致剧本无法进行下去,因此使用忽略功能可以有效的使剧本进行下去。

1.6.6 剧本标签设置功能

标签功能主要用来调试剧本
tags:标签

  1. [root@m01 ansible_playbook]#vim test_标签功能.yaml
  2. - hosts: 172.16.1.41
  3. tasks:
  4. - name: 01:安装软件
  5. yum: name=rsync state=installed
  6. ignore_errors: yes
  7. - name: 02:创建用户
  8. user: name=rsync create_home=no shell=/sbin/nologin
  9. ignore_errors: yes
  10. tags: create_user
  11. - name: 03:创建目录
  12. file: path=/backup state=directory

运行剧本

  1. ansible-playbook -t create_user test_标签功能.yaml --- 执行剧本中标签任务
  2. ansible-playbook --skip-tags create_user test_标签功能.yaml --- 跳过指定标签任务,执行其他任务
  3. ansible-playbook -t create_user,create_dir test_标签功能.yaml --- 执行多个标签
  4. # -t=tags

1.6.7 剧本忽略采集功能

  1. [root@m01 ansible_playbook]#vim test_忽略采集.yaml
  2. - hosts: 172.16.1.41
  3. gather_facts: no
  4. tasks:
  5. - name: 01:安装软件
  6. yum: name=rsync state=installed
  7. ignore_errors: yes
  8. - name: 02:创建用户
  9. user: name=rsync create_home=no shell=/sbin/nologin
  10. ignore_errors: yes
  11. tags: create_user
  12. - name: 03:创建目录
  13. file: path=/backup state=directory
  14. tags: create_dir

当剧本采集大量主机信息时,可能会变得卡,慢,影响剧本后面的操作执行的效率。所以在这个时候,可以忽略采集功能,提高效率,在hosts下面添加 gather_facts: no
如果剧本中有判断功能,不能使用此参数,因为采集的信息会与判读信息对比

1.6.8 剧本信息触发功能

编写剧本

  1. [root@m01 ansible_playbook]#vim test_触发功能.yaml
  2. - hosts: 172.16.1.41
  3. tasks:
  4. - name: 01:传输配置文件
  5. copy: src=/etc/ansible/ansible_playbook/rsyncd.conf dest=/etc/
  6. notify: rsync_restart
  7. - name: 02:启动服务程序
  8. service: name=rsyncd state=started
  9. handlers:
  10. - name: rsync_restart
  11. service: name=rsyncd state=restarted

handlers:一般用于配置文件修改时,才会进行触发功能,对服务进行重启
notify:传输配置文件过来,notify通知rsync_restart这个触发器。然后handlers会进行重启服务
说明: 整体任务执行完毕,才会执行触发功能

1.7 编写剧本练习题

要求:
(1)在172.16.1.41主机上操作:
       ①将定时任务服务停止
       ②创建一个/etc/目录软连接 在/opt目录中生成  
       ③将本地/etc/hosts文件分发给41主机 保存到/tmp目录中
(2)在172.16.1.31主机上操作:
       ①将防火墙服务开机自动运行
       ②将主机上安装keepalived软件
实践:
编写剧本文件

  1. [root@m01 ansible_playbook]#vim test.yaml
  2. - hosts: 172.16.1.41
  3. tasks:
  4. - service: name=crond state=stopped
  5. - file: src=/etc path=/opt/etc_link state=link
  6. - copy: src=/etc/hosts dest=/tmp
  7. - hosts: 172.16.1.31
  8. tasks:
  9. - service: name=firewalld enabled=yes
  10. - yum: name=keepalived state=installed

剧本语法检查

  1. # 语法检查剧本文件
  2. [root@m01 ansible_playbook]#ansible-playbook --syntax-check test.yaml
  3. playbook: test.yaml

剧本模拟执行

  1. [root@m01 ansible_playbook]#ansible-playbook -C test.yaml
  2. PLAY [172.16.1.41] ***********************************************************************************
  3. TASK [Gathering Facts] *******************************************************************************
  4. ok: [172.16.1.41]
  5. TASK [service] ***************************************************************************************
  6. ok: [172.16.1.41]
  7. TASK [file] ******************************************************************************************
  8. ok: [172.16.1.41]
  9. TASK [copy] ******************************************************************************************
  10. ok: [172.16.1.41]
  11. PLAY [172.16.1.31] ***********************************************************************************
  12. TASK [Gathering Facts] *******************************************************************************
  13. ok: [172.16.1.31]
  14. TASK [service] ***************************************************************************************
  15. ok: [172.16.1.31]
  16. TASK [yum] *******************************************************************************************
  17. ok: [172.16.1.31]
  18. PLAY RECAP *******************************************************************************************
  19. 172.16.1.31 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
  20. 172.16.1.41 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

剧本真实执行

  1. [root@m01 ansible_playbook]#ansible-playbook test.yaml
  2. PLAY [172.16.1.41] ***********************************************************************************
  3. TASK [Gathering Facts] *******************************************************************************
  4. ok: [172.16.1.41]
  5. TASK [service] ***************************************************************************************
  6. ok: [172.16.1.41]
  7. TASK [file] ******************************************************************************************
  8. ok: [172.16.1.41]
  9. TASK [copy] ******************************************************************************************
  10. ok: [172.16.1.41]
  11. PLAY [172.16.1.31] ***********************************************************************************
  12. TASK [Gathering Facts] *******************************************************************************
  13. ok: [172.16.1.31]
  14. TASK [service] ***************************************************************************************
  15. ok: [172.16.1.31]
  16. TASK [yum] *******************************************************************************************
  17. ok: [172.16.1.31]
  18. PLAY RECAP *******************************************************************************************
  19. 172.16.1.31 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
  20. 172.16.1.41 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

补充:
如果系统中装有cowsay软件,在执行命令时,会产生图案信息,影响查阅结果,可以关闭。

  1. [root@m01 ansible]#vim ansible.cfg
  2. # don't like cows? that's unfortunate.
  3. # set to 1 if you don't want cowsay support or export ANSIBLE_NOCOWS=1
  4. # nocows = 1
  5. 把# nocows = 1 中的 # 去掉即可。

1.8 ansible剧本实现rsync一键化部署

第一个历程: 按照模块方式,完成服务每个步骤部署
第一步:服务端配置

  1. # 安装软件程序
  2. ansible rsync -m yum -a "name=rsync state=installed"
  3. # 编写配置文件:要在批量管理主机上提前写好,然后推送给服务端
  4. # 在管理端准备好服务配置文件
  5. ansible rsync_server -m copy -a "src=/etc/ansible/conf_file/rsyncd.conf dest=/etc/"
  6. # 创建虚拟用户
  7. ansible rsync_server -m user -a "name=rsync create_home=no shell=/sbin/nologin"
  8. # 创建密码文件 (授权600)
  9. ansible rsync_server -m copy -a "content='rsync_backup:oldboy123' dest=/etc/rsync.password mode=600"
  10. # 创建备份目录 (授权 属主 属组)
  11. ansible rsync_server -m file -a "path=/backup state=directory owner=rsync group=rsync"
  12. @ 启动程序服务
  13. ansible rsync_server -m service -a "name=rsyncd state=started enabled=yes"

第二步:客户端配置

  1. # 创建密钥文件 (授权600)
  2. ansible rsync_client -m copy -a "content='oldboy123' dest=/etc/rsync.password mode=600"
  3. # 批量测试传输文件
  4. ansible rsync_client -m shell -a "rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password"

第二个历程: 编写剧本信息

  1. [root@m01 ansible_playbook]#vim rsync_auto.yaml
  2. - hosts: rsync_server
  3. tasks:
  4. - name: 01:install rsync
  5. yum: name=rsync state=installed
  6. - name: 02:copy conf file
  7. copy: src=/etc/ansible/conf_file/rsyncd.conf dest=/etc/
  8. - name: 03:create rsync user
  9. user: name=rsync create_home=no shell=/sbin/nologin
  10. - name: 04:create password file
  11. copy: content='rsync_backup:oldboy123' dest=/etc/rsync.password mode=600
  12. - name: 05:create backup dir
  13. file: path=/backup state=directory owner=rsync group=rsync
  14. - name: 06:boot rsync server
  15. service: name=rsyncd state=started enabled=yes
  16. - hosts: rsync_client
  17. tasks:
  18. - name: 01:create password file
  19. copy: content='oldboy123' dest=/etc/rsync.password mode=600

恢复环境剧本

  1. [root@m01 ansible_playbook]#vim rsync_backup.yaml
  2. - hosts: rsync_server
  3. tasks:
  4. - name: 01:delete conf file
  5. file: path=/etc/rsyncd.conf state=absent
  6. - name: 02:delete rsync user
  7. user: name=rsync state=absent
  8. - name: 03:delete password file
  9. file: path=/etc/rsync.password state=absent
  10. - name: 04:delete backup dir
  11. file: path=/backup/ state=absent
  12. - name: 05:boot rsync server
  13. service: name=rsyncd state=stopped enabled=no
  14. - hosts: rsync_client
  15. tasks:
  16. - name: 01:delete password file
  17. file: path=/etc/rsync.password state=absent

1.9 ansible剧本实现nfs一键化部署

第一个历程: 按照模块方式,完成服务每个步骤部署

  1. 服务端配置
  2. 01. 安装部署软件程序: rpcbind nfs-utile
  3. ansible nfs_server -m yum -a "name=rpcbind state=installed"
  4. ansible nfs_server -m yum -a "name=nfs-utile state=installed"
  5. 02. 编写配置文件:配置文件要提前写好
  6. # 批量管理主机写好的配置文件推送给服务端/etc/ansible-playbook/nfs.conf
  7. ansible nfs_server -m copy -a "src=/etc/ansible/ansible_playbook/nfs.conf dest=/etc/exports"
  8. 03. 创建共享目录:
  9. ansible nfs_server -m file -a "path=/data/ state=directory owner=nfsnobody group=nfsnobody"
  10. 04. 启动程序服务:
  11. ansible nfs_server -m service -a "name=rpcbind state=started enabled=yes"
  12. ansible nfs_server -m service -a "name=nfs state=started enabled=yes"
  13. 客户端配置:
  14. 01. 安装部署软件
  15. ansible nfs_client -m yum -a "name=nfs-utile state=installed"
  16. 02. 挂载共享目录
  17. ansible nfs_client -m mount -a "src=172.16.1.31:/data/ path=/mnt fstype=nfs state=mounted"

第二个历程编写剧本:

  1. [root@m01 ansible_playbook]#vim nfs_auto.yaml
  2. - hosts: nfs_server
  3. tasks:
  4. - name: 1:install rpcbind nsf-utils
  5. yum:
  6. name:
  7. - rpcbind
  8. - nfs-utils
  9. state: installed
  10. - name: 2:copy conf file
  11. copy: src=/etc/ansible/ansible_playbook/nfs.conf dest=/etc/exports
  12. - name: 3:create data dir
  13. file: path=/data/ state=directory owner=nfsnobody group=nfsnobody
  14. - name: 4:boot server rcbind
  15. service: name=rpcbind state=started enabled=yes
  16. - name: 4:boot server nfs
  17. service: name=nfs state=restarted enabled=yes
  18. - hosts: nfs_client
  19. tasks:
  20. - name: 1:install nfs
  21. yum: name=nfs-utils state=installed
  22. - name: 2:mount data dir
  23. mount: src=172.16.1.31:/data/ path=/mnt fstype=nfs state=mounted

恢复环境剧本

  1. [root@m01 ansible_playbook]#vim nfs_backup.yaml
  2. - hosts: nfs_server
  3. tasks:
  4. - name: 01:install rpcbind nfs-utils
  5. yum:
  6. name:
  7. - rpcbind
  8. - nfs-utils
  9. state: removed
  10. - name: 02:copy conf file
  11. shell: echo "" >/etc/exports
  12. - name: 03:create data dir
  13. file: path=/data/ state=absent
  14. - hosts: nfs_client
  15. tasks:
  16. - name: 01:install nfs
  17. yum: name=nfs-utils state=removed
  18. - name: 02:mount data dir
  19. mount: src=172.16.1.31:/data/ path=/mnt fstype=nfs state=unmounted

优化剧本:

  1. [root@m01 ansible_playbook]#vim nfs_auto.yaml
  2. - hosts: nfs_server
  3. vars:
  4. conf_file: exports
  5. data_dir: /data
  6. tasks:
  7. - name: 01:install nfs rpcbind
  8. yum:
  9. name: ['nfs-utils', 'rpcbind']
  10. state: installed
  11. - name: 02:copy conf file
  12. copy: src=/etc/ansible/ansible_playbook/nfs.conf dest=/etc/{{ conf_file }}
  13. notify:
  14. - nfs_restart
  15. - name: 03:create data dir
  16. file: path={{ data_dir }} state=directory owner=nfsnobody group=nfsnobody
  17. - name: 04:boot server rpcbind
  18. service: name={{ item.name }} state={{ item.state }} enabled={{ item.enabled }}
  19. with_items:
  20. - {name: "rpcbind", state: "started", enabled: "yes"}
  21. - {name: "nfs", state: "started", enabled: "yes"}
  22. handlers:
  23. - name: nfs_restart
  24. service: name=nfs state=reloaded
  25. - hosts: nfs_client
  26. vars:
  27. data_dir: /data
  28. tasks:
  29. - name: 01:install nfs
  30. yum: name=nfs-utils state=installed
  31. - name: 02:mount data dir
  32. mount: src=172.16.1.31:{{ data_dir }} path=/mnt fstype=nfs state=mounted
  33. - name: 03:check mount info
  34. shell: df -h|grep mnt
  35. register: mount_info
  36. - debug: msg={{ mount_info.stdout_lines }}

1.10 ansible剧本实现sersync一键化部署

第一个历程: 按照模块方式,完成服务每个步骤部署

  1. 配置hosts主机清单
  2. [server_server]
  3. 172.16.1.31
  4. [server_client]
  5. 172.16.1.41
  6. #安装rsync
  7. ansible backup_server -m yum -a "name=rsync state=installed"
  8. #在批量管理主机上下载sersync,解压发送给客户端
  9. ansible backup_server -m file -a "src=/usr/local/sersync_installdir_64bit/sersync dest=/usr/local"
  10. #在批量管理主机上写好sersync配置文件,发送给客户端
  11. ansible backup_server -m copy -a "src=/usr/local/sersync_installdir_64bit/sersync/conf/confxml.xml dest=/usr/local/sersync/conf/"
  12. #给sersync加上执行权限
  13. ansible backup_server -m file -a "path=/usr/local/sersync/bin/sersync mode=a+x"
  14. #给sersync创建软链接
  15. ansible backup_server -m file -a "src=/usr/local/sersync/bin/sersync path=/usr/local/sbin/sersync state=link"
  16. #启动sersync 测试实时同步
  17. ansible backup_server -m shell -a "sersync -dro /usr/local/sersync/conf/confxml.xml"

第二个历程,编写剧本

  1. [root@m01 ansible_playbook]#vim sersync_auto.yaml
  2. - hosts: sersync_server
  3. tasks:
  4. - name: 安装rsync
  5. yum: name=rsync state=installed
  6. - name: sersync传输到客户端
  7. file: src=/usr/local/sersync_installdir_64bit/sersync/ dest=/usr/local
  8. - name: 将写好的配置文件传输到客户端
  9. copy: src=/usr/local/sersync_installdir_64bit/sersync/conf/confxml.xml dest=/usr/local/sersync/conf/
  10. - name: 加上执行权限
  11. file: path=/usr/local/sersync/bin/sersync mode=a+x
  12. - name: 创建软链接
  13. file: src=/usr/local/sersync/bin/sersync path=/usr/local/sbin/sersync state=link
  14. - name: 启动sersync 测试实时同步
  15. shell: sersync -dro /usr/local/sersync/conf/confxml.xml

恢复环境剧本

  1. [root@m01 ansible_playbook]#cat sersync_backup.yaml
  2. - hosts: sersync_server
  3. tasks:
  4. - name: 卸载rsync
  5. yum: name=rsync state=removed
  6. - name: 删除sersync
  7. file: path=/usr/local/sersync

2 多个剧本如何进行整合

第一个历程: 确保每个剧本执行成功
第二个历程: 进行剧本整合
方法一:不建议使用

  1. [root@m01 ansible_playbook]#vim zhenghe.yaml # ---角色里使用
  2. - hosts: all
  3. remote_user: root
  4. tasks:
  5. - include_tasks: nfs_auto.yml
  6. - include_tasks: rsync_auto.yml
  7. # 不写hosts信息,只写任务信息

方法二:在以后的ansible中可能会取消include功能

  1. [root@m01 ansible_playbook]#vim zhenghe.yaml
  2. - includenfs_auto.yml
  3. - includersync_auto.yml

方法三:建议使用这个方法

  1. [root@m01 ansible_playbook]#vim zhenghe.yaml
  2. - import_playbook: nfs_auto.yaml
  3. - import_playbook: rsync_auto.yaml

3 ansible剧本编写方式:角色

(1)规范ansible程序目录结构
(2)汇总剧本中有定义的主机信息

3.1 角色调用流程图

3.2 nfs服务角色编写

第一个历程: 创建角色目录结构

  1. cd roles/;mkdir {nfs,rsync,web,sersync}
  2. cd nfs/{vars,tasks,templates,handlers,files}
  3. # vars: 定义变量信息
  4. # tasks: 定义任务信息
  5. # templates: 定义模板文件(jinja2模板文件)
  6. # handlers: 定义触发器信息
  7. # files: 定义需要分发的文件

第二个历程: 编写文件信息
tasks:  任务信息编写方式一:
nfs服务编写

  1. vim main.yaml
  2. - name: 01:install nfs rpcbind
  3. yum:
  4. name: ['nfs-utils', 'rpcbind']
  5. state: installed
  6. - name: 02:copy conf file
  7. copy: src=/etc/ansible/ansible_playbook/nfs.conf dest=/etc/{{ conf_file }}
  8. notify:
  9. - nfs_restart
  10. - name: 03:create data dir
  11. file: path={{ data_dir }} state=directory owner=nfsnobody group=nfsnobody
  12. - name: 04:boot server rpcbind
  13. service: name={{ item.name }} state={{ item.state }} enabled={{ item.enabled }}
  14. with_items:
  15. - {name: "rpcbind", state: "started", enabled: "yes"}
  16. - {name: "nfs", state: "started", enabled: "yes"}
  17. - name: 01:install nfs
  18. yum: name=nfs-utils state=installed
  19. - name: 02:mount data dir
  20. mount: src=172.16.1.31:{{ data_dir }} path=/mnt fstype=nfs state=mounted
  21. - name: 03:check mount info
  22. shell: df -h|grep mnt
  23. register: mount_info
  24. - debug: msg={{ mount_info.stdout_lines }}

tasks:  任务信息编写方式二:
tasks:定义任务信息

  1. cd tasks
  2. vim main.yaml
  3. vim nfs_boot.yaml
  4. vim nfs_conf.yaml
  5. vim nfs_datadir.yaml
  6. vim nfs_install.yaml
  7. vim nfs_mount.yaml
  8. #########################
  9. vim main.yaml
  10. - include_tasks: nfs_install.yaml
  11. - include_tasks: nfs_conf.yaml
  12. - include_tasks: nfs_datadir.yaml
  13. - include_tasks: nfs_boot.yaml
  14. - include_tasks: nfs_mount.yaml

vars:定义变量信息

  1. vim main.yaml
  2. conf_file: exports
  3. data_dir: /data

files:定义需要分发的文件

  1. [root@m01 files]# ll
  2. total 4
  3. -rw-r--r-- 1 root root 42 Jul 29 10:34 nfs.conf

handlers:定义触发器信息

  1. vim main.yaml
  2. - name: nfs_restart
  3. service: name=nfs state=reloaded

ansible批量管理服务 下的更多相关文章

  1. 六.ansible批量管理服务

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

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

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

  3. ansible批量管理服务 上

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

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

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

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

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

  6. ansible批量管理软件部署及剧本

    服务器版本信息: Centos6.9 [root@db02 ~]# uname -a Linux db02 -.el6.x86_64 # SMP Tue Mar :: UTC x86_64 x86_6 ...

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

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

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

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

  9. ansible批量管理常见的配置方法

    第7章 ansible的管理 7.1 ansible概念的介绍 ansible-playbook –syntax            检查语法 ansible-playbook -C         ...

随机推荐

  1. Python向FTP服务器上传文件

    上传 代码示例: #!/usr/bin/python # -*- coding:utf-8 -*- from ftplib import FTP ftp = FTP() # 打开调试级别2, 显示详细 ...

  2. tensorflow 离线使用 fashion_mnist 数据集

    在tensflow中加载 fashion_mnist 数据集时,由于网络原因.可能会长时间加载不到或报错 此时我们可以通过离线的方式加载 1.首先下载数据集:fashion_mnist (下载后解压) ...

  3. MySQL metalock的一些技巧(写大于读的案例,以及获得锁的顺序)

    前言:元数据锁不是锁定数据,而是锁定描述数据的元数据信息.就像很多装修工人(工作线程)在室内(对象上)装修(操作),不能有其他工人(线程)把屋子拆了(表删除了). MySQL 为了数据一致性使用元数据 ...

  4. HDU 3065:病毒侵袭持续中(AC自动机)

    http://acm.hdu.edu.cn/showproblem.php?pid=3065 题意:中文题意. 思路:直接插入然后用一个数组记录id和cnt,因为n只有1000,可以开一个数组判断第几 ...

  5. Codeforces 730A:Toda 2(multiset模拟)

    http://codeforces.com/problemset/problem/730/A 题意:有n个人打天梯,想让这n个人的分数相同,每场比赛必须有2-5个人参赛,参赛的人会降低一分,问一个合理 ...

  6. 『开发技巧』Keras自定义对象(层、评价函数与损失)

    1.自定义层 对于简单.无状态的自定义操作,你也许可以通过 layers.core.Lambda 层来实现.但是对于那些包含了可训练权重的自定义层,你应该自己实现这种层. 这是一个 Keras2.0  ...

  7. c++学习书籍推荐《C++ GUI Qt 4编程(第2版)》下载

    下载地址:点我 百度云及其他网盘下载地址:点我 编辑推荐 <C++ GUI Qt 4编程(第2版)>讲授的大量Qt4编程原理和实践,都可以轻易将其应用于Qt4.4.Qt4.5及后续版本的Q ...

  8. GeoPackage - 一个简便轻量的本地地理数据库

    GeoPackage(以下简称gpkg),内部使用SQLite实现的一种单文件.与操作系统无关的地理数据库. 当前标准是1.2.1,该版本的html版说明书:https://www.geopackag ...

  9. Docker笔记(六):容器管理

    原文地址:http://blog.jboost.cn/2019/07/21/docker-6.html 容器是Docker中的另一核心概念,在Docker中,应用的运行都是在容器内进行的,容器则基于镜 ...

  10. 《C# 语言学习笔记》——目录

    C# 简介 变量和表达式 流程控制 3.1 布尔逻辑 3.2 goto语句 3.3 分支 3.4 循环 变量的更多内容 4.1 类型转换 4.2 复杂的变量类型 4.3 字符串的处理 函数 5.1 定 ...