美文网首页DevOps
Ansible-Playbook

Ansible-Playbook

作者: 小李飞刀_lql | 来源:发表于2021-11-08 09:39 被阅读0次

    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}}"
    
    

    相关文章

      网友评论

        本文标题:Ansible-Playbook

        本文链接:https://www.haomeiwen.com/subject/bjdmzltx.html