文章目录
- hosts--ventory
- roles
- 执行命令
使用ansible向3台centos7服务器上安装nginx
hosts–ventory
[root@stand playhook1]# cat /root/HOSTS
# /root/HOSTS
[webservers]
192.168.196.111 ansible_ssh_pass=password
192.168.196.112 ansible_ssh_pass=password
192.168.196.113 ansible_ssh_pass=password
[docker]
192.168.196.111
192.168.196.112
192.168.196.113
[docker:vars]
ansible_ssh_pass=password
[ansible:children]
docker
roles
.
├── roles
│ └── nginx
│ ├── files
│ │ └── index.html
│ ├── handlers
│ │ └── main.yaml
│ ├── tasks
│ │ └── main.yaml
│ ├── templates
│ │ └── nginx.conf.j2
│ └── vars
│ └── main.yaml
└── site.yml
7 directories, 6 files
[root@stand playhook1]# cat roles/nginx/files/index.html
<html>
<head>
<title>Welcome to Nginx!</title>
</head>
<body>
<h1>Hello, Nginx is working!</h1>
</body>
</html>
[root@stand playhook1]# cat roles/nginx/handlers/main.yaml
---
- name: restart nginx
service:
name: nginx
state: restarted
[root@stand playhook1]# cat roles/nginx/templates/nginx.conf.j2
user nginx;
worker_processes {{ ansible_processor_cores }};
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
# Gzip Settings
gzip on;
gzip_disable "msie6";
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name {{ server_name }};
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
}
[root@stand playhook1]# cat roles/nginx/vars/main.yaml
---
ansible_processor_cores: 2
server_name: localhost
[root@stand playhook1]# cat site.yml
---
- hosts: webservers
roles:
- { role: nginx, version: "1.24.0" }
[root@stand playhook1]# cat roles/nginx/handlers/main.yaml
---
- name: restart nginx
service:
name: nginx
state: restarted
[root@stand playhook1]# cat roles/nginx/tasks/main.yaml
---
- name: Add Nginx repository
yum_repository:
name: nginx
description: Nginx Repository
baseurl: http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck: yes
enabled: yes
gpgkey: https://nginx.org/keys/nginx_signing.key
- name: Install nginx package
yum:
name: "nginx-{{ version }}"
state: present
- name: Ensure Nginx html directory exists
file:
path: /usr/share/nginx/html
state: directory
owner: nginx
group: nginx
mode: 0755
- name: Copy nginx.conf Template
template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
owner: root
group: root
backup: yes
mode: 0755
notify: restart nginx
- name: Copy index html
copy:
src: index.html
dest: /usr/share/nginx/html/index.html
owner: root
group: root
backup: yes
mode: 0755
- name: Make sure nginx service is running
service:
name: nginx
state: started
执行命令
ansible-playbook -i /root/HOSTS site.yml
测试:
显示已经成功。
大功告成