playbook剧本安装nginx、roles
一、playbook 剧本安装nginx
[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(难点)
1、roles介绍
roles(角色):就是通过分别将variables,tasks及handlers等放置于单独的目录中,并可以便捷地调用它
们的一种机制。
假设我们要写一个playbook来安装管理lamp环境,那么这个playbook就会写很长。所以我们希望把
这个很大的文件分成多个功能拆分,分成apache管理,php管理,mysql管理,然后在需要使用的时候直
接调用就可以了,以免重复写。就类似编程里的模块化的概念,以达到代码复用的效果。
2、创建roles的目录结构
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目录,用不到的目录
可以创建为空目录.
3、案例
[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
4、练习:
请使用role来实现Inmp
mariadb中创建eleme数据库
创建表t_user
id int 主键约束,自增 编号
username varchar 非空约束 账号
password varchar 非空约束 密码
remark Ivarchar 没有约束 账号权限说明
[root@m0 ~]# rpm -qa | grep mariadb
mariadb-libs-5.5.56-2.el7.x86_64
[root@m0 ~]# yum -y remove *mariadb*
[root@m0 ~]# yum -y install mariadb
[root@m0 ~]# yum -y install mariadb-server.x86_64
[root@m0 ~]# systemctl start mariadb.service
[root@m0 ~]# mysql
[root@m0 ~]# mysql << EOF
> create database if not exists eleme charset utf8mb4;
Query OK, 1 row affected (0.00 sec)
> use eleme;
MariaDB [eleme]> create table user(id int primary key auto_increment,username varchar(45) not null,password varchar(45) not null,remark varchar(45));
> exit
Bye
[root@m0 ~]# mysql
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| eleme |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> use eleme;
Database changed
MariaDB [eleme]> show tables;
+-----------------+
| Tables_in_eleme |
+-----------------+
| user |
+-----------------+
1 row in set (0.00 sec)
MariaDB [eleme]> select * from user;
Empty set (0.00 sec)
MariaDB [eleme]> exit
Bye
[root@m0 ~]# cd /etc/ansible/roles/
[root@m0 ~]# cd /etc/ansible/roles/
[root@m0 roles]# mkdir mariadb
[root@m0 roles]# cd mariadb/
[root@m0 mariadb]# mkdir tasks
[root@m0 mariadb]# touch tasks/main.yml
[root@m0 ~]# vim /etc/ansible/roles/mariadb/tasks/main.yml
---
- name: 卸载mariadb
yum: name=mariadb,mariadb-server,mariadb-devel state=absent
- name: 安装mariadb
yum: name=mariadb,mariadb-server,mariadb-devel state=present
- name: 启动mariadb服务
service: name=mariadb state=started enabled=yes
- name: 执行脚本,创建数据库和数据表
script: /etc/ansible/script/dbtable.sh
...
[root@m0 mariadb]# cd /etc/ansible/
[root@m0 mariadb]# mkdir script
[root@m0 mariadb]# cd script/
[root@m0 script]# vim dbtable.sh
#!/bin/bash
mysql << EOF
create database if not exists eleme charset utf8mb4;
use eleme;
create table user(id int primary key auto_increment,username varchar(45) not null,password varchar(45) not null,remark varchar(45));
quit
EOF
[root@m0 script]# vim /etc/ansible/playbook/test003.yml
---
- hosts: s
remote_user: root
roles:
- mariadb
[root@m0 script]# ansible-playbook /etc/ansible/playbook/test003.yml
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/m0_71589190/article/details/141435322