LoginSignup
18

More than 5 years have passed since last update.

ansible-playbookでjenkins構築

Last updated at Posted at 2014-11-14

目次

  • ansbile-playbookの書き方
  • 実行手順
  • Vagarantでの実行

ansible-playbookの書き方

構成

ansible_test
├─ansible
  ├─inventory
  │  └─jenkins
  │    └─jenkins.yml
  ├─roles
  │ └─jenkins
  │   ├─tasks
  │   │  ├─common.yml
  │   │  ├─jenkins.yml
  │   │  └─main.yml
  │   └─vars
  │      └─main.yml
  └─jenkins.yml

iventory

inventory/jenkins/jenkins.yml
[jenkins]
# 対象のサーバのIPアドレスまたはホスト名
  • 対象のサーバを定義

main

ansible/jenkins.yml
---
- name: Setup jenkins
  hosts: all
  sudo: yes
  roles:
    - jenkins
  • 実行するファイル
  • この場合sudo権限でroles/jenkins/main.ymlを実行する
  • rolesは複数指定可能
roles/tasks/main.yml
---
- include: common.yml
- include: jenkins.yml
  • ベタ書きもできるけど他にもタスクしやすいように分けた
  • 上から順に実行

tasks

common

roles/tasks/common.yml
---
- name: Install common packages
  apt: pkg={{ item }} state=latest update_cache=yes
  with_items: jenkins_preparation_items
tags:
    - common
    - jenkins
  • with_itemsをvars/main.ymlに記述
  • tagsを登録しておくと登録したtagのみを実行することも可能

jenkins

roles/tasks/jenkins.yml
---
- name: Add the maven3 PPA
  raw: add-apt-repository ppa:natecarlson/maven3 -y
  tags:
    - jenkins

- name: Install jenkins dependencies
  apt: pkg={{ item }} state=present update_cache=yes
  register: jdk_installed
  with_items: jenkins_dependencies
  tags:
    - jenkins

- name: Update java alternatives
  action: command update-java-alternatives -s java-1.7.0-openjdk-amd64
  when: jdk_installed.changed
  tags:
    - jenkins

- name: Get jenkins pkg
  action: shell wget -q -O - http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add -
  tags:
    - jenkins

- name: Add jenkins pkg
  command: echo 'deb http://pkg.jenkins-ci.org/debian binary/' > /etc/apt/sources.list.d/jenkins.list
  tags:
    - jenkins

- name: Install jenkins
  apt: pkg=jenkins state=latest update_cache=yes
  register: jenkins_installed
  tags:
    - jenkins

main

vars/main.yml
---
jenkins_preparation_items:
  - python-software-properties
  - python
  - g++
  - make
  - git
jenkins_dependencies:
  - openjdk-7-jre
  - openjdk-7-jdk
  - maven3

apt options

parameter required default choices comment
cache_valid_time no キャッシュの有効時間
単位はsec
force no no
  • yes
  • no
installs/removesを強制的にする
state no present
  • latest
  • absent
  • present
installするpackageの状態を確かめる
update_cache no
  • yes
  • no
apt-get updateをoperation前にするかどうか
  • 他にもあるよ

実行手順

ansible環境を作る

ansibleのinstall

terminal
$ brew update

# エラーが出る方のみ
$ cd $(brew --prefix)
$ git fetch origin
$ git reset --hard origin/master

$ brew install ansible
$ ansible --version
# ansible-1.7.2

ansible-1.7.2は不要

ansible-1.6.2
# インストール
$ easy_install virtualenv

# virtual環境の構築
$ virtualenv ansible-env

# virtual環境に入ってansibleに必要なものをinstallする
$ source ansible-env/bin/activate
$ easy_install pip
$ pip install paramiko PyYAML jinja2

# ansibleのinstall
$ git clone git://github.com/ansible/ansible.git ansible_setup
$ cd ansible_setup
$ source ./hacking/env-setup
$ git submodule update --init --recursive
$ cd ..

# ERROR: apt is not a legal parameter in an Ansible task or handler はgit submodule updateで解決した

実行

terminal
$ pwd
# /Users/hoge/ansible_test
// 状況に応じてoptionつける
$ ansible-playbook -i ./ansible/inventory/jenkins/jenkins.yml ./ansible/jenkins.yml -K
  • -i INVENTORY, --inventory=INVENTORY
    • インベントリファイルを指定する
  • -k, --ask-pass
    • SSHのパスワードを尋ねる
  • -K, --ask-sudo-pass
    • sudoのパスワードを尋ねる
  • -u REMOTE_USER, --user=REMOTE_USER
    • SSHで接続するユーザ名を指定する
  • -C, --check
    • dry runできる(実際にサーバは変更されない)
  • -t TAGS, --tags=TAGS
    • tagを指定できる
    • カンマ区切りで複数指定も可能
  • -vvv
    • コンソール出力される

Vagrantでの実行

Vagrantfileの設定

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant::configure("2") do |config|

  # ubuntu 12.04...
  config.vm.box = "ubuntu/precise64"

  #jenkins
  config.vm.network :forwarded_port, guest: 8080, host: 8080

  # using a specific IP.
  config.vm.network :private_network, ip: "192.168.33.10"

end

inventory

inventory/jenkins/jenkins.yml
[jenkins]
192.168.33.10

sshconfigの設定

~/.ssh/config
Host 192.168.33.*
IdentityFile ~/.vagrant.d/insecure_private_key
User vagrant

実行

terminal
$ vargant --version
Vagrant 1.6.5
$ vagrant up

$ ansible-playbook -i ./ansible/inventory/jenkins/jenkins.yml ./ansible/jenkins.yml -K

アクセス確認

  • localhost:8080でjenkinsが起動していれば完了

参考URL

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18