[例題] Gitlab Omnibus を Ansible でインストール

手動でのインストール手順

  1. Install and configure the necessary dependencies
    sudo yum install curl policycoreutils openssh-server openssh-clients
    sudo systemctl enable sshd
    sudo systemctl start sshd
    sudo yum install postfix
    sudo systemctl enable postfix
    sudo systemctl start postfix
    sudo firewall-cmd --permanent --add-service=http
    sudo systemctl reload firewalld
  2. Add the GitLab? package server and install the package
    curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
    sudo yum install gitlab-ce
    or
    download rpm and 
    rpm -i gitlab-ce-XXX.rpm
  3. Configure and start GitLab?
    sudo gitlab-ctl reconfigure

これを ansible playbook で実現

https://github.com/kagyuu/AnsibleExam/blob/master/gitlab/gitlab.yml

- name: Configure gitlab
  hosts: testserver
  become: True
  gather_facts: False
  tasks:

    - name: Add EPEL repository
      yum_repository:
        name: epel
        description: EPEL YUM repo
        baseurl: http://download.fedoraproject.org/pub/epel/$releasever/$basearch/

    - name: Add gpg key of EPEL
      rpm_key: key=https://dl.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-7 state=present

    - name: Install Services
      yum: name={{ item }}  state=present
      with_items:
        - curl
        - policycoreutils
        - openssh-server
        - openssh-clients
        - postfix
        - firewalld

    - name: Start Services
      service: name={{ item }} enabled=Yes state=started
      with_items:
        - sshd
        - postfix
        - firewalld

    - name: Setup Firewall
      firewalld: permanent=True service={{ item }} state=enabled permanent=yes
      with_items:
        - http
        - https
      notify: Reload firewall

#    - name: Register gitlab-ce yum repo
#      shell: curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
#      args:
#         creates: /etc/yum.repos.d/gitlab_gitlab-ce.repo
#
#    - name: Install Gitlab-CE
#      yum: name=gitlab-ce  state=present

    - name: Copy Gitlab-CE
      copy: src=files/gitlab/gitlab-ce-8.9.3-ce.0.el7.x86_64.rpm dest=/tmp/

    - name: Install Gitlab-CE
      yum: name=/tmp/gitlab-ce-8.9.3-ce.0.el7.x86_64.rpm state=present

    - name: Configure Gitlab-CE
      command: gitlab-ctl reconfigure

  handlers:

    - name: Reload firewall
      service: name=firewalld state=reloaded


実行結果

[~/sourcetree/AnsibleExam/gitlab]$ ansible-playbook gitlab.yml

PLAY [Configure gitlab] ********************************************************

TASK [Add EPEL repository] *****************************************************
changed: [testserver]

TASK [Add gpg key of EPEL] *****************************************************
changed: [testserver]

TASK [Install Services] ********************************************************
ok: [testserver] => (item=[u'curl', u'policycoreutils', u'openssh-server', u'openssh-clients', u'postfix', u'firewalld'])

TASK [Start Services] **********************************************************
ok: [testserver] => (item=sshd)
ok: [testserver] => (item=postfix)
changed: [testserver] => (item=firewalld)

TASK [Setup Firewall] **********************************************************
changed: [testserver] => (item=http)
changed: [testserver] => (item=https)

TASK [Copy Gitlab-CE] **********************************************************
changed: [testserver]

TASK [Install Gitlab-CE] *******************************************************
changed: [testserver]

TASK [Configure Gitlab-CE] *****************************************************
changed: [testserver]

RUNNING HANDLER [Reload firewall] **********************************************
changed: [testserver]

PLAY RECAP *********************************************************************
testserver                 : ok=9    changed=8    unreachable=0    failed=0
gitlab.png

Done.

Playbook の全体構成

a graph image

playbook を少しづつ読み解く

play全体の設定

- name: Configure gitlab
  hosts: testserver
  become: True
  gather_facts: False
  tasks
項目名デフォルト値設定内容
nameplayの名称
hosts実行ホストのパターン (必須)
tasks実行タスクのリスト (必須)
vars変数 → cf.Ansible Jinja2 template
vars_file変数定義 YAML ファイル → cf.Ansible Jinja2 template
remote_user接続ユーザ → cf.ansible.cfg で共通定義
bocomeFalseremote_user と異なるユーザでタスクを実行するか?
become_methodsudobecome=True のとき、異なるユーザになるためのコマンド。{sudo, su, pbrun, pfexec, doas}
become_userタスクを実行するユーザ名
sudoAnsible2 からは become をつかう
handlersタスク正常終了時に呼び出されるタスクのリスト
includeこの play に include する YAML ファイル
gather_factsTrue実行タスクに関する情報 (CPU/OS/Memoryなど) を収集するか? → Ansible Jinja2 template#fact

YUMにEPELの追加

   - name: Add EPEL repository
     yum_repository:
       name: epel
       description: EPEL YUM repo
       baseurl: http://download.fedoraproject.org/pub/epel/$releasever/$basearch/

   - name: Add gpg key of EPEL
     rpm_key: key=https://dl.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-7 state=present

yum install コマンドの実行 & ansible の loop 構文(with_item)

   - name: Install Services
     yum: name={{ item }}  state=present
     with_items:
       - curl
       - policycoreutils
       - openssh-server
       - openssh-clients
       - postfix
       - firewalld

サービスの立ちあげ (systemctl)

   - name: Start Services
     service: name={{ item }} enabled=Yes state=started
     with_items:
       - sshd
       - postfix
       - firewalld

firewalld の操作 & ansible の handler

   - name: Setup Firewall
     firewalld: permanent=True service={{ item }} state=enabled permanent=yes
     with_items:
       - http
       - https
     notify: Reload firewall
...
  handlers:

   - name: Reload firewall
     service: name=firewalld state=reloaded

コマンド実行 (shell vs command) とファイルコピー

#    - name: Register gitlab-ce yum repo
#      shell: curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
#      args:
#         creates: /etc/yum.repos.d/gitlab_gitlab-ce.repo
#
#    - name: Install Gitlab-CE
#      yum: name=gitlab-ce  state=present

   - name: Copy Gitlab-CE
     copy: src=files/gitlab/gitlab-ce-8.9.3-ce.0.el7.x86_64.rpm dest=/tmp/

   - name: Install Gitlab-CE
     yum: name=/tmp/gitlab-ce-8.9.3-ce.0.el7.x86_64.rpm state=present

   - name: Configure Gitlab-CE
     command: gitlab-ctl reconfigure

rpm コマンドって、どうやって実行するんだっけ?

  - name: Install Gitlab-CE
    yum: name=/tmp/gitlab-ce-8.9.3-ce.0.el7.x86_64.rpm state=present

yum モジュールで rpm ファイルを指定する

補遺 (ここで登場しなかった構文)

with_items 以外 loop

with_itemsリストから要素を一つづつ取り出す
with_dictディクショナリから (item.key , item.value) の組を一つづつ取り出す
with_fileglobファイルパターンに会うファイル名を一つづつ取り出す (files/*.conf とか)
with_nested複数のリストの順列組み合わせを一つづつ取り出す ({a,b}x{1,2,3}=>{a1,a2,a3,b1,b2,b3})
with_random_choiceリストから、要素をランダムに一つ取り出す
with_first_foundリストから、最初の要素を取り出す

タスクの終了結果による条件分岐

- shell : curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
  register : result

- debug : msg = "コマンド実行失敗"
  when  : result | failed

- debug : msg = "状態変更"
  when  : result | changed

- debug : msg = "コマンド実行成功"
  when  : result | success

- debug : msg = "スキップ"
  when  : result | skipped

コマンド実行結果を register で変数にとっておいて、あとで when で評価できる。
コマンド実行結果は {failed, changed, success, skipped}

factによる条件分岐

-debut : msg = "RHELです"
 when : ansible_os_family == "RedHat"

ファイルの存在確認による条件分岐

-stat : path=/etc/yum.repos.d/gitlab_gitlab-ce.repo
 register : repo_file_status

-file : path=/etc/yum.repos.d/gitlab_gitlab-ce.repo owner=root group=root mode=400
 when: repo_file_status.exists

stat でファイルのステータスを取得して、register で変数にとっておく。
後段の when で条件分岐を行う。

モジュール指定で改行したい

MEMO


Ansible


添付ファイル: filegitlab.png 2166件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS   sitemap
Last-modified: 2016-07-06 (水) 01:39:53 (2845d)
Short-URL:
ISBN10
ISBN13
9784061426061