1

I want to connect to a host from which I will run ansible playbook using screen. Using screen allows me to have the playbook finish in case my ssh connection gets interrupted or terminated. So far no problem here.

My ansible host needs password or ssh key to connect to the hosts in the inventory file. So instead of creating a new private key, I use ssh agent forwarding when I connect to my ansible instance.

# from my macOS
ssh-add -K
ssh -A ansible-host

# from ansible-host
screen -R run-playbook-example
ansible-playbook -i inventory/hosts my-playbook.yml

Everything works great so far. Until I simulate losing connection. That also means the agent forwarding no longer works, and ansible-host cannot authenticate to any of the hosts. Which defeats the whole point of using screen.

Is there anyway to do both? 1) Avoid creating a new ssh private key and storing it on ansible host and use ssh agent forwarding or some other trick 2) Be able to run in screen without worrying if you lose ssh connection to ansible-host and have your playbook run regardless if you disconnect.

Something tells me it is not possible. But I really hate to have to store a private key that several people will have access to just so I can use screen and be immune to ssh connection issues. Any ideas, tips, different ways of approaching this problem?

  • 1
    You can use ControlMaster and ControlPersist to set up the SSH connection in advance and hold it alive until the end of the play book run. Since you won't need to reconnect to the node, losing access to the agent shouldn't affect it. – muru Feb 27 at 7:09
  • But the node needs the agent to connect with my key to other hosts. – alexfvolk Feb 27 at 20:25
  • Then the ansible server having its own key is pretty much it. – muru Feb 27 at 21:12
  • Actually gave it a try with Control options and it works if I disconnect. – alexfvolk Feb 27 at 21:19
1

If anyone is interested in the solution, you will need to add the following options to your ssh config. It is called ssh multiplexing and it saves the ssh connections for resuse:

Host *
  Controlmaster auto
  Controlpath /tmp/ssh-%r@%h:%p
  ControlPersist yes

So now when you disconnect from your screen session on host where ansible is running, it will continue running and reuse the existing ssh connections provided they were established prior to you disconnecting.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.