Chapter 05

インベントリーの基本

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

説明

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

演習問題

  • 各設問に従って、「実習環境」の演習で作成した管理対象ノード用のインベントリーファイルを作成してください。
  • 各管理対象ノードのアカウント情報です。
    • ユーザー名 vagrant
    • パスワード vagrant
  • OS が Debian 10 の場合、使用する Python インタープリターの指定が必要です。
    ansible_python_interpreter: /usr/bin/python3
    
  • OS が Alma Linux 8 の場合、使用する Python インタープリターの指定が必要です。
     ansible_python_interpreter: /usr/libexec/platform-python
    

Q1. インベントリーファイル「 hosts1.yml 」を作成してください。

  • ホスト変数 ansible_host , ansible_user , ansible_password を使用します。
  • グループ変数は使用しません。
解答
hosts1.yml
---
all:
  hosts:
    marutamachi:
      ansible_host: 192.168.111.101
      ansible_user: vagrant
      ansible_password: vagrant
    takeyamachi:
      ansible_host: 192.168.111.102
      ansible_user: vagrant
      ansible_password: vagrant
    ebisugawa:
      ansible_host: 192.168.111.103
      ansible_user: vagrant
      ansible_password: vagrant
    nijyo:
      ansible_host: 192.168.111.104
      ansible_user: vagrant
      ansible_password: vagrant
    oshikoji:
      ansible_host: 192.168.111.105
      ansible_user: vagrant
      ansible_password: vagrant
      ansible_python_interpreter: /usr/bin/python3
    oike:
      ansible_host: 192.168.111.106
      ansible_user: vagrant
      ansible_password: vagrant
      ansible_python_interpreter: /usr/libexec/platform-python

Q2. インベントリーファイル「 hosts2.yml 」を作成してください。

  • ホスト変数 ansible_host を使用します。
  • グループ変数 ansible_user , ansible_password を使用します。
解答
hosts2.yml
---
all:
  vars:
    ansible_user: vagrant
    ansible_password: vagrant
  hosts:
    marutamachi:
      ansible_host: 192.168.111.101
    takeyamachi:
      ansible_host: 192.168.111.102
    ebisugawa:
      ansible_host: 192.168.111.103
    nijyo:
      ansible_host: 192.168.111.104
    oshikoji:
      ansible_host: 192.168.111.105
      ansible_python_interpreter: /usr/bin/python3
    oike:
      ansible_host: 192.168.111.106
      ansible_python_interpreter: /usr/libexec/platform-python

Q3. インベントリーファイル「 hosts3.yml 」を作成してください。

  • ホスト変数 ansible_host を使用します。
  • グループ変数 ansible_user , ansible_ssh_private_key_file を使用します。
鍵を作成、登録の方法
  • 鍵を作成
ssh-keygen -t ed25519 -P "" -C ""

管理対象ノードに鍵を登録 (管理対象ホストの数だけ実行)

ssh-copy-id -i ~/.ssh/id_ed25519.pub vagrant@管理対象ノードのIP アドレス

鍵の実行ログ

y_mrok@ctrl:~/code/exam1$ ssh-keygen -t ed25519 -P "" -C ""
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/y_mrok/.ssh/id_ed25519): 
Your identification has been saved in /home/y_mrok/.ssh/id_ed25519
Your public key has been saved in /home/y_mrok/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:Z7vFLVhTzlul7UC6HN6MLYKgIMRTPwpDT6KlYouGl5Y 
The key's randomart image is:
+--[ED25519 256]--+
| o.o             |
|+o= .            |
|+B . o        o .|
|* =o. .      * o.|
|+oE.  . S o = = o|
|.+ . . . + B X = |
|    .   . + X * .|
|           + o   |
|          .      |
+----[SHA256]-----+
y_mrok@ctrl:~/code/exam1$ ls -l ~/.ssh
合計 12
-rw------- 1 y_mrok y_mrok  65  8月 26 21:37 config
-rw------- 1 y_mrok y_mrok 387 10月  2 07:57 id_ed25519
-rw-r--r-- 1 y_mrok y_mrok  82 10月  2 07:57 id_ed25519.pub
y_mrok@ctrl:~/code/exam1$ ssh-copy-id -i ~/.ssh/id_ed25519.pub vagrant@192.168.111.101
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/y_mrok/.ssh/id_ed25519.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
Warning: Permanently added '192.168.111.101' (ECDSA) to the list of known hosts.
vagrant@192.168.111.101's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'vagrant@192.168.111.101'"
and check to make sure that only the key(s) you wanted were added.

y_mrok@ctrl:~/code/exam1$ ssh-copy-id -i ~/.ssh/id_ed25519.pub vagrant@192.168.111.102
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/y_mrok/.ssh/id_ed25519.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
Warning: Permanently added '192.168.111.102' (ECDSA) to the list of known hosts.
vagrant@192.168.111.102's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'vagrant@192.168.111.102'"
and check to make sure that only the key(s) you wanted were added.

y_mrok@ctrl:~/code/exam1$ ssh-copy-id -i ~/.ssh/id_ed25519.pub vagrant@192.168.111.103
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/y_mrok/.ssh/id_ed25519.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
Warning: Permanently added '192.168.111.103' (ECDSA) to the list of known hosts.
vagrant@192.168.111.103's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'vagrant@192.168.111.103'"
and check to make sure that only the key(s) you wanted were added.

y_mrok@ctrl:~/code/exam1$ ssh-copy-id -i ~/.ssh/id_ed25519.pub vagrant@192.168.111.104
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/y_mrok/.ssh/id_ed25519.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
Warning: Permanently added '192.168.111.104' (ECDSA) to the list of known hosts.
vagrant@192.168.111.104's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'vagrant@192.168.111.104'"
and check to make sure that only the key(s) you wanted were added.

y_mrok@ctrl:~/code/exam1$ ssh-copy-id -i ~/.ssh/id_ed25519.pub vagrant@192.168.111.105
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/y_mrok/.ssh/id_ed25519.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
Warning: Permanently added '192.168.111.105' (ECDSA) to the list of known hosts.
vagrant@192.168.111.105's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'vagrant@192.168.111.105'"
and check to make sure that only the key(s) you wanted were added.

y_mrok@ctrl:~/code/exam1$ ssh-copy-id -i ~/.ssh/id_ed25519.pub vagrant@192.168.111.106
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/y_mrok/.ssh/id_ed25519.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
Warning: Permanently added '192.168.111.106' (ECDSA) to the list of known hosts.
vagrant@192.168.111.106's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'vagrant@192.168.111.106'"
and check to make sure that only the key(s) you wanted were added.

y_mrok@ctrl:~/code/exam1$ 

解答
hosts3
---
all:
  vars:
    ansible_user: vagrant
    ansible_ssh_private_key_file: ~/.ssh/id_ed25519
  hosts:
    marutamachi:
      ansible_host: 192.168.111.101
    takeyamachi:
      ansible_host: 192.168.111.102
    ebisugawa:
      ansible_host: 192.168.111.103
    nijyo:
      ansible_host: 192.168.111.104
    oshikoji:
      ansible_host: 192.168.111.105
      ansible_python_interpreter: /usr/bin/python3
    oike:
      ansible_host: 192.168.111.106
      ansible_python_interpreter: /usr/libexec/platform-python

Q4. インベントリーファイル「 hosts4.yml 」を作成してください。

  • ホスト変数 ansible_host を使用します。
  • グループ変数 ansible_user , ansible_ssh_private_key_file を使用します。
  • ( 子 ) グループ db を作成し、ホストを 2 つ登録します。登録するホストはどれでも良いです。
  • ( 子 ) グループ db に含まれるホストには次の 3 つの変数を割り当てます。
    • 変数 db_port / 値 1521
    • 変数 db_user / 値 scott
    • 変数 db_password / 値 tiger
解答
hosts4.yml
---
all:
  vars:
    ansible_user: vagrant
    ansible_ssh_private_key_file: ~/.ssh/id_ed25519
  hosts:
    marutamachi:
      ansible_host: 192.168.111.101
    takeyamachi:
      ansible_host: 192.168.111.102
    ebisugawa:
      ansible_host: 192.168.111.103
    nijyo:
      ansible_host: 192.168.111.104
  children:
    db:
      vars:
        db_port: 1521
        db_user: scott
        db_password: tiger
      hosts:
        oshikoji:
          ansible_host: 192.168.111.105
          ansible_python_interpreter: /usr/bin/python3
        oike:
          ansible_host: 192.168.111.106
          ansible_python_interpreter: /usr/libexec/platform-python