(準備)コントロールノードからマネージドノードにアクセスできるようにする - aktnk/til GitHub Wiki

実施すること

  • ansible をインストールしたコントロールノードのユーザ ansible から、マネージドノードにアクセスできるようにする

手順

設定手順

  1. /etc/hostsにマネージドノードのIPアドレスとホスト名を登録する
    $ sudo nano /etc/hosts
    172.168.0.11	ubuntuTest10
    172.168.0.12	ubuntuTest11
    
  2. 設定ファイル.ansible.cfg にインベントリの定義場所を指定する
    $ nano .ansible.cfg
    [defaults]
    inventory = $HOME/hosts  ← ホームディレクトリ直下のhostsファイルにインベントリを定義する
    
  3. hostsファイルにマネージドノードを指定する
    $ nano hosts
    [testservers]
    ubuntuTest10
    ubuntuTest11
    
  4. 正しく設定ができたか確認する
    $ ansible all --list-hosts
      hosts (2):
        ubuntuTest10
        ubuntuTest11
    

SSHでマネージドノードにアクセスする

  1. ssh ansible@(ホスト名)を実行する
    $ ssh ansible@ubuntuTest10
    The authenticity of host 'ubuntutest10 (172.168.0.11)' can't be established.
    ED25519 key fingerprint is SHA256:ZYVhr7gdVlcC3K8Gn4STmV5N7HD5BPbKx+hhrxuMR/U.
    This host key is known by the following other names/addresses:
        ~/.ssh/known_hosts:1: [hashed name]
    Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
    Warning: Permanently added 'ubuntutest10' (ED25519) to the list of known hosts.
    Welcome to Ubuntu 24.04.3 LTS (GNU/Linux 6.14.0-27-generic x86_64)
    <省略>
    Last login: Sat Aug  9 12:05:47 2025 from 172.168.0.10
    $ exit
    

動作確認

  • 動作確認としてansible からマネージドノードにpingを実行する
  1. ansible all -m pingを実行する
    $ ansible all -m ping
    [WARNING]: Platform linux on host ubuntuTest10 is using the discovered Python
    interpreter at /usr/bin/python3.12, but future installation of another Python
    interpreter could change the meaning of that path. See
    https://docs.ansible.com/ansible-
    core/2.17/reference_appendices/interpreter_discovery.html for more information.
    ubuntuTest10 | SUCCESS => {   ← ansibleコマンドによる ping は成功した
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python3.12"
        },
        "changed": false,
        "ping": "pong"
    }
    

⇒ これでansibleでマネージドノードに指示を出せる準備が整った。