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

原文地址:https://www.cnblogs.com/tianma-0/p/14894073.html