ansible部署nginx:1个简单的playbook脚本-roles

.
├── roles
│   └── nginx
│       ├── files
│       │   └── index.html
│       ├── handlers
│       │   └── main.yaml
│       ├── tasks
│       │   └── main.yaml
│       ├── templates
│       │   └── nginx.conf.j2
│       └── vars
│           └── main.yaml
└── site.yml

7 directories, 6 files
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

[root@stand playhook1]# cat roles/nginx/files/index.html

<html>
  <head>
    <title>Welcome to Nginx!</title>
  </head>
  <body>
    <h1>Hello, Nginx is working!</h1>
  </body>
</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

[root@stand playhook1]# cat roles/nginx/handlers/main.yaml

---
- name: restart nginx
  service:
    name: nginx
    state: restarted
  • 1
  • 2
  • 3
  • 4
  • 5

[root@stand playhook1]# cat roles/nginx/templates/nginx.conf.j2

user  nginx;
worker_processes  {{ ansible_processor_cores }};

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

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

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    # Gzip Settings
    gzip  on;
    gzip_disable "msie6";

    include /etc/nginx/conf.d/*.conf;
    server {
        listen       80;
        server_name  {{ server_name }};

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }
}
  • 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

[root@stand playhook1]# cat roles/nginx/vars/main.yaml

---
ansible_processor_cores: 2
server_name: localhost
  • 1
  • 2
  • 3

[root@stand playhook1]# cat site.yml

---

- hosts: webservers
  roles:
    - { role: nginx, version: "1.24.0" }
  • 1
  • 2
  • 3
  • 4
  • 5

[root@stand playhook1]# cat roles/nginx/handlers/main.yaml

---
- name: restart nginx
  service:
    name: nginx
    state: restarted
  • 1
  • 2
  • 3
  • 4
  • 5

[root@stand playhook1]# cat roles/nginx/tasks/main.yaml

---
- name: Add Nginx repository
  yum_repository:
    name: nginx
    description: Nginx Repository
    baseurl: http://nginx.org/packages/centos/$releasever/$basearch/
    gpgcheck: yes
    enabled: yes
    gpgkey: https://nginx.org/keys/nginx_signing.key

- name: Install nginx package
  yum:
    name: "nginx-{{ version }}"
    state: present

- name: Ensure Nginx html directory exists
  file:
    path: /usr/share/nginx/html
    state: directory
    owner: nginx
    group: nginx
    mode: 0755

- name: Copy nginx.conf Template
  template:
    src: nginx.conf.j2
    dest: /etc/nginx/nginx.conf
    owner: root
    group: root
    backup: yes
    mode: 0755
  notify: restart nginx

- name: Copy index html
  copy:
    src: index.html
    dest: /usr/share/nginx/html/index.html
    owner: root
    group: root
    backup: yes
    mode: 0755

- name: Make sure nginx service is running
  service:
    name: nginx
    state: started

  • 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
上一篇:32.攻防世界Web_php_include


下一篇:2024年前端真实面试题集合(Vue篇02)