Share
Embed
Transcript
17 slides
-
発表の前に… • 話すこと ◦ MolecueとGitHub Actionsのチュートリアル体験 ◦ 雑誌で紹介されている内容の体験 ◦ それぞれのツールの(初心者なりの)説明
◦ 公式ドキュメントで躓いた経験 • 話さないこと ◦ 上級者的なこと ◦ 明日から業務で役立てるTIPS -
目次 • 背景 • やったこと • イメージ図 • Ansible Moleculeとは
• GitHub Actionsとは • 実装 • 躓いたこと • 感想 -
背景 • Ansible Roleは一度作成しても、度々修正する必要がある • 修正後にテストを行って、動作確認する必要がある ◦ 修正の度にテストは面倒 • テストフレームワークを使ってテストを簡単にしたい
• GitHub Actionsを使ってテストもCI/CDに✨ -
やったこと • テスト環境としてCentOS7,CentOS8のコンテナを用意(molecule.yml) • テスト対象のタスクPlaybookを用意 (Software Design 2020年6月号|技術評論社) • molecule
testでタスクがPlaybookが問題なく動くことを確認 • GitHub Actionsをリポジトリ内に組み込む • GitHub Actionsの仮想環境でMoleculeを実行 -
GitHub Actionsとは • GitHub社が提供するCI/CDシステム(https://github.co.jp/features/actions) • GitHubに関係する操作なども自動化できる • パブリックリポジトリでの利用は無料 プラン ジョブ実行時間
ストレージ Free 2,000分/月 500MB Pro 3,000分/月 1GB Team 3,000分/月 2GB Enterprise Cloud 50,000分/月 50GB -
実装(Ansible Molecule編) molecule.yml --- dependency: name: galaxy driver: name: docker
platforms: - name: instance1 image: docker.io/centos:7 pre_build_image: true privileged: True command: /sbin/init - name: instance2 image: docker.io/centos:8 pre_build_image: true privileged: True command: /sbin/init provisioner: name: ansible verifier: name: ansible main.yml --- # tasks file for testmol - name: install httpd package yum: name: httpd state: latest - name: start httpd systemd: name: httpd state: started enabled: yes verify.yml --- # This is httpd install playbook to execute Ansible tests. - name: Verify hosts: all tasks: - ignore_errors: yes block: - name: httpdパッケージの存在を確認する yum: list: httpd register: result_rpm - name: httpdプロセスが起動していることを確認する shell: ps -ef | grep http[d] register: result_proc - name: httpdサービスが自動起動になっているかを確認する shell: systemctl is-enabled httpd register: result_enabled - name: 結果をまとめて確認する assert: that: "{{ result.failed == false }}" loop: - "{{ result_rpm }}" - "{{ result_proc }}" - "{{ result_enabled }}" loop_control: loop_var: result -
実装(Ansible Molecule編) • molecule testコマンドを実行(長いので結果だけ) • テスト完了後にテスト環境を削除するアクションを自動で行う(destroy) PLAY RECAP *********************************************************************
instance1 : ok=5 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 instance2 : ok=5 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 Verifier completed successfully. --> Scenario: 'default' --> Action: 'cleanup' Skipping, cleanup playbook not configured. --> Scenario: 'default' --> Action: 'destroy' PLAY [Destroy] ***************************************************************** -
実装(GitHub Actions編) --- name: Molecule Test on: [push, pull_request] jobs:
build: runs-on: ubuntu-latest strategy: max-parallel: 4 matrix: python-version: [3.7, 3.8] steps: - uses: actions/checkout@v2 with: path: molecule_demo - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v2 with: python-version: ${{ matrix.python-version }} - name: Install dependencies run: | sudo apt install git apt-transport-https ca-certificates curl gnupg-agent software-properties-common curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" sudo apt-get update sudo apt-get install docker-ce docker-ce-cli containerd.io git clone https://github.com/Yuhta28/ansible-Molecule.git python3 -m pip install --upgrade pip python3 -m pip install -r ansible-Molecule/requirements.txt python3 -m pip install "molecule[docker,lint]" - name: Test with molecule run: | cd ansible-Molecule/testmol molecule test • job ◦ ワークフローの中の構成要素 ◦ 複数のjobを並列して実行も可能 • step ◦ jobの中の構成要素 ◦ コマンドの実行内容 • runner ◦ jobの実行環境(WinS Mac Ubuntuが対応) 詳しい内容は公式のリファレンスを⇓ https://docs.github.com/ja/free-pro-team@latest/actions/learn-github-actions/introduction-to-gi thub-actions 実行環境 処理内容 -
躓いたこと • Ansilbe Moleculeの公式リファレンスで紹介されていたサンプルで失敗 • 紹介されているDockerコンテナのインストール方法がおかしい --- name: Molecule Test
on: [push, pull_request] jobs: build: runs-on: ubuntu-latest strategy: max-parallel: 4 matrix: python-version: [3.6, 3.7] steps: - uses: actions/checkout@v2 with: path: molecule_demo - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v2 with: python-version: ${{ matrix.python-version }} - name: Install dependencies run: | sudo apt install docker python3 -m pip install --upgrade pip python3 -m pip install -r requirements.txt - name: Test with molecule run: | molecule test Ubuntuでこんなインストール方法は知らない https://docs.docker.com/engine/install/ubuntu/