演習 1.8 - ボーナスラボ

Read this in other languages: uk English, japan日本語, brazil Portugues do Brasil.

あなたは既にラボを完了しています・・・、が、さらに先に進みたい方は是非このボーナスラボにチャレンジしてみてください。

ステップ 1.8.1 - ボーナスラボ: アドホックコマンド

アドホックコマンドを使って、適当なコメント付きで新しいユーザー “testuser” を node1node3 に作成します。node2 に作成してはいけません。実行後、想定通り作成できていることも確認します。

ヒント

権限昇格の記述を忘れないこと!

答えは以下の通り

コマンドは次のようになります。

[student<X>@ansible-1 ansible-files]$ ansible-doc -l | grep -i user
[student<X>@ansible-1 ansible-files]$ ansible-doc user
[student<X>@ansible-1 ansible-files]$ ansible node1,node3 -m user -a "name=testuser comment='Test D User'" -b
[student<X>@ansible-1 ansible-files]$ ansible node1,node3 -m command -a " id testuser" -b
[student<X>@ansible-1 ansible-files]$ ansible node2 -m command -a " id testuser" -b
[student<X>@ansible-1 ansible-files]$ ansible node1,node3 -m user -a "name=testuser state=absent remove=yes" -b
[student<X>@ansible-1 ansible-files]$ ansible web -m command -a " id testuser" -b

ステップ 1.8.2 - ボーナスラボ: テンプレートと変数

皆さんは今までのラボで、Ansibleのテンプレート、変数、そしてハンドラについての基本を既に学んでいます。これらすべてを組み合わせてみましょう。

httpd.conf のリッスンポートを都度 vi 等で編集してコピーするのではなく、変数としてテンプレートの中で定義し、その変数の値を変数ファイルを使って与える方法について考えてみます。

ヒント

group_varshost_vars ディレクトリを覚えていますか? そうでない場合は、「演習1.4 変数を使ってみる」の章を参照してください。

回答は以下の通り

変数を定義します:

グループ変数を定義する group_vars/web に以下を記述します

listen_port: 8080

node2 は設定が異なるので、ホスト変数を定義する host_vars/node2 に以下を記述します

listen_port: 80

テンプレートを準備します

[...]
Listen {{ listen_port }}
[]...]

Playbook を作成します。

Playbook apache_config_tpl.yml を以下の内容で作成します。

---
- name: Apache httpd.conf
  hosts: web
  become: yes
  tasks:
  - name: Create Apache configuration file from template
    template:
      src: httpd.conf.j2
      dest: /etc/httpd/conf/httpd.conf
    notify:
        - restart apache
  handlers:
    - name: restart apache
      service:
        name: httpd
        state: restarted

実行し確認します

まずは playbook を実行し、curl コマンドで、 node1node3 にポート 8080 そして node2 にポート 80 で接続してみます。

[student<X>@ansible-1 ansible-files]$ ansible-playbook apache_config_tpl.yml
[...]
[student<X>@ansible-1 ansible-files]$ curl http://18.195.235.231:8080
<body>
<h1>This is a development webserver, have fun!</h1>
</body>
[student<X>@ansible-1 ansible-files]$ curl http://35.156.28.209:80
<body>
<h1>This is a production webserver, take care!</h1>
</body>

Ansible Engine ワークショップ表紙に戻る