1. 程式人生 > 其它 >ansible專案 一鍵部署

ansible專案 一鍵部署

1、一鍵部署包介紹
(1)服務共分3部分:service config.yml inventory
(2)service 中存放著專案元件
服務元件中
files 存放著檔案安裝包以及前端專案包
tasks 中存放著執行指令碼main.yml檔案

  - name: install | Create msspoper group.
    group: name={{msspoper_user}}  state=present

  - name: install | Create msspoper user.
    user: name={{ msspoper_user }} group={{ msspoper_user }} password={{ msspoper_user_pwd }}

  - name: chmod /tmp|chmod tmp
    command: chmod 777 /tmp  

  - name: mkdir | Create project dir.
    file:
        path: /home/msspoper/{{ dest_dir }}/
        state: directory
        mode: '777'
      
  - name: change /etc/hosts
    lineinfile:
      dest: /etc/hosts
      line: "{{ settings_server_address }} {{ settings_server_name }}"

  - name: install | Copy ROOT.zip
    unarchive: src=ROOT.zip dest=/home/{{ msspoper_user }}/{{dest_dir}} owner={{ msspoper_user }} mode=777

  - name: get start sh file
    shell: "ls /home/{{ msspoper_user }}/{{ dest_dir }}/bin/|grep .sh"
    register: sh_file

  - name: check if single.sh exists
    stat: 'path=/home/{{ msspoper_user }}/{{ dest_dir }}/bin/{{ sh_file.stdout}}'
    register: script_stat

  - debug: msg="single.sh exists"
    when: script_stat.stat.exists
  
  - name: restart jar   
    shell: 'su - {{ msspoper_user }} /home/{{ msspoper_user }}/{{ dest_dir }}/bin/{{ sh_file.stdout }} restart'
    register: res
  - name: mkdir web | Create web dir.
    file:
        path: "{{dest_web_dir}}"
        state: directory
        mode: '777'

  - name: install | Copy web pro
    unarchive: src=mssmpro.zip dest={{dest_web_dir}} owner=msspoper mode=0755

defaults 存放著指令碼中的變數main.yml檔案

dest_dir: ""
dest_web_dir: ""
jar_file: ""

(3)config.xml
該檔案主要設定hosts下的變數以及角色(服務元件)
hosts對應著一個叢集,一個hosts對應多個角色,hosts是服務元件(role)中指令碼執行的目標機器叢集

- hosts: t
  vars:
    - msspoper_user: ""
    - msspoper_user_pwd: ""
    - settings_server_address: ""
    - settings_server_name: ""
  roles:
    - service/*

(4)inventory
[hosts] hosts指代目標叢集名稱 和config.xml 中的配置相對應,主要用來設定目標叢集ip
[all:vars] 為全域性變數 ,對於非root使用者 代理登入 ansible_become=true ansible_become_user=root ansible_become_pass= ***

[t]
0.0.0.0
1.1.1.1
[all:vars]
ansible_ssh_user=msspoper
ansible_ssh_pass=Bjca@2019cl
ansible_become=true
ansible_become_user=root
ansible_become_pass=Bjca@2019cl
ansible_ssh_port=7327

2、一鍵部署步驟
(1)服務元件收集
(2)生成一鍵部署包
上傳到具有ansible環境的伺服器上,在對應目錄下執行命令 ansible_playbook -i inventory config.yml