Ansible 基于Python开发
不适合没技术的人观看
操作无任何坑 后期可以配合zabbix 自动发现(自动注册)关联模板 实施一键完成部署
1 目录结构解释
ls /etc/ansible/
ansible.cfg 主配
hosts 主机清单
playbook.yaml 可以是任意名字 .yam yaml结尾
reles 定义角色目录可以是任何名字
└── roles
├── nginx.yaml
└── roles
└── nginx
├── files
│ ├── index.html
│ └── index.php
├── handlers
├── tasks
│ └── main.yaml
├── templates
│ ├── php_server.conf.j2
│ ├── server.conf.j2
│ └── www.conf.j2
└── vars
└── main.yaml
files copy的文件 这里放的是主页
handlers 触发模板(定义触发内容 可以在里面触发)此操作没用触发器所以为空
tasks 主要的 yaml 操作
templates 存放模板 这里放的是配置文件 playbook 可以直接调用
vars 变量 全局变量可以定义到配置文件中
使用机器
使用三台机器
ansible 192.168.1.18 主机名 w18
web 192.168.1.19 主机名 w19
web1 192.168.1.20 主机名 w20
本地hosts
vim /etc/hosts
192.168.1.19 web
192.168.1.20 web1
anisble hosts
vim /etc/ansible/hosts
[webs]
web aisible_ssh_pass=“1”
web1 aisible_ssh_pass=“1”
web 走静态 web1走动态
cat roles/nginx/tasks/main.yam
-
name: install epel-release
yum: name=epel-release.noarch state=present -
name: install nginx
yum: name=nginx state=present -
name: install php-fpm
yum: name=php-fpm state=present
when: ansible_hostname == ‘w19’ -
name: install php-fpm_conf
template: src=www.conf.j2 dest=/etc/php-fpm.d/www.conf
when: ansible_hostname == ‘w19’ -
name: install http_server_conf
template: src=server.conf.j2 dest=/etc/nginx/nginx.conf
when: ansible_hostname == ‘w20’ -
name: install php_server_conf
template: src=php_server.conf.j2 dest=/etc/nginx/nginx.conf
when: ansible_hostname == ‘w19’ -
name: mkdir directory
file: name={{ index_root }} state=directory -
name: cp file
copy: src=index.html dest={{ index_root }}
when: ansible_hostname == ‘w20’ -
name: cp php_file
copy: src=index.php dest={{ index_root }}
when: ansible_hostname == ‘w19’ -
name: start nginx
service: name=nginx state=started -
name: start php-fpm
service: name=php-fpm state=started
when: ansible_hostname == ‘w19’
cat roles/nginx/files/index.html
jun nginx
cat roles/nginx/files/index.php
<?php phpinfo(); ?>cat roles/nginx/vars/main.yaml
index_root: /web1/
变量根据自己需要 可以定义到配置文件中
ls roles/nginx/templates/
php_server.conf.j2 server.conf.j2 www.conf.j2
php_server.conf.j2 为 web 动态的 nginx 配置文件
server.conf.j2 为web1 静态的nginx配置文件
www.conf.j2 为 web 动态的php 配置文件
可以根据 需求 定义 vars 下的变量 和更改配置文件内容
PHP 版本为 PHP 5.4.16
nginx 版本为 nginx/1.16.1
子配置文件 有点多就不放了 需要留言
一起学习