Ansible综合案例
架构图
分步剧本执行
初始配置
- 修改主机清单文件
[root@server1 ~]# vim /etc/ansible/hosts
[all_ip]
192.168.31.10
192.168.31.20
192.168.31.30
192.168.31.40
192.168.31.50
192.168.31.60
[all_hostname]
server1
server2
server3
server4
server5
server6
[nginx]
server2
[apache]
server3
server4
[mariadb]
server5
[nfs]
server6
- 配置ssh免密登录
[root@server1 ~]# ssh-keygen
[root@server1 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.31.10
[root@server1 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.31.20
[root@server1 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.31.30
[root@server1 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.31.40
[root@server1 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.31.50
[root@server1 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.31.60
主机名服务
- 创建hosts文件模板
[root@server1 ~]# vim /template/hosts.j2
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
{% for host in groups.all_ip %}
{{hostvars[hosts].ansible_ens33.ipv4.address}} {{hostvars[hosts].ansible_hostname}}
{% endfor %}
- 编写剧本
[root@server1 ~]# vim /playbook/hosts.yml
- name: Config hosts file
hosts: all_ip
remote_user: root
tasks:
- name: copy hosts.j2 to group servers
template:
src: /template/hosts.j2
dest: /etc/hosts
- 执行剧本
[root@server1 playbook]# ansible-playbook hosts.yml
- 测试连通性
[root@server1 ~]# ping server1
[root@server1 ~]# ping server2
[root@server1 ~]# ping server3
[root@server1 ~]# ping server4
[root@server1 ~]# ping server5
[root@server1 ~]# ping server6
更换阿里源
- 创建角色
[root@server1 roles]# ansible-galaxy init ali_yum_repo
更换阿里yum源
- 使用
file
模块把所有受控节点的/etc/yum.repos.d/
下的所有文件删除 - 再使用
copy
模块把准备好的.repo文件copy到所有受控节点
- 使用
[root@server1 roles]# vim ali_yum_repo/tasks/main.yml
---
# tasks file for ali_yum_repo
- name: Find files in yum.repos.d/*
find:
paths: /etc/yum.repos.d/
patterns: '*'
register: files_to_delete
- name: Remove original yum.repos.d/*
file:
path: "{{ item.path }}"
state: absent
with_items: "{{ files_to_delete.files }}"
- name: Copy aliyun yum.repo to all nodes
copy:
src: /etc/ansible/roles/ali_yum_repo/files/yum.repo
dest: /etc/yum.repos.d/yum.repo
- 编写剧本
[root@server1 roles]# vim yum_repo_role_use.yml
- name: Updata all nodes yum.repo file
hosts: all_hostname
roles:
- ali_yum_repo
- 执行剧本
[root@server1 roles]# ansible-playbook yum_repo_role_use.yml
部署Nginx
- 创建角色
[root@server1 roles]# ansible-galaxy init nginx
- 部署Nginx
[root@server1 roles]# vim nginx/tasks/main.yml
---
# tasks file for nginx
- name: yum install epel
yum:
name: epel-release.noarch
state: present
- name: yum install nginx
yum:
name: nginx
state: present
- name: stop firewalld
service:
name: firewalld
state: stopped
- name: start nginx
service:
name: nginx
state: restarted
enable: yes
- 编写剧本
[root@server1 roles]# vim nginx_install.yml
- name: install nginx
hosts: server2
roles:
- nginx
- 执行剧本
[root@server1 roles]# ansible-playbook nginx_install.yml
- 测试网站
[root@server1 ~]# curl server2 -I
HTTP/1.1 200 OK
Server: nginx/1.20.1
Date: Mon, 07 Mar 2022 15:00:51 GMT
Content-Type: text/html
Content-Length: 4833
Last-Modified: Fri, 16 May 2014 15:12:48 GMT
Connection: keep-alive
ETag: "53762af0-12e1"
Accept-Ranges: bytes
部署Apache
- 创建角色
[root@server1 roles]# ansible-galaxy init apache
- 部署Apache
[root@server1 roles]# vim apache/tasks/main.yml
---
# tasks file for apache
- name: Install lamp environment
yum:
name: httpd,php-fpm,php-mysql,mod_php
state: present
- name: Start httpd
service:
name: httpd
state: restarted
- name: Start php-fpm
service:
name: php-fpm
state: restarted
- 编写剧本
[root@server1 roles]# vim lamp_install.yml
- name: prepare lamp
hosts: apache
roles:
- apache
- 执行剧本
[root@server1 roles]# ansible-playbook lamp_install.yml
负载均衡
- 创建角色
[root@server1 roles]# ansible-galaxy init nginx_lb
- 创建负载均衡配置文件
[root@server1 roles]# vim nginx_lb/templates/lb.conf.j2
upstream webservers{
server server3;
server server4;
}
server{
listen 8080;
server_name 192.168.31.20:8080;
location / {
proxy_pass http://webservers;
}
}
- 拷贝配置文件
[root@server1 roles]# vim nginx_lb/tasks/main.yml
---
# tasks file for nginx_lb
- name: configure nginx lb conf file
template:
src: /etc/ansible/roles/nginx_lb/templates/lb.conf.j2
dest: /etc/nginx/conf.d/lb.conf
- name: restart nginx
service:
name: nginx
state: restarted
- 编写剧本
[root@server1 roles]# vim nginx_lb.yml
- name: configure nginx lb server
hosts: server2
roles:
- nginx_lb
- 执行剧本
[root@server1 roles]# ansible-playbook nginx_lb.yml
部署Mariadb
- 创建角色
[root@server1 roles]# ansible-galaxy init mariadb
- 部署Mariadb
[root@server1 roles]# vim mariadb/tasks/main.yml
---
# tasks file for mariadb
- name: yum install mariadb
yum:
name: mariadb-server
state: present
- name: stop firewalld
service:
name: firewalld
state: stopped
- name: start mariadb
service:
name: mariadb
state: restarted
- 编写剧本
[root@server1 roles]# vim mariadb_install.yml
- name: install mariadb
hosts: mariadb
roles:
- mariadb
- 运行剧本
[root@server1 roles]# ansible-playbook mariadb_install.yml
部署逻辑卷
- 创建角色
[root@server1 roles]# ansible-galaxy init lvm_create
- 使用
parted
模块创建分区,使用lvg
模块创建卷组,使用lvol
模块创建逻辑卷,再把逻辑卷挂载到本地文件夹
[root@server1 roles]# vim lvm_create/tasks/main.yml
---
# tasks file for lvm_create
- name: create partion # 创建分区
parted:
device: /dev/sdb
number: 1
flags: [ lvm ]
state: present
part_start: 1MiB
part_end: 2GiB
- name: create vg # 创建卷组
lvg:
vg: vg-nfs
pvs: /dev/sdb1
- name: create lvm 1G # 创建逻辑卷
lvol:
vg: vg-nfs
lv: lv-nfs
size: 1G
- name: format lv-nfs # 格式化
filesystem:
fstype: ext4
dev: /dev/vg-nfs/lv-nfs
- name: mkdir nfs dir # 创建挂载目录
file:
path: /nfs
state: directory
- name: mount lvm # 挂载逻辑卷
mount:
path: /nfs
src: /dev/vg-nfs/lv-nfs
fstype: ext4
state: mounted
opts: noatime
- 编写剧本
[root@server1 roles]# vim lvm_create.yml
- name: Create lvm for nfs
hosts: nfs
roles:
- lvm_create
- 执行剧本
[root@server1 ~]# ansible-playbook lvm_create.yml
- 在server6上检验
整体剧本执行
- server1中需要用到每一个角色的
tasks/main.yml
文件(定义任务列表),以及/template/hosts.j2
文件
[root@server1 roles]# ansible-galaxy list
# /usr/share/ansible/roles
# /etc/ansible/roles
- ali_yum_repo, (unknown version)
- nginx, (unknown version)
- nginx_lb, (unknown version)
- apache, (unknown version)
- mariadb, (unknown version)
- lvm_create, (unknown version)
- server2-server6都是纯净环境
- 配置ssh免密登录
[root@server1 ~]# ssh-keygen
[root@server1 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.31.10
[root@server1 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.31.20
[root@server1 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.31.30
[root@server1 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.31.40
[root@server1 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.31.50
[root@server1 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.31.60
- 编写剧本文件
[root@server1 roles]# vim all.yaml
- name: update hosts file # 更新hosts文件
hosts: all_ip
remote_user: root
tasks:
- name: copy hosts.j2 to group servers
template:
src: /template/hosts.j2
dest: /etc/hosts
- name: update yum.repo file # 换源
hosts: all_hostname
remote_user: root
tasks:
- include_tasks: /etc/ansible/roles/ali_yum_repo/tasks/main.yml
- name: install nginx # 部署nginx
hosts: nginx
remote_user: root
tasks:
- include_tasks: /etc/ansible/roles/nginx/tasks/main.yml
- name: install apache # 部署apache
hosts: apache
remote_user: root
tasks:
- include_tasks: /etc/ansible/roles/apache/tasks/main.yml
- name: install mariadb # 部署mariadb
hosts: mariadb
remote_user: root
tasks:
- include_tasks: /etc/ansible/roles/mariadb/tasks/main.yml
- name: configure nginx lb server # 负载均衡
hosts: nginx
remote_user: root
tasks:
- include_tasks: /etc/ansible/roles/nginx_lb/tasks/main.yml
- name: create lvm for nfs # 创建逻辑卷
hosts: nfs
remote_user: root
tasks:
- include_tasks: /etc/ansible/roles/lvm_create/tasks/main.yml
- 执行剧本
[root@server1 roles]# ansible-playbook all.yaml
只需要准备好角色以及它们的一些任务列表,然后再进行统一的剧本配置就可以只需要在一台机器上通过一个命令就可以完成部署,由此可见Ansible的自动化部署的强大性