1. 什么是playbook,playbook翻译过来就是剧本,那playbook组成如下
playbook:定义一个文本文件,以yml为后缀结尾
play:定义的是主机的角色
总结:playbook是由一个或多个play组成,一个play可以包含多个task任务
可以理解为使用不同的模块共同完成一件事情
2.ansible playbook与AD-Hoc的关系
1.playbook是对AD-Hoc的一种编排方式
2.playbook可以持久运行,而AD-Hoc只能临时运行
3.playbook适合做复杂的任务,而AD-hoc适合做快速简单的任务
4.playbook能控制任务执行的先后顺序
3.playbook三板斧:缩进,冒号,短横线
示例:
1.使用playbook编写一个创建文件的yml
[root@manager project1]# cat f1.yml
- hosts: webservers
tasks:
- name: Create New File
file: path=/tmp/123.txt state=touch owner=root group=root mode=600
- name: Create New File2
file:
path: /tmp/456.txt
state: touch
owner: root
group: root
mode: 0666
案例一、使用ansible安装并配置nfs服务
#172.16.1.31 nfs
#172.16.1.7 clinet
#172.16.1.8 clinet
#1.新增一台nfs服务器
[root@manager project1]# cat hosts
[nfsservers]
172.16.1.31
[webservers]
172.16.1.7
172.16.1.8
[root@manager project1]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
#2.测试三台主机是否通
[root@manager project1]# ansible all -m ping -i hosts
#3.编写一个nfs-server的yml
1.安装nfs yum
2.配置nfs copy
3.初始化环境
用户 group user
目录 file
授权 file
4.启动服务 systemd
[root@manager project1]# cat nfs_server.yml
- hosts: nfsservers
tasks:
- name: Installed NFS Server
yum:
name: nfs-utils
state: present
- name: Configure NFS Server
copy:
src: ./file/exports.j2
dest: /etc/exports
owner: root
group: root
mode: 0644
backup: yes
- name: Create NFS Group www
group:
name: www
gid: 666
- name: Create NFS User www
user:
name: www
group: www
uid: 666
create_home: no
shell: /sbin/nologin
- name: Create NFS Share Directory
file:
path: /ansible_data
state: directory
owner: www
group: www
mode: 0755
recurse: yes
- name: Systemd NFS Server
systemd:
name: nfs
state: restarted
enabled: yes
#4.编写一个nfs-clinet的yml
[root@manager project1]# cat nfs_client.yml
- hosts: webservers
tasks:
- name: Mount NFS Server share directory
mount:
src: 172.16.1.31:/ansible_data
path: /mnt
fstype: nfs
opts: defaults
state: mounted
案例二、使用ansible安装并配置nginx服务
1.安装 yum
2.配置 copy
3.启动 systmd
handlers
[root@manager project1]# cat nginx.yml
- hosts: webservers
tasks:
- name: Installed Nginx Server
yum:
name: nginx
state: present
- name: Configure Nginx Server
copy:
src: ./file/nginx.conf.j2
dest: /etc/nginx/nginx.conf
owner: root
group: root
mode: 0644
backup: yes
notify: Restart Nginx Server
- name: Systmd nginx Server
systemd:
name: nginx
state: started
enabled: yes
handlers:
- name: Restart Nginx Server
systemd:
name: nginx
state: restarted
案例三、使用Ansible playbook方式构建LAP架构。
1.使用yum安装httpd、php、firewalld
2.使用get_url下载[http://fj.xuliangwei.com/public/index.php](http://fj.xuliangwei.com/public/index.php)文件
3.启动httpd、firewalld、等服务
4.添加防火墙规则,放行http的流量
[root@manager project1]# cat hosts
[nfsservers]
172.16.1.31
[backupservers]
172.16.1.41
[web:children]
nfsservers
backupservers
[webservers]
172.16.1.7
172.16.1.8
#具体配置
[root@manager project1]# cat lamp.yml
- hosts: web
tasks:
- name: Installed Httpd Server
yum:
name: httpd
state: present
- name: Installed PHP Server
yum:
name: php
state: present
- name: Configure Httpd WebSite
get_url:
url: http://fj.xuliangwei.com/public/index.php
dest: /var/www/html/index.php
mode: 0644
- name: Systemd Httpd Server
systemd:
name: httpd
state: started
- name: Systemd Firewalld Server
systemd:
name: firewalld
state: started
- name: Configure Firewalld Rule
firewalld:
service: http
state: enabled
案例五、搭建可道云网盘
Nginx+PHP 搭建可道云
- 1.先手动实现
- 1.配置yum源 nginx php
- 2.安装软件包 (循环的方式)
- nginx php71w
- 3.创建用户 www 统一UID和GID
- 4.配置nginx.conf配置文件,修改启用用户为www
- 5.配置php的权限 /etc/php-fpm.d/www.conf
- 6.添加虚拟主机 /etc/nginx/conf.d/xx.conf
- 7.创建网站的站点目录
- 8.传输代码至站点目录
- 9.启动nginx和php
- 10.修改配置还需要能够实现自动重启
[root@m01 project1]# vim lnp.yml
- name: Installed Nginx repo
yum_repository:
name: nginx
description: nginx repos
baseurl: http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck: no
#2.配置yum源仓库 php
- name: Installed PHP repo
yum_repository:
name: webtatic-php
description: php repos
baseurl: http://us-east.repo.webtatic.com/yum/el7/x86_64/
gpgcheck: no
#3.安装nginx和php
- name: Installed Nginx and PHP Packages
yum:
name: "{{ packages }}"
vars:
packages:
- nginx
- php71w
- php71w-cli
- php71w-common
- php71w-devel
- php71w-gd
- mod_php71w
- php71w-fpm
- php71w-opcache
#4.创建程序启动的用户身份
- name: Create Group www
group:
name: www
gid: 666
- name: Create User www
user:
name: www
group: www
uid: 666
create_home: no
shell: /sbin/nologin
#5.管理nginx配置文件
- name: Configure nginx.conf
copy:
src: ./file/nginx.conf.j2
dest: /etc/nginx/nginx.conf
notify: Restart Nginx Server
#6.管理php-fpm配置文件
- name: Configure php-fpm.conf
copy:
src: ./file/php-www.conf.j2
dest: /etc/php-fpm.d/www.conf
notify: Restart PHP-FPM Server
#6.添加kodcloud虚拟主机(检测语法)
- name: Add Nginx VirtHost kod.oldxu.com
copy:
src: ./file/kold.oldxu.com.conf.j2
dest: /etc/nginx/conf.d/kold.oldxu.com.conf
notify: Restart Nginx Server
- name: Init Nginx BseEnv
file:
path: /code
state: directory
owner: www
group: www
recurse: yes
- name: Push KodCloud Code
synchronize:
src: ./file/kod
dest: /code/
- name: Chomod kodcloud
file:
path: /code
owner: www
group: www
mode: 0777
recurse: yes
- name: Systemd Nginx Server
systemd:
name: nginx
state: started
enabled: yes
- name: Systemd PHP-FPM Server
systemd:
name: php-fpm
state: started
enabled: yes
#当nginx或php配置文件发生变更才会触发此操作
handlers:
- name: Restart Nginx Server
systemd:
name: nginx
state: restarted
- name: Restart PHP-FPM Server
systemd:
name: php-fpm
state: restarted