iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 28
1
自我挑戰組

DevOps學習之旅系列 第 28

Day 28 Ansible IT自動化工具(二)

  • 分享至 

  • xImage
  •  

Ansible

導入專案,建置專案從手工到自動,以下幾個是在嘗試把專案會用到的都寫成 playbook,先分開不同的檔案,未來等都完成的差不多,就可以整合成一個大的 playbook,下次需要再建置一次部署環境的時候,就可以直接拿來使用,非常的方面,而且還可以加入版控,方便管理.

實作

git 2.8.1

安裝 git 2.8.1

vim git2.8.1.yml
---
- hosts: all
  name: Git 2.8.1
  tasks:
    - name: install packages
      sudo: yes
      yum: name="{{ packages }}" state=present
      when: ansible_pkg_mgr == "yum"
      vars:
        packages:
        - make
        - wget
        - curl-devel
        - expat-devel
        - gettext-devel
        - openssl-devel
        - zlib-devel
        - gcc
        - perl-ExtUtils-MakeMaker

    - name: Download git-2.8.1.tar.gz
      sudo: yes
      get_url:
        url: https://www.kernel.org/pub/software/scm/git/git-2.8.1.tar.gz
        dest: /usr/src/git-2.8.1.tar.gz

    - name: Unarchive git-2.8.1.tar.gz
      sudo: yes
      unarchive:
        src: /usr/src/git-2.8.1.tar.gz
        dest: /usr/src
        remote_src: yes

    - name: make all
      sudo: yes
      shell: cd /usr/src/git-2.8.1; make prefix=/usr/local/git all

    - name: make install 
      sudo: yes
      shell: cd /usr/src/git-2.8.1; make prefix=/usr/local/git install

    - name: backup old git
      sudo: yes
      shell: mv /usr/bin/git /usr/bin/git-bak; ln /usr/local/git/bin/git /usr/bin/git;git --version
      register: result

    - name: print stdout
      debug:
        msg: "{{ result.stdout }}"

# 測試執行
ansible-playbook git2.8.1.yml

螢幕快照 2018-10-29 下午11.19.41.png

Postgresql

安裝 Postgresql 9.5

vim postgres.yml
---
- hosts: all
  name: Postgresql 9.5 
  tasks:
    - name: pgdg-centos95-9.5-2.noarch.rpm
      sudo: yes
      when: ansible_pkg_mgr == "yum"
      yum:
        name: http://yum.postgresql.org/9.5/redhat/rhel-7-x86_64/pgdg-centos95-9.5-2.noarch.rpm
        state: present

    - name: install postgresql95
      sudo: yes
      when: ansible_pkg_mgr == "yum"
      yum: name="{{ packages }}" state=present
      vars:
        packages:
        - postgresql95-server
        - postgresql95
        - postgresql95-devel

    - name: check /var/lib/pgsql/9.5/data is exist
      sudo: yes
      stat:
        path: "/var/lib/pgsql/9.5/data"
      register: pgdata_dir_version

    - name: initdb
      sudo: yes
      when: not pgdata_dir_version.stat.exists
      shell: /usr/pgsql-9.5/bin/postgresql95-setup initdb

    - name: systemctl restart postgresql-9.5
      sudo: yes
      systemd:
        name: postgresql-9.5
        state: started
        enabled: yes
# 測試執行
ansible-playbook postgres.yml

螢幕快照 2018-10-29 下午11.22.04.png

User

創建使用者,並且創建 DB 的使用者

vim user.yml
---
- hosts: all
  name: Create Linux User and DB User
  tasks:
  - name: Add the user 'project' 
    sudo: yes
    user:
      name: project
      state: present
      password: 123456

  - name: install packages
    sudo: yes
    yum: name="{{ packages }}" state=present
    when: ansible_pkg_mgr == "yum"
    vars:
      packages:
      - gcc
      - gcc-c++
      - make
      - patch
      - openssl-devel
      - zlib-devel
      - readline-devel
      - sqlite-devel
      - bzip2-devel
      - net-tools
      - libjpeg
      - zlib
      - libtiff
      - libwebp
      - tcl
      - tk
      - openjpeg
      - libtiff-devel
      - libjpeg-devel
      - libzip-devel
      - freetype-devel
      - lcms2-devel
      - libwebp-devel
      - tcl-devel
      - tk-devel
      - python-devel
      - libffi-devel

  - name: Create Postgres User
    become: yes
    become_user: postgres
    command: "psql -c 'CREATE ROLE project CREATEDB LOGIN;'"
    ignore_errors: yes

# 測試
ansible-playbook user.yml

螢幕快照 2018-10-29 下午11.25.06.png

pyenv

切換到 project 帳號安裝 pyenv

vim pyenv.yml
---
- hosts: all
  name: Create Linux User and DB User
  tasks:
  - name: add pgsql-9.5 PATH 
    become: yes
    become_user: project
    lineinfile:
      path: ~/.bash_profile
      line: 'export PATH=/usr/pgsql-9.5/bin:$PATH'

  - name: git clone pyenv
    become: yes
    become_user: project
    command: "git clone git://github.com/yyuu/pyenv.git ~/.pyenv"
    ignore_errors: yes
  
  - name: add pyevn 1
    become: yes
    become_user: project
    lineinfile:
      path: ~/.bash_profile
      line: 'export PATH=/home/project/.pyenv/bin:$PATH'

  - name: add pyevn 2
    become: yes
    become_user: project
    lineinfile:
      path: ~/.bash_profile
      line: 'eval "$(pyenv init -)"'

  - name: source .bash_profile
    become: yes
    become_user: project
    shell: source ~/.bash_profile

  - name: pyenv install 3.5.0
    become: yes
    become_user: project
    command: ~/.pyenv/bin/pyenv install 3.5.0

  - name: pyenv install 2.7.12
    become: yes
    become_user: project
    command: ~/.pyenv/bin/pyenv install 2.7.12
#測試
ansible-playbook pyenv.yml

螢幕快照 2018-10-29 下午11.21.33.png

結論

在實作 playbook 的腳本的時候,發現 shell module 很好用,但是有一個問題是,每次執行都會跑一次指令,所以後來都盡量避免使用 shell module.

參考

常用的 Ansible Module 有哪些?


上一篇
Day 27 Ansible IT自動化工具(一)
下一篇
Day 29 Terraform 雲端 IaC
系列文
DevOps學習之旅30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言