3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[Ansible]暗黙的なlocalhostにご用心

Posted at

結論

  • Ansibleは暗黙的にlocalhostのインベントリ情報が作成されている
  • localhostをインベントリで定義した場合、暗黙的に宣言されている変数も追加しておく必要がある
  • トラブル回避のために、プロジェクト開始当初から明示的にlocalhostを定義するのがおすすめ

事象

AnsibleでvCenterのようなAPIを使った操作をする際、vCenterログイン情報はグループvcenterに記載して実際の作業はlocalhostというパターンがよくあります。
タスクごとにdelegate_to: localhostを書くことでも対応はできるのですが全タスクに書くのがとても面倒です。

そういったとき、グループvcenterにホストlocalhostを含めることでPlaybook側は、hosts: vcenterのみ記載するだけでよく、delegate_to: localhostもなくPlaybookがスッキリします。

具体的には以下のような記載

playbook.yml
---
- hosts: vcenter
  gather_facts: false
  tasks:
    - name: vcenter VM作成
      community.vmware.vmware_guest:
        ~~
hosts.yml
---
all:
  children:
    vcenter:
      hosts:
        localhost:

しかし、このまま動かすとSSHでアクセスしようとして以下のようなエラーが出てしまいます。

PLAY [vcenter] ******************************************************************************************************************************************************************************************************************************

TASK [vCenter VM作成] ***********************************************************************************************************************************************************************************************************************
fatal: [localhost]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: root@localhost: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).", "unreachable": true}

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

解決方法

Ansibleはlocalhostが定義されていないときに、わざわざlocalで接続しに行くlocalhostを作成しています。
明示的にlocalhostが定義された場合、上記の設定がなくなりAnsibleのデフォルト接続方法であるSSHで接続しに行ってエラーが起きています。

そのため、暗黙的なlocalhostで定義されていた情報を明示的に書いてあげることで解決ができます。

記載場所は色々ありますが、今回は手っ取り早くhost_vars/localhost.ymlを作成してそこに以下の変数を定義します。

暗黙的に宣言されている変数

host_vars/localhost.yml
---
ansible_connection: local
ansible_python_interpreter: "{{ ansible_playbook_python }}"

余談

初歩的な内容ではありますが、localhostへのアクセスはよく使われるポイントであり、初心者が詰まりやすいポイントです。

また、環境差異が起きやすいポイントでもあります。
例えば、Ansible実行サーバが自身のSSH公開鍵を~/.ssh/authorized_keysに登録しているなどしているとエラーなく通ってしまいます。

テスト環境では、うまく行ったのに本番環境ではうまくいかなかった。などが起こってしまうので、最初から明示的にlocalhostを定義しておいた方がトラブルなくなるのではないかと筆者は考えます。

参考リンク

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?