playbook 相当于多个命令的编排组合然后一起运行,类似写脚本。在学习 playbook 之前需要了解 yaml 格式。

编写playbook的步骤:

  • 定义主机与用户
  • 编写任务列表
  • 执行 playbook

当然 playbook 支持拆分多个文件,并且可以使用多种维度的封装,例如定义变量、任务、处理程序等,鼓励代码/文件复用。

下面是安装 nginx 和 ntp server 的示例。

1、文件目录结构

  1. [root@localhost ansible_demo]# tree nginx_ntp/
  2. nginx_ntp/
  3. ├── group_vars
  4.    ├── all
  5.    └── webserver
  6. ├── hosts
  7. ├── roles
  8.    ├── common
  9.       ├── handlers
  10.          └── main.yml
  11.       ├── tasks
  12.          └── main.yml
  13.       ├── templates
  14.          └── ntp.conf.j2
  15.       └── vars
  16.       └── main.yml
  17.    └── web
  18.    ├── handlers
  19.       └── main.yml
  20.    ├── tasks
  21.       └── main.yml
  22.    └── templates
  23.    └── nginx2.conf
  24. └── site.yml
  25.  
  26. 11 directories, 11 files

2、Inventory 文件

  1. [root@localhost nginx_ntp]# cat hosts
  2. [webserver]
  3. 192.168.34.129
  4. 192.168.34.130

3、playbook 入口文件

  1. [root@localhost nginx_ntp]# cat site.yml
  2. ---
  3. - name: apply common configuration to all nodes
  4. hosts: all
  5. roles:
  6. - common
  7.  
  8. - name: configure and deploy the webserver and application code
  9. hosts: webserver
  10. roles:
  11. - web

4、组变量文件

根据 inventory 来区分,all 表示所有,webserver 则指 hosts 中的 webserver section

  1. [root@localhost nginx_ntp]# cd group_vars/
  2. [root@localhost group_vars]# ll
  3. total 8
  4. -rw-r--r--. 1 root root 32 Mar 6 18:12 all
  5. -rw-r--r--. 1 root root 72 Mar 6 18:24 webserver
  6. [root@localhost group_vars]# cat all
  7. ---
  8. ntpserver: ntp.sjtu.edu.cn
  9.  
  10. [root@localhost group_vars]# cat webserver
  11. ---
  12. worker_processes: 4
  13. root: /data1
  14. worker_connections: 1024
  15. user: www

5、roles 设置

5.1、角色 common 配置

安装 ntp server,配置文件并同步时间启动 ntp server

  • 任务入口 tasks
  1. [root@localhost nginx_ntp]# cd roles/common/tasks/
  2. [root@localhost tasks]# cat main.yml
  3. ---
  4. - name: install ntp server
  5. yum: name=ntp state=present
  6.  
  7. - name: set zone info
  8. shell: \cp -rf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  9.  
  10. - name: update time
  11. shell: ntpdate asia.pool.ntp.org
  12.  
  13. - name: configure ntp file
  14. template: src=ntp.conf.j2 dest=/etc/ntp.conf
  15. notify:
  16. - restart ntp
  17.  
  18. - name: start ntp server
  19. service: name=ntpd state=started enabled=true
  20.  
  21. - name: test to see if selinux is running
  22. command: getenforce
  23. register: sestatus
  24. changed_when: false
  • name 为 configure ntp file 的任务运行后的 callback handlers
  1. [root@localhost common]# cat handlers/main.yml
  2. ---
  3. - name: restart ntp
  4. service: name=ntpd state=restarted
  • template 源文件
  1. [root@localhost common]# cat templates/ntp.conf.j2
  2. driftfile /var/lib/ntp/drift
  3. restrict 127.0.0.1
  4. restrict -6 :: 1
  5.  
  6. server {{ ntpserver }}
  7. includefile /etc/ntp/crypto/pw
  8. keys /etc/ntp/keys
  • role common 内置变量
  1. [root@localhost common]# cat vars/main.yml
  2. ---
  3. ntpserver: 210.72.145.44

5.2 角色 web 配置

安装 nginx,配置文件并启动

  • 任务入口 tasks
  1. [root@localhost web]# cat tasks/main.yml
  2. ---
  3.  
  4. - name: install nginx
  5. yum: name=nginx state=latest
  6.  
  7. - name: add user
  8. shell: useradd {{ user }}
  9.  
  10. - name: write config
  11. template: src=nginx2.conf dest=/etc/nginx/nginx.conf
  12. notify:
  13. - restart nginx
  14.  
  15. - name: ensure nginx is running
  16. shell: /usr/sbin/nginx -c /etc/nginx/nginx.conf
  • name 为 write config 的任务运行后的 callback handlers
  1. [root@localhost web]# cat handlers/main.yml
  2. ---
  3. - name: restart nginx
  4. service: name=nginx state=restarted
  • template 文件
  1. [root@localhost web]# cat templates/nginx2.conf
  2. user www;
  3. worker_processes {{ worker_processes }};
  4.  
  5. events {
  6. worker_connections {{ worker_connections }};
  7. }
  8.  
  9. http {
  10. include mime.types;
  11. default_type application/octet-stream;
  12.  
  13. sendfile on;
  14. keepalive_timeout 65;
  15.  
  16. server {
  17. listen 80;
  18. server_name localhost;
  19. root {{ root }};
  20.  
  21. #charset koi8-r;
  22.  
  23. #access_log logs/host.access.log main;
  24.  
  25. location / {
  26. root html;
  27. index index.html index.htm;
  28. }
  29.  
  30. #error_page 404 /404.html;
  31.  
  32. # redirect server error pages to the static page /50x.html
  33. #
  34. error_page 500 502 503 504 /50x.html;
  35. location = /50x.html {
  36. root html;
  37. }
  38. }
  39. }

6 检查语法和运行

  1. [root@localhost nginx_ntp]# ansible-playbook -i hosts site.yml --syntax-check
  2. ERROR! Problem parsing file '/data1/ansible_demo/nginx_ntp/group_vars/all': line 2, column 1

发现 all 文件出错,修改后继续

  1. [root@localhost nginx_ntp]# ansible-playbook -i hosts site.yml --syntax-check
  2.  
  3. playbook: site.yml
  4. [root@localhost nginx_ntp]# ansible-playbook -i hosts site.yml -f 10
  5.  
  6. PLAY [apply common configuration to all nodes] **********************************************************************************************
  7.  
  8. TASK [Gathering Facts] **********************************************************************************************************************
  9. ok: [192.168.34.130]
  10. ok: [192.168.34.129]
  11.  
  12. TASK [common : install ntp server] **********************************************************************************************************
  13. ok: [192.168.34.129]
  14. ok: [192.168.34.130]
  15.  
  16. TASK [common : set zone info] ***************************************************************************************************************
  17. changed: [192.168.34.130]
  18. changed: [192.168.34.129]
  19.  
  20. TASK [common : update time] *****************************************************************************************************************
  21. changed: [192.168.34.130]
  22. changed: [192.168.34.129]
  23.  
  24. TASK [common : configure ntp file] **********************************************************************************************************
  25. changed: [192.168.34.129]
  26. changed: [192.168.34.130]
  27.  
  28. TASK [common : start ntp server] ************************************************************************************************************
  29. changed: [192.168.34.130]
  30. changed: [192.168.34.129]
  31.  
  32. TASK [common : test to see if selinux is running] *******************************************************************************************
  33. ok: [192.168.34.130]
  34. ok: [192.168.34.129]
  35.  
  36. RUNNING HANDLER [common : restart ntp] ******************************************************************************************************
  37. changed: [192.168.34.129]
  38. changed: [192.168.34.130]
  39.  
  40. PLAY [configure and deploy the webserver and application code] ******************************************************************************
  41.  
  42. TASK [Gathering Facts] **********************************************************************************************************************
  43. ok: [192.168.34.130]
  44. ok: [192.168.34.129]
  45.  
  46. TASK [web : install nginx] ******************************************************************************************************************
  47. ok: [192.168.34.130]
  48. ok: [192.168.34.129]
  49.  
  50. TASK [web : add user] ***********************************************************************************************************************
  51. changed: [192.168.34.129]
  52. changed: [192.168.34.130]
  53.  
  54. TASK [web : write config] *******************************************************************************************************************
  55. changed: [192.168.34.129]
  56. changed: [192.168.34.130]
  57.  
  58. TASK [web : ensure nginx is running] ********************************************************************************************************
  59. changed: [192.168.34.129]
  60. changed: [192.168.34.130]
  61.  
  62. RUNNING HANDLER [web : restart nginx] *******************************************************************************************************
  63. changed: [192.168.34.129]
  64. changed: [192.168.34.130]
  65.  
  66. PLAY RECAP **********************************************************************************************************************************
  67. 192.168.34.129 : ok=14 changed=9 unreachable=0 failed=0
  68. 192.168.34.130 : ok=14 changed=9 unreachable=0 failed=0
  69.  
  70. [root@localhost nginx_ntp]#

 7、被控机验证

  1. [root@localhost yum.repos.d]# ps -ef | grep ntp
  2. ntp : ? :: ntpd -u ntp:ntp -p /var/run/ntpd.pid -g
  3. root : pts/ :: grep ntp
  4. [root@localhost yum.repos.d]# ps -ef | grep nginx
  5. root : ? :: nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
  6. www : ? :: nginx: worker process
  7. www : ? :: nginx: worker process
  8. www : ? :: nginx: worker process
  9. www : ? :: nginx: worker process
  10. root : pts/ :: grep nginx

Ansible 笔记 (3) - 编写 playbook的更多相关文章

  1. ansible笔记(11):初识ansible playbook(二)

    ansible笔记():初识ansible playbook(二) 有前文作为基础,如下示例是非常容易理解的: --- - hosts: test211 remote_user: root tasks ...

  2. ansible笔记(10):初识ansible playbook

    ansible笔记():初识ansible playbook 假设,我们想要在test70主机上安装nginx并启动,我们可以在ansible主机中执行如下3条命令 ansible test70 -m ...

  3. ansible笔记(8):初识ansible playbook

    回顾总结:我们来想象一个工作场景,看看怎样把之前的知识点应用到这个工作场景中.假设,我们想要在192.168.10.2主机上安装nginx并启动,我们可以在ansible控制主机中执行如下3条命令. ...

  4. ansible笔记(12):handlers的用法

    ansible笔记():handlers的用法 这篇文章会介绍playbook中handlers的用法. 在开始介绍之前,我们先来描述一个工作场景: 当我们修改了某些程序的配置文件以后,有可能需要重启 ...

  5. ansible笔记(1)在centos中安装ansible

    ansible笔记():ansible的基本概念 一些基础概念 ansible是什么? 它是一个"配置管理工具",它是一个"自动化运维工具",如果你没有使用过任 ...

  6. Hadoop学习笔记(5) ——编写HelloWorld(2)

    Hadoop学习笔记(5) ——编写HelloWorld(2) 前面我们写了一个Hadoop程序,并让它跑起来了.但想想不对啊,Hadoop不是有两块功能么,DFS和MapReduce.没错,上一节我 ...

  7. ansible笔记(7):常用模块之系统类模块

    ansible笔记():常用模块之系统类模块 cron模块 cron模块可以帮助我们管理远程主机中的计划任务,功能相当于crontab命令. 在了解cron模块的参数之前,先写出一些计划任务的示例,示 ...

  8. ansible笔记(8):常用模块之系统类模块(二)

    ansible笔记():常用模块之系统类模块(二) user模块 user模块可以帮助我们管理远程主机上的用户,比如创建用户.修改用户.删除用户.为用户创建密钥对等操作. 此处我们介绍一些user模块 ...

  9. ansible笔记(9):常用模块之包管理模块

    ansible笔记():常用模块之包管理模块 yum_repository模块 yum_repository模块可以帮助我们管理远程主机上的yum仓库. 此处我们介绍一些yum_repository模 ...

随机推荐

  1. vue深入了解组件——自定义事件

    一.事件名 跟组件和prop不同,事件名不存在任何自动化的大小写转换.而是触发的事件名需要完全匹配监听这个事件所用的名称.举个例子,如果重复啊一个camelCase名字的事件: this.$emit( ...

  2. ARMV7,ARMV8

    ARMV7是32位,2011年出了ARMV8,是64位架构,IPHONE5S以上都是64位架构,说明是使用ARMV8??

  3. 调整Mic音量

    uses  MMSystem; function GetLineInHandle(AudioType : integer) : integer;var  i : integer;  AudioCaps ...

  4. mysql 新增数据

  5. docker 配置远程访问证书验证

    centos7 生成证书 工具:openssl #cd /etc/docker   (docker的证书一般放这) #openssl genrsa -aes256 -passout pass:密码   ...

  6. Python frozenset() 函数

    Python frozenset() 函数  Python 内置函数 描述 frozenset() 返回一个冻结的集合,冻结后集合不能再添加或删除任何元素. 语法 frozenset() 函数语法: ...

  7. ECMAScript5之JSON对象属性的遍历顺序

    测试浏览器 Chrome.Safari 一 键可以用parseInt解析成整数的,按数值升序顺序. var intObj = { '3.3' : 3.3, '2' : 222, '1' :111 } ...

  8. Monkey原理初步和改良优化--Android自动化测试学习历程

    章节:自动化基础篇——Monkey原理初步和改良优化(第三讲) 主要讲解内容与笔记: 一.理论知识: 直接看文档,来了解monkey的概念.基本原理,以及如何使用. First,what is And ...

  9. windows系统如何真正隐藏文件夹[转载]

    方法一(推荐)eg:现需隐藏e盘bak目录下的tools文件夹e:\bak\tools运行:cmd键入:attrib +s +a +h +r e:\bak\tools然后,你再进去看e盘bak目录下, ...

  10. jquery-jsonp插件解决跨域问题

    用jquery-jsonp插件解决ajax跨域问题,既可以实现ajax同样的请求效果,而且server服务端的相关代码也不用做任何改变. 代码如下: var url="http://loca ...