大网数据广告活动
服务器运维问答

ansible ssh connections with two factor auth

· 52CE 我爱测

I'm setting ansible to manage a whole farm of servers. My approach is the following: Allow a user to connect to all servers protecting his connections with a heavy RSA key, passphrase protected, and user password. In order to aut…

问题原文

原帖链接 · Abel · CC BY-SA 3.0

I'm setting ansible to manage a whole farm of servers. My approach is the following:

Allow a user to connect to all servers protecting his connections with a heavy RSA key, passphrase protected, and user password.

In order to automate as much as possible the process I would like to pass the RSA passphrase and the user password in a vault file or an encrypted string.

When I'm testing (passing the passwords manually) with just a ping to a test server if I set up in client both authentication methods (publickey + user password), I'm prompted for RSA passphrase but no password, and obviously the execution of the playbook fails.

If i just set up user password as authentication method the playbook is executed normally.

If i just set up RSA key with it's passphrase authentication the job does not end, this the output I've had in my screen for at least 30 minutes (time for research, coffee, ..)

PLAY [all] ******************************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************
Enter passphrase for key '/home/users/<user_name>/.ssh/id_rsa': 
ok: [<test_server_ip_address>]

TASK [include vars] *********************************************************************************************************************************************************
ok: [<test_server_ip_address>]

TASK [test connection] ******************************************************************************************************************************************************

And anything else.

The playbook that I'm trying to execute is this one

---
- hosts: all
  tasks:
    - name: test connection
      ping:

And the command to execute the playbok the next one:

ansible-playbook -i ansible_hosts/test_hosts.yml playbooks/ping.yml

The content of test_host.yml :

all:
  hosts:
    <test_server_ip_address>

My question is:

Is it possible to set up double factor authentication for ansible? An if yes, can anyone redirect me in the right direction please?

已采纳解答

原帖链接 · Konstantin Suvorov · CC BY-SA 3.0

I doubt you can use ssh key's passphrase from Ansible vault, but the general 2-factor workflow is as follows:

On the server side, enable 2-factor Auth with

AuthenticationMethods "publickey,password"

in sshd_config.

On the client side (Ansible):

  1. Setup ssh-agent, add password protected key to agent when connection is required
  2. Define ansible_password in Ansible vault for every host
  3. Run playbook

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-12-20)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。