読者です 読者をやめる 読者になる 読者になる

ITの隊長のブログ

ITの隊長のブログです。いや、まだ隊長と呼べるほどには至っていないけど、日々がんばります。CakePHPとPlayFrameworkを使って仕事しています。最近はAngular2をさわりはじめたお(^ω^ = ^ω^)

とりあえず複数のDockerコンテナにAnsibleでプロビジョニング

Docker Ansible CentOS CentOS7

スポンサードリンク

photo by saifikhan_org

dockerとかansibleの導入記事をまだ投稿していないと思うので、時間が錯誤していますが、とりあえず。

環境

バージョン

$ docker -v
Docker version 1.9.1, build a34a1d5

$ ansible --version
ansible 1.9.4
  configured module search path = None

$ cat /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core) 

docker container

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS                    PORTS               NAMES
7e5fc764536e        test/ansible        "/usr/sbin/sshd -D"   4 days ago          Exited (255) 3 days ago                       test03
d15b06579e15        test/ansible        "/usr/sbin/sshd -D"   4 days ago          Exited (255) 3 days ago                       test02
a930bc25bdcf        test/ansible        "/usr/sbin/sshd -D"   4 days ago          Exited (255) 3 days ago                       test01

準備

hosts(インベントリ)ファイルを用意

  • check_docker_container_ip.sh
#!/bin/bash
IP_ADDRESS=()
for i in test01 test02 test03
do
  IP_ADDRESS+=(`sudo docker inspect --format '{{.NetworkSettings.IPAddress}}' $i`)
done

echo '[test]' >hosts
echo ${IP_ADDRESS[@]} | tr ' ' '\n' >>hosts
$ bash check_docker_container_ip.sh
$ cat hosts
[test]
172.17.0.2
172.17.0.3
172.17.0.4

ansible疎通テスト

$ ansible all -i hosts -m ping
172.17.0.2 | FAILED => SSH Error: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
    while connecting to 172.17.0.2:22
It is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue.
172.17.0.4 | FAILED => SSH Error: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
    while connecting to 172.17.0.4:22
It is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue.
172.17.0.3 | FAILED => SSH Error: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
    while connecting to 172.17.0.3:22
It is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue.

?!(´・ω・`)

なんかうまくいかない。ググッて見る。

どうやら、認証keyが間違っているらしい。指定してあげる。

$ ansible all -i hosts -m ping -u docker --private-key ~/.ssh/id_rsa
172.17.0.4 | success >> {
    "changed": false, 
    "ping": "pong"
}

172.17.0.2 | success >> {
    "changed": false, 
    "ping": "pong"
}

172.17.0.3 | success >> {
    "changed": false, 
    "ping": "pong"
}

おk

playbookを作成

yamlだって、jsonみたいなやつらしい(構造は違うけど)

lamp環境を作るyamlを用意(リスペクトしたファイルなので、そのままです)

---
- hosts: all
  sudo: yes
  vars:
    mysql_user_name: vagrant
    mysql_user_password: vagrant
  tasks:

    #
    # Apache
    #
    - name: Apacheをインストール
      yum: name=httpd

    - name: Apacheを起動
      service: name=httpd state=started enabled=yes

    - name: DocumentRootを/vagrantに変更
      replace: 
        dest=/etc/httpd/conf/httpd.conf
        regexp='DocumentRoot "/var/www/html"'
        replace='DocumentRoot "/vagrant"'
      notify:
        - restart httpd

    - name: .htaccessを有効にする
      replace: 
        dest=/etc/httpd/conf/httpd.conf
        regexp='AllowOverride None'
        replace='AllowOverride All'
      notify:
        - restart httpd

    #
    # PHP
    #
    - name: PHPをインストール
      yum: name={{item}}
      with_items:
        - php
        - php-mbstring
        - php-mysql

    - name: PHPをタイムゾーンの設定
      replace: >
        dest=/etc/php.ini
        regexp="^;date\.timezone ="
        replace="date.timezone = Asia/Tokyo"

    #
    # MySQL5.6
    #
    - name: MySQL5.6のリポジトリを追加
      command: >
        yum -y install http://dev.mysql.com/get/mysql-community-release-el6-5.noarch.rpm
        creates=/etc/yum.repos.d/mysql-community.repo

    - name: MySQLをインストール
      yum: name={{item}}
      with_items:
        - mysql-server
        - MySQL-python

    - name: MySQLを起動
      service: name=mysqld state=started enabled=yes

    - name: MySQLのユーザーを追加
      mysql_user: name={{ mysql_user_name }} password={{ mysql_user_password }} priv=*.*:ALL

  handlers:
    - name: restart httpd
      service: name=httpd state=restarted

実行

んで、実行

$ ansible-playbook -i ./hosts playbook.yml -u docker --private-key ~/.ssh/id_rsa

PLAY [all] ******************************************************************** 

GATHERING FACTS *************************************************************** 
ok: [172.17.0.3]
ok: [172.17.0.2]
ok: [172.17.0.4]

TASK: [Apacheをインストール] ******************************************* 
changed: [172.17.0.4]
changed: [172.17.0.2]
changed: [172.17.0.3]

...

おおお!

すごい。これはすごい。

TASK: [MySQLを起動] ******************************************************** 
failed: [172.17.0.3] => {"failed": true}
msg: 2016-01-13 02:26:48 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2016-01-13 02:26:48 0 [Note] /usr/sbin/mysqld (mysqld 5.6.28) starting as process 634 ...
2016-01-13 02:26:49 634 [Note] InnoDB: Using atomics to ref count buffer pool pages
2016-01-13 02:26:49 634 [Note] InnoDB: The InnoDB memory heap is disabled
2016-01-13 02:26:49 634 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2016-01-13 02:26:49 634 [Note] InnoDB: Memory barrier is not used
2016-01-13 02:26:49 634 [Note] InnoDB: Compressed tables use zlib 1.2.3
2016-01-13 02:26:49 634 [Note] InnoDB: Using Linux native AIO
2016-01-13 02:26:49 634 [Note] InnoDB: Using CPU crc32 instructions
2016-01-13 02:26:49 634 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2016-01-13 02:26:49 634 [Note] InnoDB: Completed initialization of buffer pool
2016-01-13 02:26:50 634 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
2016-01-13 02:26:50 634 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2016-01-13 02:26:50 634 [Note] InnoDB: Database physically writes the file full: wait...
2016-01-13 02:26:50 634 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
2016-01-13 02:26:50 634 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
2016-01-13 02:26:51 634 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2016-01-13 02:26:51 634 [Warning] InnoDB: New log files created, LSN=45781
2016-01-13 02:26:51 634 [Note] InnoDB: Doublewrite buffer not found: creating new
2016-01-13 02:26:51 634 [Note] InnoDB: Doublewrite buffer created
2016-01-13 02:26:51 634 [Note] InnoDB: 128 rollback segment(s) are active.
2016-01-13 02:26:51 634 [Warning] InnoDB: Creating foreign key constraint system tables.
2016-01-13 02:26:51 634 [Note] InnoDB: Foreign key constraint system tables created
2016-01-13 02:26:51 634 [Note] InnoDB: Creating tablespace and datafile system tables.
2016-01-13 02:26:51 634 [Note] InnoDB: Tablespace and datafile system tables created.
2016-01-13 02:26:51 634 [Note] InnoDB: Waiting for purge to start
2016-01-13 02:26:51 634 [Note] InnoDB: 5.6.28 started; log sequence number 0
2016-01-13 02:26:52 634 [Note] Binlog end
2016-01-13 02:26:52 634 [Note] InnoDB: FTS optimize thread exiting.
2016-01-13 02:26:52 634 [Note] InnoDB: Starting shutdown...
2016-01-13 02:26:54 634 [Note] InnoDB: Shutdown completed; log sequence number 1625977
2016-01-13 02:26:54 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2016-01-13 02:26:54 0 [Note] /usr/sbin/mysqld (mysqld 5.6.28) starting as process 656 ...
2016-01-13 02:26:54 656 [Note] InnoDB: Using atomics to ref count buffer pool pages
2016-01-13 02:26:54 656 [Note] InnoDB: The InnoDB memory heap is disabled
2016-01-13 02:26:54 656 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2016-01-13 02:26:54 656 [Note] InnoDB: Memory barrier is not used
2016-01-13 02:26:54 656 [Note] InnoDB: Compressed tables use zlib 1.2.3
2016-01-13 02:26:54 656 [Note] InnoDB: Using Linux native AIO
2016-01-13 02:26:54 656 [Note] InnoDB: Using CPU crc32 instructions
2016-01-13 02:26:54 656 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2016-01-13 02:26:54 656 [Note] InnoDB: Completed initialization of buffer pool
2016-01-13 02:26:54 656 [Note] InnoDB: Highest supported file format is Barracuda.
2016-01-13 02:26:55 656 [Note] InnoDB: 128 rollback segment(s) are active.
2016-01-13 02:26:55 656 [Note] InnoDB: Waiting for purge to start
2016-01-13 02:26:55 656 [Note] InnoDB: 5.6.28 started; log sequence number 1625977
2016-01-13 02:26:55 656 [Note] Binlog end
2016-01-13 02:26:55 656 [Note] InnoDB: FTS optimize thread exiting.
2016-01-13 02:26:55 656 [Note] InnoDB: Starting shutdown...
2016-01-13 02:26:56 656 [Note] InnoDB: Shutdown completed; log sequence number 1625987

ほげーーーーーーーーー^q^

突然のエラー文

どうやら、mysqlが起動しないっぽい。

でももっかい実行したらうまくいった。。。なんだったんだ。。。?

本当にインストールされているか、テストしてみる。

それぞれのサーバ確認

$ curl -v telnet://172.17.0.2:80
...
* Connected to 172.17.0.2 (172.17.0.2) port 80 (#0)

$ curl -v telnet://172.17.0.3:80
...
* Connected to 172.17.0.3 (172.17.0.3) port 80 (#0)

$ curl -v telnet://172.17.0.4:80
...
* Connected to 172.17.0.4 (172.17.0.4) port 80 (#0)

感想

おkっぽい。できた!

さて、playbookの調整とjenkinsとの連携を急ぐ。