本节结合前面所学的ansible命令和playbook语法做一个小的练习,实现在一台服务器上部署nginx.
playbook如下:
剧本如下:安装nginx 添加nginx用户 考本配置文件 添加index.html文件
ansible-playbook depoly_nginx.yml
depoly_nginx.yml
---
- hosts: nginx_server
become: yes
become_method: sudo
vars:
worker_processes: 4
worker_connections: 768
max_operi_files: 65506
tasks:
- name: install nginx
yum: name=nginx update_cache=yes state=latest
- name: useradd nginx
user: name=ngigx createhome=no comment='nginx_command'
- name: copy nginx config file
template: src=/etc/ansible/roles/nginx/nginx.conf.j2 dest=/etc/nginx/nginx.conf
notify: restart nginx
- name: copy index.html
template:
src: /etc/ansible/roles/nginx/index.html.j2
dest: /usr/share/nginx/www/index.html
mode: 644
notify: restart nginx
handlers:
- name: restart nginx
service: name=nginx state=restarted
nginx.conf.j2
worker_processes {{ worker_processes }};
worker_rlimit_nofile {{ max_open_files }};
events {
worker_connections {{ worker_connections}};
}
http{
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
listen 443 ssl;
root /usr/share/nginx/www;
index index.html index.htm;
server_name localhost;
location /{
try_files $uri $uri/ =404;
}
}
}
index.html.j2
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Wecome to Ansible</title>
<meta name="description" content="">
<meta name="keywords" content="">
<link href="" rel="stylesheet">
</head>
<body>
<p> If you see this,Ansible successfuly installed nginx.</p>
<p>{{ ansible_hostname}}</p>
</body>
</html>