4

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?

4
  • 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
    Commented Feb 27, 2020 at 7:09
  • But the node needs the agent to connect with my key to other hosts.
    – alexfvolk
    Commented Feb 27, 2020 at 20:25
  • Then the ansible server having its own key is pretty much it.
    – muru
    Commented Feb 27, 2020 at 21:12
  • Actually gave it a try with Control options and it works if I disconnect.
    – alexfvolk
    Commented Feb 27, 2020 at 21:19

1 Answer 1

3

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.

1
  • 2
    Just to clarify, did you add this ssh config to your local machine (MacOS) or the ansible-host... or both? Commented May 17, 2022 at 14:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .