Ansible剧本

PlayBook

1.目录结构

	[root@ansible ~]# tree roles/
	roles/
	├── nginx
	│?? ├── files
	│?? │?? └── index.html
	│?? ├── handlers
	│?? │?? └── main.yaml
	│?? ├── tasks
	│?? │?? └── main.yaml
	│?? ├── templates
	│?? │?? └── nginx.conf.j2
	│?? └── vars
	│??     └── main.yaml
	└── site.yaml
***准备目录结构***
	mkdir roles/nginx/{files,handlers,tasks,templates,vars} -p
	touch roles/site.yaml roles/nginx/{handlers,tasks,vars}/main.yaml
	yum install -y nginx && cp /etc/nginx/nginx.conf roles/nginx/templates/nginx.conf.j2

2.编写任务

vim roles/nginx/tasks/main.yaml
	---
	- name: install epel-release packge
  	yum: name=epel-release state=latest

	- name: install nginx packge
	  yum: name=nginx  state=latest

	- name: copy index.html
	  copy: src=index.html dest=/usr/share/nginx/html/index.html

	- name: copy nginx.conf template
 	 template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
  	notify: restart nginx

	- name: make sure nginx service running
 	 service: name=nginx state=started enabled=yes

3.准备配置文件

vim roles/nginx/templates/nginx.conf.j2   
	修改:worker_processes  {{ ansible_processor_cores }};
	           worker_connections {{ worker_connections }};

4.编写变量

vim roles/nginx/vars/main.yaml
	worker_connections: 10240

5.编写处理程序

vim roles/nginx/handlers/main.yaml
	---
	- name: restart nginx
  	service: name=nginx state=restarted	

6.编写剧本

vim roles/site.yaml
	- hosts: host4
  	roles:
 	 - nginx

7.实施

cd roles
ansible-playbook site.yaml --syntax-check
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值