【Infrastructure as code】Ansible入門その3

どうもKuboです。

本日は以前紹介しました、Ansibleの基本的な構成を紹介しようと思います。

前回の記事はこちら

Hello world

環境設定が完了したらまず「Hello world」を出力してみましょう!

ロールのパスはデフォルトでは以下を参照するようです。別の箇所を設定したい場合は、設定ファイルで指定します。

  • カレントディレクトリのroles/
  • /etc/ansible/roles

今回はこんなディレクトリ構成で作ります。

.
└─ roles
    └─ hello_world
        └─ tasks
            └─ main.yml

インベントリファイル

サーバの接続情報などを記載するファイルです。

./inventory

[target]
localhost

プレイブックファイル

サーバとの接続方法やどんなロールを呼び出す(処理は直接記述も出来ます)か、などを定義するファイルです。

今回は「hello_world」ロールを呼び出します。

./playbook.yml

---
- hosts: all
  roles:
    - hello_world

hello_worldロール

ロールはある程度処理を定義する部品のようなイメージです。playbook側で部品を組み合わせてやりたことを実現していきます。

./roles/hello_world/tasks/main.yml

---
- debug:
    msg: "hello world"

 

実行

それでは作成したロールを実行しましょう!

$ ansible-playbook -i inventory playbook.yml

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

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

TASK [hello_world : debug] ******************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "hello world"
}

PLAY RECAP **********************************************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

 

以下の様に「hello world」が表示されていれば成功です。

ok: [localhost] => {
    "msg": "hello world"
}

今回はこれまで次回は基本構文を紹介します。

以上、Kuboでした。

開発日記
この記事の投稿者
kubo

このサイトの共同編集者、プログラマ、素人デザイナー
得意言語:Java,PHP,Python

フォローする
フォローする
スポンサーリンク
WoW creators
タイトルとURLをコピーしました