LoginSignup
24
22

More than 5 years have passed since last update.

ansibleのコマンド実行時の冪等性を簡単にするためのモジュール(ansible2にも対応)

Last updated at Posted at 2016-05-01

ansible2にも対応しました。

ansibleは色々なモジュールがあり便利ですね。
クライアントインストールレスが嬉しいです。
構成管理以外にもリリース作業の自動化や、情報収集や調査タスクにも使えますね。今後ansibleを中心に進めていこうと思っているのですが。

しかし、複数行のコマンドを実行した際の冪等性の考慮が難しいです。参考:http://lab.tricorn.co.jp/kamo/4836
やってられなくはないですが、

- name: check default conf
  stat: path=/etc/nginx/conf.d/default.conf
  register: is_exists
- name: move default conf
  shell: mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.backup
  when: is_exists.stat.md5 is defined
  • YAMLの行数が増えて、可読性が落ちる。
  • register等のプログラミング的な要素が出てきて書きにくい。

そこでモジュールを作りました。

コマンド実行の冪等制のために開発したモジュール
ansible_commanderを作成した。
名前 command モジュールとshellモジュールの進化版なので、command + er を命名

配布場所

git clone https://bitbucket.org/takao_yasuhiro/ansible_commander.git
https://bitbucket.org/takao_yasuhiro/ansible_commander

インストール

下記に配置

 library/commander.py

使い方

  • command:必須、実行するコマンド。複数行呼び出し可能。
  • test_command:(任意) commandを実行前後でチェックするためのコマンド群
  • test_command_rc:(任意)デフォルト 0、test_commandの実行のリターン値の期待値。
  • test_command_msg:(任意)test_commandの標準エラーと標準出力の期待値。
  • chdir:(任意)コマンドを実行する前にcdコマンドでディレクトリを移動する。
  • creates:(任意)指定したファイル名が既に存在する場合、コマンドを実行しない。
  • executable:(任意)実行するシェルを絶対パスで指定する。
  • removes:(任意)指定したファイル名が存在しない場合、コマンドを実行しない。
  • warn:(任意)『no』または『false』を指定した場合、コマンド警告を出さない。

実行サンプル

  - name: 3.4 mysql_secure_installation
    commander:
    args:
      command: |
        expect -c "
        set timeout 5
        spawn /usr/bin/mysql_secure_installation
        expect \"Enter current password for root \"
        send \"\n\"
        expect \"root password\"
        send \"Y\n\"
        expect \"New password:\"
        send \"{{ root_password }}\n\"
        expect \"Re\-enter new password\:\"
        send \"{{ root_password }}\n\"
        expect \"Remove anonymous users?\"
        send \"Y\n\"
        expect \"Disallow root login remotely?\"
        send \"Y\n\"
        expect \"Remove test database and access to it?\"
        send \"Y\n\"
        expect \"Reload privilege tables now\"
        send \"Y\n\"
        interact
        "
      test_command: "echo exit|mysql -u root -p{{root_password}}"

返り値を判別方式

  - name: sample repo
    commander:
    args:
      command: |
        cd /mnt/iscsi/svn
        svnadmin create sample
        chown -R apache:apache sample
      test_command: |
        test -e /mnt/iscsi/svn/sample/
      test_command_rc: "0"
  1. test_commandを実行
  2. test_commandの実行結果が
    • test_command_rcと等しい場合は3へ
    • test_command_rcと等しくない場合はSKIP
  3. commandを実行
  4. 3が実行が正常完了したら、test_commandを再度実行。
  5. 4の実行結果がtest_commandの実行結果が
    • test_command_rcと等しくない場合はエラーとして挙げる。
    • test_command_rcと等しい場合は正常終了

返り値を判別方式

  - name:  bundle install
    commander:
    args:
      command: |
        cd /var/lib/redmine
        source /root/.bash_profile
        bundle install
      test_command: |
        cd /var/lib/redmine
        bundle check
      test_command_msg: "dependencies are satisfied"
  1. test_commandを実行
  2. test_commandの標準出力、標準エラーが
    • test_command_msgと等しい場合は3へ
    • test_command_msgと等しくない場合はSKIP
  3. commandを実行
  4. 3が実行が正常完了したら、test_commandを再度実行。
  5. 4のtest_commandの標準出力、標準エラーが
    • test_command_rcと等しくない場合はエラーとして挙げる。
    • test_command_rcと等しい場合は正常終了

所感

従来の手順書の記載をそのままplaybookに記載すればよく
わかりやすくなったのではと思っています。

24
22
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
24
22