DevOps

Ansible-Playbook

2021-11-08  本文已影响0人  小李飞刀_lql

Nginx自动安装

001 playbook文件

---
- hosts: webservers
  vars:
    hello: Ansible
  tasks:
  - name: Add repo
    yum_repository:
    name: nginx
    description: nginx repo
    baseurl: http://nginx.org/packages/centos/7/$basearch/
    gpgcheck: no
    enabled: 1
  - name: Install nginx
    yum:
    name: nginx
    state: latest
  - name: Copy nginx configuration file
    copy:
    src: ./site.conf
    dest: /etc/nginx/conf.d/site.conf
  - name: Start nginx
    service:
    name: nginx
    state: started
  - name: Create wwwroot directory
    file:
    dest: /var/www/html
    state: directory
  - name: Create test page index.html
    shell: echo "hello {{hello}}" > /var/www/html/index.html
 
 
 ---------------------------------------------------------------------------
 
 ---
- hosts: webservers
 vars:  //变量定义 【1、资产清查hosts文件 2、资产清单同级目录,group_vars/xxx.yaml
           3、命令行-e】
 hello: Ansible
 tasks:
 - name: Add repo
 yum_repository:
 name: nginx
 description: nginx repo
 baseurl: http://nginx.org/packages/centos/7/$basearch/
 gpgcheck: no
 enabled: 1
 - name: Install nginx
 yum:
 name: nginx
 state: latest
 - name: Copy nginx configuration file
 copy:
 src: ./site.conf
 dest: /etc/nginx/conf.d/site.conf
 - name: Start nginx
 service:
 name: nginx
 state: started
 - name: Create wwwroot directory
 file:
 dest: /var/www/html
 state: directory
 - name: Create test page index.html
 shell: echo "hello {{hello}}" > /var/www/html/index.html
 
server {
  listen 81;
  server_name localhost;
  location / {
    root /var/www/html;
    index index.html;
  }
}

002 启动相关服务

ansbile-playbook nginx.yaml

003 查看nginx服务

find / -name nginx

在变更时运行特定任务(handlers)

001 notify

 - name: write the nginx config file
   copy:
     src: ./site.conf
     dest: /etc/nginx/conf.d/site.conf
   notify:
     - restart nginx

002 立即执行

- meta: flush_handlers

003 handlers

 handlers:
   - name: restart nginx
     service: name=nginx state=restarted

004 不能重启nginx的处理

将/etc/selinux/config 
SELINUX=enforcing         #改成disabled就OK了

005 完整文件nginx2.yaml

---
- hosts: webservers
  vars:
    hello: Ansible
  tasks:
  - name: Add repo
    yum_repository:
      name: nginx
      description: nginx repo
      baseurl: http://nginx.org/packages/centos/7/$basearch/
      gpgcheck: no
      enabled: 1
  - name: Install nginx
    yum:
      name: nginx
      state: latest
  - name: Copy nginx configuration file
    copy:
      src: ./site.conf
      dest: /etc/nginx/conf.d/site.conf
    notify:
      - restart nginx 
  - meta: flush_handlers
  - name: Start nginx
    service:
      name: nginx
      state: started
  - name: Create wwwroot directory
    file:
      dest: /var/www/html
      state: directory
  - name: Create test page index.html
    shell: echo "hello {{hello}}" > /var/www/html/index.html
  handlers:
    - name: restart nginx
      service: name=nginx state=restarted

任务控制(tags)

001 给任务打标签

  - name: Install nginx
    yum:
      name: nginx
      state: latest
    tags: install

002 执行指定的标签

ansible-playbook nginx3.yaml --tags "addrepo,install"

003 跳过的标签不执行

 ansible-playbook nginx3.yaml --skip-tags "create"  

register(注册变量)

001 作用

将任务执行的结果保存到变量,供后续任务使用

002 格式

---
- hosts: webservers
  tasks:
  - name: get date
    shell: date +"%F_%T"
    register: result

  - name: 使用shell结果
    shell: touch /tmp/{{result.stdout}}

  - name: 输出shell结果
    debug: msg="{{result.stdout}}"

facts(系统信息变量)

  - name: 内置变量
    debug: msg="{{ansible_all_ipv4_addresses[0]}}" 

  - name: hostname
    debug: msg="{{ansible_hostname}}"

上一篇 下一篇

猜你喜欢

热点阅读