1.playbook?playbook翻译过来就是“剧本”,那playbook组成如下

  1. play: 定义的是主机的角色
  2. task: 定义的是具体执行的任务
  3. playbook: 由一个或多个play组成,一个play可以包含多个task任务
  4. 简单理解为: 使用不同的模块完成一件事情
  5. 通俗理解playbook?
  6. - playbook 剧本 <---文件 YAML
  7. - play 找谁 明星 找那个主机 web01
  8. - task 做什么 干什么事情 yum copy service
  9. - 找多个明星,做多件事情
  10. - 找一个明星,做多件事情

2.playbook的优势

  1. 1.功能比ad-hoc更全
  2. 2.能很好的控制先后执行顺序, 以及依赖关系
  3. 3.语法展现更加的直观
  4. 4.ad-hoc无法持久使用,playbook可以持久使用

3.playbook的配置语法是由yaml语法描述的,扩展名是yaml

  1. 缩进
  2. YAML使用固定的缩进风格表示层级结构,每个缩进由两个空格组成, 不能使用tabs
  3. 冒号
  4. 以冒号结尾的除外,其他所有冒号后面所有必须有空格。
  5. 短横线
  6. 表示列表项,使用一个短横杠加一个空格。
  7. 多个项使用同样的缩进级别作为同一列表
  8. Playbook执行结果返回颜色状态:
  9. 红色: 表示有task执行失败或者提醒的信息
  10. 黄色:表示执行了且改变了远程主机状态
  11. 绿色:表示执行成功

4.使用playbook编写一个创建文件的yml

  1. 创建一个文件---》两种方法
  2. [root@manager project1]# cat f1.yml
  3. - hosts: webservers
  4. tasks:
  5. - name: Create New File
  6. file: path=/tmp/123.txt state=touch owner=root group=root mode=600
  7. - name: Create New File2
  8. file:
  9. path: /tmp/456.txt
  10. state: touch
  11. owner: root
  12. group: root
  13. mode: 0666

案例一、使用ansible安装并配置nfs服务

  1. ---》172.16.1.31 nfs
  2. ---》172.16.1.7 clinet
  3. ---》172.16.1.8 clinet
  4. #1.新增一台nfs服务器
  5. [root@manager project1]# cat hosts
  6. [nfsservers]
  7. 172.16.1.31
  8. [webservers]
  9. 172.16.1.7
  10. 172.16.1.8
  11. 下发公钥至存储服务器
  12. [root@manager project1]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.31
  13. #2.测试三台主机是否通
  14. [root@manager project1]# ansible all -m ping -i hosts
  15. #3.编写一个nfs-server的yml
  16. 1.安装nfs yum
  17. 2.配置nfs copy
  18. 3.初始化环境
  19. 用户 group user
  20. 目录 file
  21. 授权 file
  22. 4.启动服务 systemd
  23. [root@manager project1]# cat nfs_server.yml
  24. - hosts: nfsservers
  25. tasks:
  26. - name: Installed NFS Server
  27. yum:
  28. name: nfs-utils
  29. state: present
  30. - name: Configure NFS Server
  31. copy:
  32. src: ./file/exports.j2
  33. dest: /etc/exports
  34. owner: root
  35. group: root
  36. mode: 0644
  37. backup: yes
  38. - name: Create NFS Group www
  39. group:
  40. name: www
  41. gid: 666
  42. - name: Create NFS User www
  43. user:
  44. name: www
  45. group: www
  46. uid: 666
  47. create_home: no
  48. shell: /sbin/nologin
  49. - name: Create NFS Share Directory
  50. file:
  51. path: /ansible_data
  52. state: directory
  53. owner: www
  54. group: www
  55. mode: 0755
  56. recurse: yes
  57. - name: Systemd NFS Server
  58. systemd:
  59. name: nfs
  60. state: restarted
  61. enabled: yes
  62. #4.编写一个nfs-clinet的yml
  63. [root@manager project1]# cat nfs_client.yml
  64. - hosts: webservers
  65. tasks:
  66. - name: Mount NFS Server share directory
  67. mount:
  68. src: 172.16.1.31:/ansible_data
  69. path: /mnt
  70. fstype: nfs
  71. opts: defaults
  72. state: mounted

案例二、使用ansible安装并配置nginx服务

  1. 1.安装 yum
  2. 2.配置 copy
  3. 3.启动 systmd
  4. 4.触发重启 handlers
  5. [root@manager project1]# cat nginx.yml
  6. - hosts: webservers
  7. tasks:
  8. - name: Installed Nginx Server
  9. yum:
  10. name: nginx
  11. state: present
  12. - name: Configure Nginx Server
  13. copy:
  14. src: ./file/nginx.conf.j2
  15. dest: /etc/nginx/nginx.conf
  16. owner: root
  17. group: root
  18. mode: 0644
  19. backup: yes
  20. notify: Restart Nginx Server
  21. - name: Systmd nginx Server
  22. systemd:
  23. name: nginx
  24. state: started
  25. enabled: yes
  26. handlers:
  27. - name: Restart Nginx Server
  28. systemd:
  29. name: nginx
  30. state: restarted

案例三:使用AnsiblePlaybook方式构建LAP架构,具体操作步骤如下:

  1. 1.使用yum安装 httpdphpfirewalld 7.1 5.3
  2. 2.使用get_url下载http://fj.xuliangwei.com/public/index.php文件
  3. 3.启动httpdfirewalld、等服务
  4. 4.添加防火墙规则,放行http的流量
  5. #1.配置主机清单
  6. [root@manager project1]# cat hosts
  7. [nfsservers]
  8. 172.16.1.31
  9. [backupservers]
  10. 172.16.1.41
  11. [web:children]
  12. nfsservers
  13. backupservers
  14. [webservers]
  15. 172.16.1.7
  16. 172.16.1.8
  17. #2.lamp剧本具体配置
  18. [root@manager project1]# cat lamp.yml
  19. - hosts: web
  20. tasks:
  21. - name: Installed Httpd Server //1.安装httpd
  22. yum:
  23. name: httpd
  24. state: present
  25. - name: Installed PHP Server //2.安装PHP
  26. yum:
  27. name: php
  28. state: present
  29. - name: Configure Httpd WebSite //3.配置站点
  30. get_url:
  31. url: http://fj.xuliangwei.com/public/index.php
  32. dest: /var/www/html/index.php
  33. mode: 0644
  34. - name: Systemd Httpd Server //4.启动http服务
  35. systemd:
  36. name: httpd
  37. state: started
  38. - name: Systemd Firewalld Server //5.启动防火墙firewalld
  39. systemd:
  40. name: firewalld
  41. state: started
  42. - name: Configure Firewalld Rule //6.放行http
  43. firewalld:
  44. service: http
  45. state: enabled

案例四、搭建可道云网盘 31 41 apache+php

  1. 1.安装 apache+php
  2. 2,下载代码
  3. 3.启动 systemd
  4. 4.下载代码 wget 解压
  5. [root@manager project1]# cat kod.yml
  6. - hosts: web
  7. tasks:
  8. - name: Installed Httpd Server
  9. yum:
  10. name: httpd
  11. state: present
  12. - name: Installed PHP Server
  13. yum:
  14. name: php
  15. state: present
  16. - name: Get kodcloud code
  17. synchronize:
  18. src: ./file/kod
  19. dest: /var/www/html/kodcloud
  20. - name: Chmod kodcloud
  21. file:
  22. path: /var/www/html
  23. owner: root
  24. group: root
  25. mode: 0777
  26. recurse: yes
  27. - name: Systemd Httpd Server
  28. systemd:
  29. name: httpd
  30. state: restarted

案例五: Nginx+PHP 搭建可道云

  1. 先手动实现,其次再写剧本
  2. - 1.配置yum nginx php
  3. - 2.安装软件包 (循环的方式)
  4. - nginx php71w
  5. - 3.创建用户 www 统一UIDGID
  6. - 4.配置nginx.conf配置文件,修改启用用户为www
  7. - 5.配置php的权限 /etc/php-fpm.d/www.conf
  8. - 6.添加虚拟主机 /etc/nginx/conf.d/xx.conf
  9. - 7.创建网站的站点目录
  10. - 8.传输代码至站点目录
  11. - 9.启动nginxphp
  12. - 10.修改配置还需要能够实现自动重启
  13. 剧本配置如下:
  14. [root@manager project1]# cat lnp.yml
  15. - hosts: webservers
  16. tasks:
  17. #1.配置nginx源
  18. - name: Installed Nginx repo
  19. yum_repository:
  20. name: nginx
  21. description: nginx repo
  22. baseurl: http://nginx.org/packages/centos/$releasever/$basearch/
  23. gpgcheck: no
  24. #2.配置php源
  25. - name: Installed PHP repo
  26. yum_repository:
  27. name: php
  28. description: webtatic-php
  29. baseurl: http://192.168.0.128/php
  30. gpgcheck: no
  31. #3.安装nginx和php软件
  32. - name: Installed Nginx and PHP Packages
  33. yum:
  34. name: "{{ packages }}"
  35. vars:
  36. packages:
  37. - nginx
  38. - php71w
  39. - php71w-cli
  40. - php71w-common
  41. - php71w-devel
  42. - php71w-gd
  43. - mod_php71w
  44. - php71w-fpm
  45. - php71w-opcache
  46. #4.创建属组www
  47. - name: Create Group www
  48. group:
  49. name: www
  50. gid: 666
  51. #5.创建属主www
  52. - name: Create User www
  53. user:
  54. name: www
  55. group: www
  56. uid: 666
  57. create_home: no
  58. shell: /sbin/nologin
  59. #6.管理nginx配置文件
  60. - name: Configure Nginx.conf
  61. copy:
  62. src: ./file/nginx.conf.j2
  63. dest: /etc/nginx/nginx.conf
  64. notify: Restart Nginx Server
  65. #7.管理php-fpm配置文件
  66. - name: Configure php-fpm.conf
  67. copy:
  68. src: ./file/php-www.conf.j2
  69. dest: /etc/php-fpm.d/www.conf
  70. notify: Restart PHP-fpm Server
  71. #8.创建虚拟主机
  72. - name: Add Nginx VirtHost kod.cheng.com
  73. copy:
  74. src: ./file/kod.cheng.com.conf.j2
  75. dest: /etc/nginx/conf.d/kod.cheng.com.conf
  76. notify: Restart Nginx Server
  77. #9.创建站点目录
  78. - name: Init Nginx BseEnv
  79. file:
  80. path: /code
  81. state: directory
  82. owner: www
  83. group: www
  84. recurse: yes
  85. #10.同步代码至站点目录
  86. - name: Push kodcloud code
  87. synchronize:
  88. src: ./file/kod
  89. dest: /code
  90. #11.授权站点目录权限
  91. - name: Chmod kodcloud
  92. file:
  93. path: /code
  94. owner: www
  95. group: www
  96. mode: 0777
  97. recurse: yes
  98. #12.启动nginx服务
  99. - name: Systemd Nginx Server
  100. systemd:
  101. name: nginx
  102. state: started
  103. enabled: yes
  104. #13.启动php-fpm
  105. - name: Systemd PHP-fpm Server
  106. systemd:
  107. name: php-fpm
  108. state: started
  109. enabled: yes
  110. handlers:
  111. - name: Restart Nginx Server
  112. systemd:
  113. name: nginx
  114. state: restarted
  115. - name: Restart PHP-fpm Server
  116. systemd:
  117. name: php-fpm
  118. state: restarted

6.模块练习:

  1. 1.安装httpd服务 yum
  2. 2.编写简单网页测试内容 copy
  3. 3.启动服务并加入开机自启 service|systemd
  4. 4.放行对应的端口 firewalld

2.Ansible Playbook剧本的更多相关文章

  1. ansible基础-playbook剧本的使用

    ansible基础-playbook剧本的使用 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.YAML概述 1>.YAML的诞生 YAML是一个可读性高,用来表达数据序 ...

  2. ansible的playbook剧本

    一.playbook剧本介绍 1)playbook介绍 Playbooks是Ansible的配置,部署和编排语言.它们可以描述您希望远程系统执行的策略,或一般IT流程中的一组步骤. 如果说ansibl ...

  3. Ansible之playbook剧本

    Ansible之playbook剧本 目录 Ansible之playbook剧本 1. playbook的组成 2. 剧本示例test1 2.1 剧本制作 2.2 准备http.conf 2.3 运行 ...

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

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

  5. Ansible Playbook 简介

    我们去远程执行命令时要使用 command 模块,拷贝文件时要使用 copy 模块,如果我们要操作的东西很多,那就要执行很多条不同模块的命令Playbook 是一个 yaml 配置文件,我们可以把不同 ...

  6. Ansible playbook基础组件介绍

    本节内容: ansible playbook介绍 ansible playbook基础组件 playbook中使用变量 一.ansible playbook介绍 playbook是由一个或多个“pla ...

  7. ansible入门四(Ansible playbook基础组件介绍)

    本节内容: ansible playbook介绍 ansible playbook基础组件 playbook中使用变量 一.ansible playbook介绍 playbook是由一个或多个“pla ...

  8. ansible playbook模式及语法

    一.什么是playbook及其组成 什么是playbook playbook 翻译过来就是"剧本" playbook的组成 play:定义的是主机的角色 task:定义的是具体执行 ...

  9. Playbook剧本小结

    1.Playbook剧本小结 1.什么是playbook,playbook翻译过来就是"剧本",那playbook组成如下 play: 定义的是主机的角色task: 定义的是具体执 ...

随机推荐

  1. 不给糖果就捣乱,用Python绘制有趣的万圣节南瓜怪【华为云分享】

    关于万圣节 万圣节又叫诸圣节,在每年的11月1日,是西方的传统节日:而万圣节前夜的10月31日是这个节日最热闹的时刻.在中文里,常常把万圣节前夜(Halloween)讹译为万圣节(All Saints ...

  2. 还在用背单词App?使用Python开发英语单词自测工具,助你逆袭单词王!

    学英语广告 最近也许是刚开学的原因,不管是公众号,还是刷抖音,导出都能看到关于学英语.背单词的广告. 不知道现在学生们背单词买的什么辅导材料.反正我们上学那会,<星火阅读>特别的火.记得当 ...

  3. h5-面试题

    干货!各种常见布局实现+知名网站实例分析 前端面试考点多?看这些文章就够了(2019年6月更新版) 前端面试:这50个经典前端面试题面试者必看! Vue面试中,经常会被问到的面试题/Vue知识点整理 ...

  4. 压缩感知重构算法之OMP算法python实现

    压缩感知重构算法之OMP算法python实现 压缩感知重构算法之CoSaMP算法python实现 压缩感知重构算法之SP算法python实现 压缩感知重构算法之IHT算法python实现 压缩感知重构 ...

  5. .Net Core使用分布式缓存Redis:数据结构

    一.前言 本篇主要使用StackExchangeRedis在.Net Core中使用Redis,使用基础见:点击此处. 二.五种基础数据结构 1.字符串类型String 字符串类型是Redis中最基本 ...

  6. servlet读取请求参数后流失效的问题

    在用reset接口的时候,常常会使用request.getInputStream()方法,但是流只能读取一次,一旦想要加上一个过滤器用来检测用户请求的数据时就会出现异常. 在过滤器中通过流读取出用户p ...

  7. NTFS在openwrt下的挂载问题

    在openwrt上市可以挂载ntfs分区的,但是如果原来如果搞过win,或者异常关机,那么会遇到以下的错误: root@Openwrt:/etc/config# mount -t ntfs -o rw ...

  8. HPS端的GPIO如何控制

    该笔记主要记录HPS端的GPIO如何控制,包括控制LED和Key 1.GPIO地址映射 Peripheral Base Address 0xf000_0000 64M 2.HPS外设 (1)GPIO ...

  9. Android WebView 基本设置与H5 交互

    mWebView.setDrawingCacheEnabled(true); WebChromeClient webChromeClient = new WebChromeClient(); mWeb ...

  10. Sql中将datetime转换成字符串的方法(CONVERT())

    语法格式:CONVERT (<data_ type>[ length ], <expression> [, style]) style的含义:style 是将DATATIME ...