目录
ansible | 10.0.0.10 |
lb-server | 10.0.0.11 |
web01 | 10.0.0.15 |
web02 | 10.0.0.16 |
redis | 10.0.0.20 |
目录结构、主机清单
[root@ansible ~]# mkdir -p ansible/roles/
[root@ansible ~]# cd ansible/roles/
[root@ansible roles]# mkdir -p {nginx,php-fpm,redis}/{tasks,templates,handlers,file
[root@ansible roles]# mkdir -p basic/{tasks,templates,handlers,files}
[root@ansible roles]# mkdir group_vars
[root@ansible roles]# egrep "^[a-Z]" ansible.cfg
inventory = ./hosts
forks = 5
[root@ansible roles]# grep "^[^#]" hosts
[lb-server]
10.0.0.11
[webserver]
10.0.0.15
10.0.0.16
[redis]
10.0.0.20
【1】安装基础环境
[root@ansible roles]# cat basic/tasks/main.yml
- name: Install Nginx Repo
yum_repository:
name: ansible_nginx
description: ansible_nginx_repo
baseurl: http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck: no
gpgkey: https://nginx.org/keys/nginx_signing.key
when: ( ansible_hostname is match ("web*") or ansible_hostname is match ("lb*") )
- name: PHP Reop
yum_repository:
name: ansible_php
description: ansible_php_repo
baseurl: http://us-east.repo.webtatic.com/yum/el7/x86_64/
gpgcheck: no
when: ( ansible_hostname is match ("web*") )
- name: epel
yum:
name: epel-release.noarch
state: present
- name: Install Basices
yum:
name: "{
{ item }}"
state: present
loop:
- vim
- wget
- make
- automake
- lrzsz
- tree
- net-tools
- nfs-utils
- rsync
- ntpdate
- unzip
- zip
- name: Disabled Firewall
systemd:
name: firewalld
state: stopped
enabled: no
- name: Disabled Selinux
selinux:
state: disabled
- name: Create Group
group:
name: "{
{ group }}"
gid: "{
{ id }}"
- name: Create User
user:
name: "{
{ user }}"
uid: "{
{ id }}"
group: "{
{ id }}"
shell: /sbin/nologin
create_home: no
- name: Code Dir
file:
path: "{
{ Code_path }}"
owner: "{
{ user }}"
group: "{
{ group }}"
state: directory
mode: '0755'
recurse: yes
[root@ansible roles]# cat kod.yml
- hosts: all
roles:
- role: basic
【2】webserver - 安装nginx
- tasks任务
- name: Install Nginx
yum:
name: nginx
state: present
- name: MV Default
shell:
cmd: mv /etc/nginx/conf.d/default.conf{,.bak}
creates: /etc/nginx/conf.d/default.conf.bak
- name: Configure Nginx-conf
template:
src: "{
{ item.src }}"
dest: "{
{ item.dest }}"
loop:
- { src: 'nginx.conf.j2', dest: '/etc/nginx/nginx.conf' }
notify: Restart Nginx
- name: Start Nginx
systemd:
name: nginx
state: started
enabled: yes
- 触发器
[root@ansible roles]# cat nginx/handlers/main.yml
- name: Restart Nginx
systemd:
name: nginx
state: restarted
- 变量文件
[root@ansible roles]# cat group_vars/all
## Basices
group: "www"
id: "666"