目录
一、playbook 剧本安装nginx
二、roles(难点)
1、roles介绍
2、创建roles的目录结构
3、案例
4、练习:
[root@m0 ~]# mkdir /etc/ansible/playbook
[root@m0 ~]# vim /etc/ansible/playbook/nginx.yml
---
- hosts: group02
remote_user: root
tasks:
- name: 卸载httpd
yum: name=httpd state=absent
- name: 安装epel
yum: name=epel-release state=present
- name: 安装nginx
yum: name=nginx state=present
- name: 修改资源文件
shell: echo 'i am nginx,port is 80' > /usr/share/nginx/html/index.html
- name: 修改端口
command: sed -i '/listen/s/80/8080/g' /etc/nginx/nginx.conf
notify:
- restart nginx
- name: 启动服务
service: name=nginx state=started enabled=yes
handlers:
- name: restart nginx
service: name=nginx state=restarted
[root@m0 ~]# ansible-playbook /etc/ansible/playbook/nginx.yml
roles(⻆⾊): 就是通过分别将variables, tasks及handlers等放置于单独的⽬录中,并可以便捷地调⽤它们的⼀种机制。
假设我们要写⼀个playbook来安装管理lamp环境,那么这个playbook就会写很⻓。所以我们希望把这个很⼤的⽂件分成多个功能拆分, 分成apache管理,php管理,mysql管理,然后在需要使⽤的时候直接调⽤就可以了,以免重复写。就类似编程⾥的模块化的概念,以达到代码复⽤的效果。
files:⽤来存放由copy模块或script模块调⽤的⽂件。
tasks:⾄少有⼀个main.yml⽂件,定义各tasks。
handlers:有⼀个main.yml⽂件,定义各handlers。
templates:⽤来存放jinjia2模板。
vars:有⼀个main.yml⽂件,定义变量。
meta:有⼀个main.yml⽂件,定义此⻆⾊的特殊设定及其依赖关系。
注意: 在每个⻆⾊的⽬录中分别创建files,tasks,handlers,templates,vars和meta⽬录,⽤不到的⽬录可以创建为空⽬录.
[root@m0 ~]# cd /etc/ansible/roles/
[root@m0 roles]# ls
[root@m0 roles]# mkdir nginx
[root@m0 roles]# ls
nginx
[root@m0 roles]# cd nginx/
[root@m0 nginx]# mkdir files
[root@m0 nginx]# mkdir tasks
[root@m0 nginx]# mkdir handlers
[root@m0 nginx]# mkdir templates
[root@m0 nginx]# mkdir vars
[root@m0 nginx]# mkdir meta
[root@m0 nginx]# tree
.
├── files
├── handlers
├── meta
├── tasks
├── templates
└── vars
[root@m0 nginx]# touch handlers/main.yml
[root@m0 nginx]# touch tasks/main.yml
[root@m0 nginx]# touch vars/main.yml
[root@m0 nginx]# touch meta/main.yml
[root@m0 nginx]# tree
.
├── files
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── tasks
│ └── main.yml
├── templates
└── vars
└── main.yml
[root@m0 ~]# vim /etc/ansible/playbook/test002.yml
---
- hosts: s
remote_user: root
roles:
- nginx
[root@m0 ~]# ansible-playbook /etc/ansible/playbook/test002.yml
[root@m0 ~]# ansible s -m yum -a 'name=nginx state=absent'
[root@m0 ~]# vim /etc/ansible/roles/nginx/tasks/main.yml
---
- name: 卸载httpd
yum: name=httpd state=absent
- name: 卸载nginx
yum: name=nginx state=absent
- name: 安装nginx
yum: name=nginx state=present
- name: 启动服务
service: name=nginx state=started enabled=yes
...
[root@m0 ~]# ansible-playbook /etc/ansible/playbook/test002.yml
[root@m0 ~]# vim /etc/ansible/playbook/test002.yml
---
- hosts: s
remote_user: root
roles:
- nginx
- mysql
[root@m0 ~]# cp /etc/ansible/roles/nginx/ /etc/ansible/roles/mysql -r
[root@m0 ~]# ansible-playbook /etc/ansible/playbook/test002.yml
1.创建roles⽬录及⽂件,并确认⽬录结构
[root@m0 ~]# cd /etc/ansible/roles/
[root@m0 roles]# tree
.
└── httpd
├── files
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── tasks
│ └── main.yml
├── templates
└── vars
└── main.yml
2.准备httpd服务器的主⻚⽂件,php测试⻚和配置⽂件等
[root@m0 ~]# yum -y install httpd
[root@m0 roles]# echo "" > /etc/ansible/roles/httpd/files/test.php
[root@m0 roles]# echo "test main page" > /etc/ansible/roles/httpd/files/index.html
[root@m0 roles]# cp /etc/httpd/conf/httpd.conf /etc/ansible/roles/httpd/files/
3.编写httpd⻆⾊的main.yml⽂件
[root@m0 roles]# vim /etc/ansible/roles/httpd/tasks/main.yml
---
- name: 安装httpd
yum: name=httpd,httpd-devel state=present
- name: 同步httpd配置文件
copy: src=/etc/ansible/roles/httpd/files/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify: restart httpd
- name: 同步主页文件
copy: src=/etc/ansible/roles/httpd/files/index.html dest=/var/www/html/index.html
- name: 同步php测试页
copy: src=/etc/ansible/roles/httpd/files/test.php dest=/var/www/html/test.php
- name: 启动httpd并开机自启动
service: name=httpd state=started enabled=yes
4.编写httpd⻆⾊⾥的handler
[root@m0 ansible]# vim /etc/ansible/roles/httpd/handlers/main.yml
---
- name: restart httpd
service: name=httpd state=restarted
5.将两台主机添加到组
[root@m0 ~]# vim /etc/ansible/hosts
s1 ansible_ssh_host=192.168.1.68 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=q
s2 ansible_ssh_host=192.168.1.69 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=q
[s]
s1
s2
6.编写httpd的playbook⽂件调⽤前⾯定义好的⻆⾊
[root@m0 roles]# vim /etc/ansible/playbook/httpd.yaml
---
- hosts: s
remote_user: root
roles:
- httpd
7.执行playbook文件
[root@m0 roles]# ansible-playbook /etc/ansible/playbook/httpd.yaml //执行