ansible之playbook实战

playbook实战

通过playbook role部署nginx

1.创建角色目录及相关子目录

[root@ansinble_center tmp]#cd /etc/ansible/roles
[root@ansinble_center roles]#mkdir -pv nginx/{files,templates,vars,handers,meta,default,tasks}
mkdir: created directory ‘nginx’
mkdir: created directory ‘nginx/files’
mkdir: created directory ‘nginx/templates’
mkdir: created directory ‘nginx/vars’
mkdir: created directory ‘nginx/handers’
mkdir: created directory ‘nginx/meta’
mkdir: created directory ‘nginx/default’
mkdir: created directory ‘nginx/tasks’

2.在tasks定义任务

vim nginx/tasks/main.yaml

在这里插入图片描述

- name: mkdir /data/www
  file: path=/data/www state=directory
- name: create user nginx
  user: name=nginx
- name: copy nginx html to remote host
  copy: src=index.html dest=/usr/local/nginx/html/
- name: copy nginx package to remote host
  copy: src=nginx-1.12.2.tar.gz dest=/tmp/nginx-1.12.2.tar.gz
  tags: cppkg
- name: tar nginx
  shell: cd /tmp; tar -xf nginx-1.12.2.tar.gz
- name: install pkg
  yum: name={{ item }} state=latest
  with_items:
     - openssl-devel
     - pcre-devel
     - gcc
- name: install nginx
  shell: cd /tmp/nginx-1.12.2; ./configure --prefix=/usr/local/nginx  --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre; make && make install
- name: copy conf file nginx.conf
  template: src=nginx.conf dest=/usr/local/nginx/nginx.conf
  tags: ngxconf
  notify: reload nginx service

shell: yum install -y openssl-devel pcre-devel gcc automake

nginx源码安装文件
cd nginx/files
wget http://nginx.org/download/nginx-1.12.2.tar.gz
nginx首页html文件
cd nginx/files
vim index.html
<h1>HELLO WROLD</h1>
nginx配置模版文件
cd /nginx/templates
vim nginx.conf

在这里插入图片描述

#user  nobody;
worker_processes  1;

events {
    worker_connections  {{ con_counts }};
}


http {
    include       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  logs/access.log  main;

sendfile        on;
#tcp_nopush     on;

#keepalive_timeout  0;
keepalive_timeout  65;

#gzip  on;

server {
    listen       80;
    server_name  localhost;

    location / {
        root   {{ html_dir }};
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }

}
}

3.在vars定义变量

vim nginx/vars/main.yaml

con_counts:“65535”

html_dir:"/data/www"

4.在handers定义触发器

vim nginx/handers/main.yaml

-name:reload nginx service

shell:/usr/local/nginx/sbin/nginx

5.创建nginx.yaml引导文件

cd /etc/ansible/roles
vim nginx.yaml

在这里插入图片描述

6.运行

[root@ansinble_center roles]#ansible-playbook  nginx.yaml

在这里插入图片描述

7.去网页查看

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值