云计算架构学习之Ansible-playbook实战、Ansible-流程控制、Ansible-字典循环-roles角色

一、Ansible-playbook实战

1.Ansible-playbook安装软件

```bash

#编写yml

root@ansible ansible# cat wget.yml

  • hosts: backup

tasks:

  • name: Install wget

yum:

name: wget

state: present

#检查playbook的语法

root@ansible ansible# ansible-playbook --syntax-check wget.yml

playbook: wget.yml

#执行playbook

root@ansible ansible# ansible-playbook wget.yml

2.Playbook重构backup服务

```bash

1.定义主机清单

root@ansible ansible# cat /etc/ansible/hosts

nfs ansible_ssh_host=10.0.0.31

backup ansible_ssh_host=10.0.0.41

2.写playbook重构

root@ansible ansible# cat backup.yml

  • hosts: backup

tasks:

  • name: Install Rsync Server

yum:

name: rsync

state: present

  • name: Configure Rsync Server

copy:

src: rsyncd.conf

dest: /etc/rsyncd.conf

  • name: Create www Group

group:

name: www

gid: 666

  • name: Create User www

user:

name: www

uid: 666

group: www

shell: /sbin/nologin

create_home: false

  • name: Configure passwd file

copy:

content: rsync_backup:123456

dest: /etc/rsync.passwd

mode: 0600

  • name: Create Dir /backup

file:

path: /backup

state: directory

owner: www

group: www

  • name: Start Rsync Server

systemd:

name: rsyncd

state: started

enabled: yes

3.playbook重构nfs服务

```bash

1.定义主机清单

root@ansible ansible# cat /etc/ansible/hosts

nfs ansible_ssh_host=10.0.0.31

backup ansible_ssh_host=10.0.0.41

2.打通免秘钥

root@ansible \~# ssh-copy-id 10.0.0.31

3.写playbook

root@ansible ansible# cat nfs.yml

  • hosts: nfs

tasks:

  • name: Install NFS Server

yum:

name: nfs-utils

state: present

  • name: Configure nfs Server

copy:

src: exports

dest: /etc/

  • name: Create www Group

group:

name: www

gid: 666

  • name: Create User www

user:

name: www

uid: 666

group: www

shell: /sbin/nologin

create_home: false

  • name: Create /data/wp

file:

path: /data/wp

state: directory

owner: www

group: www

  • name: Start NFS Server

systemd:

name: nfs

state: started

enabled: yes

客户端挂载:

root@ansible ansible# cat web.yml

  • hosts: web01

tasks:

  • name: Install nfs-utils

yum:

name: nfs-utils

state: present

  • name: mount nfs /data/wp-->wordpress

mount:

src: 172.16.1.31:/data/wp

path: /code/wordpress/wp-content/uploads/

state: mounted

fstype: nfs

```

4.Playbook重构nginx-php

```bash

1.定义主机清单

root@ansible \~# cat /etc/ansible/hosts

nfs ansible_ssh_host=10.0.0.31

backup ansible_ssh_host=10.0.0.41

web01 ansible_ssh_host=10.0.0.7

web02 ansible_ssh_host=10.0.0.8

2.免秘钥

root@ansible \~# ssh-copy-id 10.0.0.8

3.playbook

root@ansible ansible# cat nginx.yml

  • hosts: web02

tasks:

  • name: Nginx Repo

yum_repository:

name: nginx

description: Nginx YUM repo

baseurl: http://nginx.org/packages/centos/7/$basearch/

gpgcheck: no

enabled: yes

  • name: Install Nginx Server

yum:

name: nginx

state: present

  • name: Configure Nginx Server

copy:

src: nginx.conf

dest: /etc/nginx/

  • name: create group www

group:

name: www

gid: 666

  • name: Create www user

user:

name: www

uid: 666

group: www

shell: /sbin/nologin

create_home: false

  • name: Start Nginx Server

systemd:

name: nginx

state: started

enabled: yes

5.Playbook重构mariadb

root@ansible ansible# cat php.yml

  • hosts: web02

tasks:

  • name: Install PHP Server

yum:

name: php,php-bcmath,php-cli,php-common,php-devel,php-embedded,php-fpm,php-gd,php-intl,php-mbstring,php-mysqlnd,php-opcache,php-pdo,php-process,php-xml,php-json

state: present

  • name: Configure PHP Server

copy:

src: www.conf

dest: /etc/php-fpm.d/

  • name: Start PHP Server

systemd:

name: php-fpm

state: started

enabled: yes

6.整合playbook文件

root@ansible ansible# cat mysql.yml

  • hosts: db01

tasks:

  • name: Install mariadb Server

yum:

name: mariadb-server,python3-mysqlclient

state: present

  • name: Start mariadb Server

systemd:

name: mariadb

state: started

enabled: yes

  • name: copy all.sql to 51

copy:

src: all.sql

dest: /root/

  • name: Configure Mmriadb Server

mysql_db:

login_user: root

login_host: localhost

login_port: 3306

name: all

target: /root/all.sql

state: import

  • name: Restart mariadb

systemd:

name: mariadb

state: restarted

```

07.部署wordpress

```bash

root@ansible ansible# cat wp.yml

  • hosts: web02

tasks:

  • name: Delete Default default.conf

file:

path: /etc/nginx/conf.d/default.conf

state: absent

  • name: Copy wp.conf

copy:

src: wp.conf

dest: /etc/nginx/conf.d/

  • name: unarchive wp.tar.gz

unarchive:

src: wp.tar.gz

dest: /

creates: /code/wordpress

  • name: Restart Nginx Server

systemd:

name: nginx

state: restarted

```

二、Ansible-流程控制

1.vars变量定义方法

2.vars变量定义方法

3.变量注册

4.when判断语法格式

5.when判断案例

6.handlers模块

7.nfs服务重构

三、Ansible-字典循环-roles角色

1.字典循环

2.tasks任务整合到一个文件

3.jinja2的循环和判断语法

4.rsync使用jinja2模版重构

5.Roles角色重新编排rsync

6.Roele角色重新编排nfs

相关推荐
星恒随风32 分钟前
Python 基础语法详解(一):从表达式、变量到数据类型
开发语言·笔记·python·学习
tedcloud1232 小时前
cc-switch评测:多AI Coding Agent管理工具详解
数据库·人工智能·sql·学习·自动化
胡图图不糊涂^_^3 小时前
测试BUG篇
学习·bug·测试
humors2215 小时前
学习方法的系统梳理与实践应用
学习·学习方法
爱讲故事的5 小时前
操作系统第一讲复习:为什么学习操作系统,以及操作系统到底在做什么?
linux·开发语言·windows·学习·ubuntu·c#
AKAMAI5 小时前
客户案例 | 重构部署体验,流媒体开源走向轻量化
开源·云计算
胡图图不糊涂^_^6 小时前
测试用例篇——设计测试用例的方法
笔记·学习·测试用例·判定表法·正交法生成用例测试·等价类·边界值
Fanfanaas7 小时前
C++ 继承
java·开发语言·jvm·c++·学习·算法
飞翔中文网7 小时前
Java学习笔记之抽象类
java·笔记·学习
Esaka_Forever8 小时前
few‑shot learning(少样本学习)
人工智能·学习