Python Linux系统管理与自动化运维之深入浅出Ansible(四)

本节结合前面所学的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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值