【Ansible】ansible実行時にSSHパスワード認証を使う方法


はじめに

ansible実行時にSSHパスワード認証を使う方法についてアウトプットしたいと思います。

今回の環境

  • OS
(venv) [root@tspdev01 test_role]# cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)
(venv) [root@tspdev01 test_role]#
  • Ansibleバージョン
(venv) [root@tspdev01 test_role]# ansible --version
ansible 2.9.10
  config file = /root/.ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /root/venv/lib/python2.7/site-packages/ansible
  executable location = /root/venv/bin/ansible
  python version = 2.7.5 (default, Apr  2 2020, 13:16:51) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
(venv) [root@tspdev01 test_role]#

対応内容

ansible.cfgに下記内容を追加

追加内容
[defaults]
ask_pass = True



ansible.cfgの場所は、インストール方法によって異なります。
yum install ansibleにてインストールした際には、下記ディレクトリに存在します。

ansible.cfg
/etc/ansible/ansible.cfg

試しにping疎通を実施

このような感じになります。

実行結果
(venv) [root@tspdev01 test_role]# ansible -i inventory/test01_inventory.ini test_servers -m ping --ask-pass
SSH password:
192.168.56.51 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
(venv) [root@tspdev01 test_role]#

pongであればping疎通ができています。

今回の経緯

最初にansibleを実行した際に、下記エラーが表示されました。

error
(venv) [root@tspdev01 test_role]# ansible -i inventory/test01_inventory.ini test_servers -m ping
192.168.56.51 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).",
    "unreachable": true
}
(venv) [root@tspdev01 test_role]#

エラーの原因を探る過程で、「ansible実行時にSSHパスワード認証を使う方法」の情報にたどり着きました。

参考

ansible で UNREACHABLE! Failed to connect to the host via ssh: Permission denied エラーで対象ホストにログインできない