“Playbook”的版本间的差异

来自linux中国网wiki
跳到导航 跳到搜索
第1行: 第1行:
==
+
=简单例子=
 +
<pre>
 +
ansible tree  ansible-nginx
 +
ansible-nginx
 +
├── files
 +
│   └── nginx.conf.j2
 +
└── playbook.yml
 +
 
 +
1 directory, 2 files
 +
 
 +
➜  ansible-nginx cat files/nginx.conf.j2
 +
server {
 +
  listen 80;
 +
 
 +
  root /tmp/;
 +
  index index.html index.htm;
 +
 
 +
  server_name a.com;
 +
 
 +
  location / {
 +
  default_type "text/html";
 +
  try_files $uri.html $uri $uri/ =404;
 +
  }
 +
}
 +
 
 +
➜  ansible-nginx cat playbook.yml
 +
---
 +
- hosts: d11
 +
  name: playbook demo
 +
  become: yes
 +
  gather_facts: false
 +
  remote_user: root
 +
  become_user: root
 +
  tasks:
 +
    - name: Update apt cache and install Nginx
 +
      apt:
 +
        name: nginx
 +
        state: latest
 +
        update_cache: yes
 +
 
 +
    - name: Apply Nginx template
 +
      template:
 +
        src: files/nginx.conf.j2
 +
        dest: /etc/nginx/sites-available/default
 +
      notify: Restart Nginx
 +
 
 +
    - name: Enable new site
 +
      file:
 +
        src: /etc/nginx/sites-available/default
 +
        dest: /etc/nginx/sites-enabled/default
 +
        state: link
 +
      notify: Restart Nginx
 +
 
 +
    - name: Allow all access to tcp port 80
 +
      ufw:
 +
        rule: allow
 +
        port: '80'
 +
        proto: tcp
 +
 
 +
  handlers:
 +
    - name: Restart Nginx
 +
      service:
 +
        name: nginx
 +
        state: restarted
 +
 
 +
 
 +
ansible-playbook -C  playbook.yml
 +
ansible-playbook  playbook.yml
 +
 
 +
 
 +
 
 +
</pre>
 +
 
  
 
=refer=
 
=refer=

2023年8月21日 (一) 06:42的版本

简单例子

ansible tree  ansible-nginx 
ansible-nginx
├── files
│   └── nginx.conf.j2
└── playbook.yml

1 directory, 2 files

➜  ansible-nginx cat files/nginx.conf.j2 
server {
  listen 80;

  root /tmp/;
  index index.html index.htm;

  server_name a.com;
  
  location / {
   default_type "text/html";
   try_files $uri.html $uri $uri/ =404;
  }
}

➜  ansible-nginx cat playbook.yml 
---
- hosts: d11
  name: playbook demo
  become: yes
  gather_facts: false
  remote_user: root
  become_user: root 
  tasks:
    - name: Update apt cache and install Nginx
      apt:
        name: nginx
        state: latest
        update_cache: yes

    - name: Apply Nginx template
      template:
        src: files/nginx.conf.j2
        dest: /etc/nginx/sites-available/default
      notify: Restart Nginx

    - name: Enable new site
      file:
        src: /etc/nginx/sites-available/default
        dest: /etc/nginx/sites-enabled/default
        state: link
      notify: Restart Nginx

    - name: Allow all access to tcp port 80
      ufw:
        rule: allow
        port: '80'
        proto: tcp

  handlers:
    - name: Restart Nginx
      service:
        name: nginx
        state: restarted


ansible-playbook -C  playbook.yml
ansible-playbook   playbook.yml




refer

Ansible之Playbook详解、案例

通过ansible安装中间件(jdk,nginx,mysql,etcd集群)


How to Install Nginx using Ansible Playbook