4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

RundeckからAnsible playbook&roleをcloneして流すまで

Last updated at Posted at 2017-10-22

Rundeckで
playbookをclone → galaxyでroleを取ってくる → Ansible流す
という感じのことをやった。わりと手こずったのでメモ。

こんなイメージ。
スクリーンショット 2017-10-22 23.51.27.png

環境

MacにVagrantでCentOS7を立ててやった。
適当なVagrantfileを書いて2ノード立てる。node01にRundeckとAnsibleを入れて、Playbookをnode02に流すという感じ。

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

Vagrant.configure(2) do |config|
  config.vm.box = "minimal/centos7"

  config.vm.define "node01" do |centos|
     centos.vm.network :forwarded_port, id: "ssh", guest: 22, host: 2222
     centos.vm.network "private_network", ip: "192.168.33.101"
     config.vm.provision "shell", run: "always", inline: "systemctl restart network.service"

     centos.vm.provider "virtualbox" do |vb|
       vb.memory = "1024"
     end
  end

  config.vm.define "node02" do |centos|
     centos.vm.network :forwarded_port, id: "ssh", guest: 22, host: 2223
     centos.vm.network "private_network", ip: "192.168.33.102"
     config.vm.provision "shell", run: "always", inline: "systemctl restart network.service"
  end
end

プライベートIPがおかしくなることがあるので、その場合は直す。
vagrantのCentOS7のIPが192.168.56.xxxになる

RundeckとAnsibleのインストール

インストールは公式通りに。

/etc/rundeck/rundeck-config.propertiesにIPを書いてから起動。ブラウザで見れるか確認しておく。

rundeck-config.properties
#loglevel.default is the default log level for jobs: ERROR,WARN,INFO,VERBOSE,DEBUG
loglevel.default=INFO
rdeck.base=/var/lib/rundeck

#rss.enabled if set to true enables RSS feeds that are public (non-authenticated)
rss.enabled=false
# change hostname here
grails.serverURL=http://192.168.33.101:4440
dataSource.dbCreate = update
dataSource.url = jdbc:h2:file:/var/lib/rundeck/data/rundeckdb;MVCC=true

ansible.cfgは予めこんな設定をしておくとよい。なぜか予めsshしておいてもダメなときがあったので、、、
AnsibleのSSH接続エラーの回避設定

rundeck-ansible-pluginのアップグレード

プラグインが古いと次に使うAnsible Playbook Inlineが入ってないので、新しいのを入れておく。
https://github.com/Batix/rundeck-ansible-plugin

Releaseからjarをwgetして/var/lib/rundeck/libextに置く。古いものはrmするか適当に拡張子でも変えておく。

JobをつくるときのAdd a StepにAnsible Playbook Inlineが出現すればおk。
スクリーンショット 2017-10-22 23.19.01.png

リポジトリを準備

こんな感じで、playbookのリポジトリとroleのリポジトリをつくる。playbookの方にはrequirements.ymlを用意しておき、roleを取得できるようにしておく。

playbookをclone&roleを取得

いい感じにcloneしてもし既存ならpullするshell scriptを書いてもいいが、せっかくなのでAnsible Playbook Inlineを使うことにした。gitモジュールならいい感じにしてくれる。

内容は以下のようにgit cloneと、コマンドでansible-galaxyを叩く感じ。
ansible-galaxyには-f(--force)オプションをつけないと、gitの方でroleが更新されてても反映されないので注意。

ディレクトリは適宜つくっておく。↓のようにする場合/home/rundeck/ansible/までつくってディレクトリのownerをrundeck:rundeckにしておけばよい。
(/home/vagrant/とかにつくるとERROR! the playbook: /home/vagrant/ansible/rundeck-ansible-sample-playbook/site.yml could not be foundになってしまう?ちょっとここは謎。rundeckユーザーに権限がないとダメな仕様なのかも)

- hosts: localhost
  tasks:
    - git:
        repo: "https://github.com/tinoji/rundeck-ansible-sample-playbook"
        dest: "/home/rundeck/ansible/rundeck-ansible-sample-playbook"
      become: yes

    - name: install ansible role by galaxy
      shell: /bin/bash -lc "ansible-galaxy install -f -p roles/ -r requirements.yml"
      args:  
        chdir: /home/rundeck/ansible/rundeck-ansible-sample-playbook
      become: yes



SSHらへんは適宜Key storageを使ったりしてやる。
スクリーンショット 2017-10-22 23.57.03.png

Ansible流す

Add a Stepボタンをクリックし、Ansible playbookを選択。inlineの方でcloneするディレクトリのplaybookを指定。
Extra Ansible argumentsには、以下のように、インベントリファイルオプションとlimitオプションを付ける。
limitを明示的につけないとエラーになる仕様?っぽいです(ERROR! Specified --limit does not match any hosts)。

-i /home/rundeck/ansible/rundeck-ansible-sample-playbook/hosts --limit=all



スクリーンショット 2017-10-23 0.04.50.png

SSHとbecome_methodはこんな感じに。
スクリーンショット 2017-10-23 0.05.34.png

これでRunで動くはず。以下ログ。

/usr/lib64/python2.7/getpass.py:83: GetPassWarning: Can not control echo on the terminal.
  passwd = fallback_getpass(prompt, stream)
Warning: Password input may be echoed.
SSH password: 
Warning: Password input may be echoed.
SUDO password[defaults to SSH password]: 
 [WARNING]: provided hosts list is empty, only localhost is available

PLAY [localhost] ***************************************************************

TASK [Gathering Facts] *********************************************************
ok: [localhost]

TASK [git] *********************************************************************
ok: [localhost]

TASK [install ansible role by galaxy] ******************************************
changed: [localhost]

PLAY RECAP *********************************************************************
localhost                  : ok=3    changed=1    unreachable=0    failed=0   

/usr/lib64/python2.7/getpass.py:83: GetPassWarning: Can not control echo on the terminal.
  passwd = fallback_getpass(prompt, stream)
Warning: Password input may be echoed.
SSH password: 
Warning: Password input may be echoed.
SUDO password[defaults to SSH password]: 

PLAY [servers] *****************************************************************

TASK [Gathering Facts] *********************************************************
ok: [192.168.33.102]

TASK [rundeck-ansible-sample-role : yum install test] **************************
ok: [192.168.33.102]

PLAY RECAP *********************************************************************
192.168.33.102             : ok=2    changed=0    unreachable=0    failed=0   

任意のansible.cfgを使う方法

デフォルトだと/tmp/rundeck/ansible.cfgが使用されてしまう。
https://github.com/Batix/rundeck-ansible-plugin/issues/119
その上現状のプラグインには変更する機能がない。

が、環境変数を変更していい感じにできる。
https://github.com/Batix/rundeck-ansible-plugin/issues/83
Optionで↑のように設定すればおk。

まとめ

これでRundeck WebUIからいじいじするだけでGithubに置いてあるAnsibleが流せるようになった。(もっといい方法ありそう)
RundeckのAnsibleプラグインは最初結構ハマってつらいが、まぁ普通に使える奴だと思う。

4
3
0

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
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?