Chapter 06

インベントリーを分割

y_mrok
y_mrok
2021.10.17に更新
このチャプターの目次

説明

この章の説明はこのリンクをクリックしてください。

演習問題

Q1. 「 インベントリーの基本 」の演習問題 Q4 で作成した「 hosts4.yml 」をもとに、インベントリーファイル「 hosts5.yml 」を作成してください。

  • ホスト変数 ansible_host を使用します。
  • グループ変数 ansible_user , ansible_ssh_private_key_file を使用します。
  • ( 子 ) グループ db を作成し、ホストを 2 つ登録します。登録するホストはどれでも良いです。
  • ( 子 ) グループ db に含まれるホストには次の 3 つの変数を割り当てます。
    • 変数 db_port / 値 1521
    • 変数 db_user / 値 scott
    • 変数 db_password / 値 tiger
  • 「 host_vars 」ディレクトリー、「 group_vars 」ディレクトリーを使用します。
解答
  • ツリー構造
.
├── group_vars
│   ├── all.yml
│   └── db.yml
├── host_vars
│   ├── ebisugawa.yml
│   ├── marutamachi.yml
│   ├── nijyo.yml
│   ├── oike:.yml
│   ├── oshikoji.yml
│   └── takeyamachi.yml
└── hosts5.yml
  • インベントリーファイル
hosts5.yml
---
all:
  hosts:
    marutamachi:
    takeyamachi:
    ebisugawa:
    nijyo:
  children:
    db:
      hosts:
        oshikoji:
        oike:
group_vars/all.yml
---
ansible_user: vagrant
ansible_ssh_private_key_file: ~/.ssh/id_ed25519
group_vars/db.yml
---
db_port: 1521
db_user: scott
db_password: tiger
host_vars/marutamachi.yml
---
ansible_host: 192.168.111.101
host_vars/takeyamachi.yml
---
ansible_host: 192.168.111.102
host_vars/ebisugawa.yml
---
ansible_host: 192.168.111.103
host_vars/nijyo.yml
---
ansible_host: 192.168.111.104
host_vars/oshikoji.yml
---
ansible_host: 192.168.111.105
ansible_python_interpreter: /usr/bin/python3
host_vars/oike.yml
---
ansible_host: 192.168.111.106
ansible_python_interpreter: /usr/libexec/platform-python

Q2. ansible-inventory コマンドを使用し、「 インベントリーの基本 」の演習問題 Q1 ~ Q4 で作成したインベントリーファイルと上記で作成した「 hosts5.yml 」ファイルに誤りがないことを確認してください。

解答
y_mrok@ctrl:~/code/chap6$ ansible-inventory -i hosts1.yml --yaml --list
all:
  children:
    ungrouped:
      hosts:
        ebisugawa:
          ansible_host: 192.168.111.103
          ansible_password: vagrant
          ansible_ssh_private_key_file: ~/.ssh/id_ed25519
          ansible_user: vagrant
        marutamachi:
          ansible_host: 192.168.111.101
          ansible_password: vagrant
          ansible_ssh_private_key_file: ~/.ssh/id_ed25519
          ansible_user: vagrant
        nijyo:
          ansible_host: 192.168.111.104
          ansible_password: vagrant
          ansible_ssh_private_key_file: ~/.ssh/id_ed25519
          ansible_user: vagrant
        oike:
          ansible_host: 192.168.111.106
          ansible_password: vagrant
          ansible_python_interpreter: /usr/libexec/platform-python
          ansible_ssh_private_key_file: ~/.ssh/id_ed25519
          ansible_user: vagrant
        oshikoji:
          ansible_host: 192.168.111.105
          ansible_password: vagrant
          ansible_python_interpreter: /usr/bin/python3
          ansible_ssh_private_key_file: ~/.ssh/id_ed25519
          ansible_user: vagrant
        takeyamachi:
          ansible_host: 192.168.111.102
          ansible_password: vagrant
          ansible_ssh_private_key_file: ~/.ssh/id_ed25519
          ansible_user: vagrant
y_mrok@ctrl:~/code/chap6$ 
y_mrok@ctrl:~/code/chap6$ ansible-inventory -i hosts2.yml --yaml --list
all:
  children:
    ungrouped:
      hosts:
        ebisugawa:
          ansible_host: 192.168.111.103
          ansible_password: vagrant
          ansible_ssh_private_key_file: ~/.ssh/id_ed25519
          ansible_user: vagrant
        marutamachi:
          ansible_host: 192.168.111.101
          ansible_password: vagrant
          ansible_ssh_private_key_file: ~/.ssh/id_ed25519
          ansible_user: vagrant
        nijyo:
          ansible_host: 192.168.111.104
          ansible_password: vagrant
          ansible_ssh_private_key_file: ~/.ssh/id_ed25519
          ansible_user: vagrant
        oike:
          ansible_host: 192.168.111.106
          ansible_password: vagrant
          ansible_python_interpreter: /usr/libexec/platform-python
          ansible_ssh_private_key_file: ~/.ssh/id_ed25519
          ansible_user: vagrant
        oshikoji:
          ansible_host: 192.168.111.105
          ansible_password: vagrant
          ansible_python_interpreter: /usr/bin/python3
          ansible_ssh_private_key_file: ~/.ssh/id_ed25519
          ansible_user: vagrant
        takeyamachi:
          ansible_host: 192.168.111.102
          ansible_password: vagrant
          ansible_ssh_private_key_file: ~/.ssh/id_ed25519
          ansible_user: vagrant
y_mrok@ctrl:~/code/chap6$ 
y_mrok@ctrl:~/code/chap6$ ansible-inventory -i hosts3.yml --yaml --list
all:
  children:
    ungrouped:
      hosts:
        ebisugawa:
          ansible_host: 192.168.111.103
          ansible_ssh_private_key_file: ~/.ssh/id_ed25519
          ansible_user: vagrant
        marutamachi:
          ansible_host: 192.168.111.101
          ansible_ssh_private_key_file: ~/.ssh/id_ed25519
          ansible_user: vagrant
        nijyo:
          ansible_host: 192.168.111.104
          ansible_ssh_private_key_file: ~/.ssh/id_ed25519
          ansible_user: vagrant
        oike:
          ansible_host: 192.168.111.106
          ansible_python_interpreter: /usr/libexec/platform-python
          ansible_ssh_private_key_file: ~/.ssh/id_ed25519
          ansible_user: vagrant
        oshikoji:
          ansible_host: 192.168.111.105
          ansible_python_interpreter: /usr/bin/python3
          ansible_ssh_private_key_file: ~/.ssh/id_ed25519
          ansible_user: vagrant
        takeyamachi:
          ansible_host: 192.168.111.102
          ansible_ssh_private_key_file: ~/.ssh/id_ed25519
          ansible_user: vagrant
y_mrok@ctrl:~/code/chap6$ 
y_mrok@ctrl:~/code/chap6$ ansible-inventory -i hosts4.yml --yaml --list
all:
  children:
    db:
      hosts:
        oike:
          ansible_host: 192.168.111.106
          ansible_python_interpreter: /usr/libexec/platform-python
          ansible_ssh_private_key_file: ~/.ssh/id_ed25519
          ansible_user: vagrant
          db_password: tiger
          db_port: 1521
          db_user: scott
        oshikoji:
          ansible_host: 192.168.111.105
          ansible_python_interpreter: /usr/bin/python3
          ansible_ssh_private_key_file: ~/.ssh/id_ed25519
          ansible_user: vagrant
          db_password: tiger
          db_port: 1521
          db_user: scott
    ungrouped:
      hosts:
        ebisugawa:
          ansible_host: 192.168.111.103
          ansible_ssh_private_key_file: ~/.ssh/id_ed25519
          ansible_user: vagrant
        marutamachi:
          ansible_host: 192.168.111.101
          ansible_ssh_private_key_file: ~/.ssh/id_ed25519
          ansible_user: vagrant
        nijyo:
          ansible_host: 192.168.111.104
          ansible_ssh_private_key_file: ~/.ssh/id_ed25519
          ansible_user: vagrant
        takeyamachi:
          ansible_host: 192.168.111.102
          ansible_ssh_private_key_file: ~/.ssh/id_ed25519
          ansible_user: vagrant
y_mrok@ctrl:~/code/chap6$ 
y_mrok@ctrl:~/code/chap6$ ansible-inventory -i hosts5.yml --yaml --list
all:
  children:
    db:
      hosts:
        oike:
          ansible_host: 192.168.111.106
          ansible_python_interpreter: /usr/libexec/platform-python
          ansible_ssh_private_key_file: ~/.ssh/id_ed25519
          ansible_user: vagrant
          db_password: tiger
          db_port: 1521
          db_user: scott
        oshikoji:
          ansible_host: 192.168.111.105
          ansible_python_interpreter: /usr/bin/python3
          ansible_ssh_private_key_file: ~/.ssh/id_ed25519
          ansible_user: vagrant
          db_password: tiger
          db_port: 1521
          db_user: scott
    ungrouped:
      hosts:
        ebisugawa:
          ansible_host: 192.168.111.103
          ansible_ssh_private_key_file: ~/.ssh/id_ed25519
          ansible_user: vagrant
        marutamachi:
          ansible_host: 192.168.111.101
          ansible_ssh_private_key_file: ~/.ssh/id_ed25519
          ansible_user: vagrant
        nijyo:
          ansible_host: 192.168.111.104
          ansible_ssh_private_key_file: ~/.ssh/id_ed25519
          ansible_user: vagrant
        takeyamachi:
          ansible_host: 192.168.111.102
          ansible_ssh_private_key_file: ~/.ssh/id_ed25519
          ansible_user: vagrant
y_mrok@ctrl:~/code/chap6$