1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
| root@master:~# mkdir /role
root@master:~# cd /role
root@master:/tmp/role# mkdir nginx/{files,handlers,vars,tasks,templates} -p
root@master:/tmp/role# touch nginx/{tasks,handlers,vars}/main.yaml
root@master:/tmp/role# touch /tmp/role/site.yaml
root@master:/tmp/role# echo welcome to nginx > /tmp/role/nginx/index.html
root@master:/tmp/role# tree .
.
├── nginx
│ ├── files
│ │ └── nginx.repo
│ ├── handlers
│ │ └── main.yaml
│ ├── index.html
│ ├── tasks
│ │ └── main.yaml
│ ├── templates
│ │ └── nginx.conf.j2
│ └── vars
│ └── main.yaml
└── site.yaml
6 directories, 7 files
# 将每一个功能分离出来定义成角色更有条理性
# site.yaml: 是一个习惯性的命名方式,存放在那个主机用那个角色
# vars: 用于存放变量,这个变量是用在整个角色中的,如果没有变量,此目录不需要创建.
# templates: ansible的jinja2模板就是template模板,该模板和copy模板作用基本一样,都是把某个文件复制到远端主机上,但是区别在于template模板可以获取变量的值,而copy则是原封不动的把文件内容复制过去.
# files: 存放一些配置文件,源,包,不需要传参的文件
# handlers: 运行任务后的触发动作
# tasks: 运行任务列表
# ansible机器准备nginx源(ubuntu 系统不需要!!!)
cat /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
root@master:/tmp/role# cp /etc/yum.repos.d/nginx.repo /role/nginx/files/
root@master:/tmp/role# yum -y install nginx
root@master:/tmp/role# cp /etc/nginx/nginx.conf /role/nginx/templates/nginx.conf.j2 # j2是一个Jinja模板文件
root@master:/tmp/role# ls
nginx site.yaml
root@master:/tmp/role# cat site.yaml
---
- hosts: webserver
roles:
- nginx # 此处的nginx对应/tmp/role/nginx,相当于一个角色,可以再来php,mysql,这样就条理很清晰
# 写tasks角色中的yaml文件
root@master:/tmp/role# cat nginx/tasks/main.yaml
---
- name: Install Nginx
apt:
name: nginx
state: present
update_cache: yes
- name: Copy Nginx configuration
template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
notify:
- Restart Nginx
- name: Copy Nginx configuration
template:
src: default.conf.j2
dest: /etc/nginx/conf.d/default.conf
notify:
- Restart Nginx
- name: Copy index.html
copy:
src: index.html
dest: /var/www/html/index.html
- name: ensure nginx running
service:
name: nginx
state: started
enabled: yes
- debug:
msg: "Nginx configuration updated and handler notified."
# 写handlers角色中的yaml文件
root@master:/tmp/role# cat nginx/handlers/main.yaml
---
- name: Restart Nginx
service:
name: nginx
state: restarted
# 写vars角色中定义变量
root@master:/tmp/role# cat nginx/vars/main.yaml
worker_connections: 10240
ports: 80
# 演ansible的剧本
root@master:/tmp/role# ansible-playbook site.yaml # 注意路径
# 检测剧本是否成功
root@master:/tmp/role# ansible webserver -m shell -a 'ss -antp |grep nginx' -o
node1 | CHANGED | rc=0 | (stdout) LISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=14054,fd=6),("nginx",pid=14053,fd=6))
root@master:/tmp/role# ansible webserver -m shell -a 'cat /usr/share/nginx/html/index.html' -o
node1 | CHANGED | rc=0 | (stdout) <!DOCTYPE html>\n<html>\n<head>\n<title>Welcome to nginx!</title>\n<style>\n body {\n width: 35em;\n margin: 0 auto;\n font-family: Tahoma, Verdana, Arial, sans-serif;\n }\n</style>\n</head>\n<body>\n<h1>Welcome to nginx!</h1>\n<p>If you see this page, the nginx web server is successfully installed and\nworking. Further configuration is required.</p>\n\n<p>For online documentation and support please refer to\n<a href="http://nginx.org/">nginx.org</a>.<br/>\nCommercial support is available at\n<a href="http://nginx.com/">nginx.com</a>.</p>\n\n<p><em>Thank you for using nginx.</em></p>\n</body>\n</html>
# 修改nginx的端口,检查handlers重启nginx的效果.需要重新下发指令。
[root@node1 role]# cat nginx/vars/main.yaml
worker_connections: 10240
ports: 80
root@master:/tmp/role# ansible webserver -m shell -a 'ss -antp |grep nginx' -o
node1 | CHANGED | rc=0 | (stdout) LISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=17025,fd=6),("nginx",pid=17024,fd=6),("nginx",pid=17023,fd=6))
|