# SSH-keys forwarding to Execution Environment for promptless runs with ansible-navigator

**URL:** <https://forum.ansible.com/t/ssh-keys-forwarding-to-execution-environment-for-promptless-runs-with-ansible-navigator/4972>\
**Category:** Guides, FAQs & Howtos\
**Tags:** ansible-navigator, howto, ee\
**Created:** [April 16, 2024, 10:52am UTC](https://forum.ansible.com/t/ssh-keys-forwarding-to-execution-environment-for-promptless-runs-with-ansible-navigator/4972 "2024-04-16T10:52:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andersson007](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/andersson007/32/89_2.png) [@Andersson007](https://forum.ansible.com/u/Andersson007)\
**Post date:** [April 16, 2024, 10:52am UTC](https://forum.ansible.com/t/ssh-keys-forwarding-to-execution-environment-for-promptless-runs-with-ansible-navigator/4972/1 "2024-04-16T10:52:24Z")

</div>

I assume you have your environment set up, built your custom EE/use one of community EEs. If not, first read the [Getting started with EE guide](https://docs.ansible.com/ansible/devel/getting_started_ee/index.html).

You also have your SSH keys generated and the public key copied to your target host via `ssh-copy-id`.

1. Create a `test_remote.yml` playbook to test SSH-keys forwarding.

```
cat > test_remote.yml<<EOF
- name: Gather and print facts
  hosts: YOUR_INVENTORY_HOST
  remote_user: YOUR_REMOTE_USER
  gather_facts: true
  tasks:

  - name: Print facts
    ansible.builtin.debug:
      var: ansible_facts
EOF

```

2. Add your private key to `ssh-agent`.

```bash
eval $(ssh-agent)
ssh-add </path/to/your/private/key>

```

3. Test it:

```bash
ansible-navigator run test_remote.yml -i inventory --execution-environment-image YOUR_EE --mode stdout --pull-policy missing

```

Thanks for reading, any feedback is welcome:)

---

<div class="post-metadata">

**Author:** ![jpmens](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jpmens/32/5543_2.png) [@jpmens](https://forum.ansible.com/u/jpmens)\
**Post date:** [April 16, 2024, 3:19pm UTC](https://forum.ansible.com/t/ssh-keys-forwarding-to-execution-environment-for-promptless-runs-with-ansible-navigator/4972/2 "2024-04-16T15:19:22Z")

</div>

Good write-up, thank you. And quite timely, as I was just experimenting with this for `ansible-runner` this morning.

In **Runner** this doesn’t work for me. With a similar configuration as yours, I have:

```console
$ echo YOUR_INVENTORY_HOST > inventory/hosts
$ cat env/settings
---
process_isolation: true
process_isolation_executable: podman
container_image: <my EE>

$ mkdir project
$ cat > project/test_remote.yml<<EOF
- name: Gather and print facts
  hosts: YOUR_INVENTORY_HOST
  remote_user: YOUR_REMOTE_USER
  gather_facts: true
  tasks:

  - name: Print facts
    ansible.builtin.debug:
      var: ansible_facts
EOF

$ ansible-runner run . -p test_remote.yml
TASK [Gathering Facts] *********************************************************
fatal: [YOUR_INVENTORY_HOST]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh:

```

Only after ensuring the SSH agen’t directory is in `~/.ssh`, additionally adding an enviroment variable to Runner, and bind-mounting the hosts .ssh directory do I succeed:

```console
$ eval $(ssh-agent -a ~/.ssh/auth-sock)

$ ssh-add my.key
Identity added: my.id_rsa (Ansible user)

$ echo 'SSH_AUTH_SOCK: "/root/.ssh/auth-sock"' > env/envvars # the EE will get this

$ ansible-runner run . --container-volume-mount $HOME/.ssh/:/root/.ssh/ -p test_remote.yml                                                 
                                                                                
PLAY [Gather and print facts] **************************************************
                                                                                
TASK [Gathering Facts] *********************************************************
ok: [YOUR_INVENTORY_HOST]              
...

```

I don’t know what this is due to, as [the documentation clearly says](https://ansible.readthedocs.io/projects/runner/en/latest/execution_environments/#ssh-symlinks) the SSH directory will be bind-mounted, but this doesn’t occur here, which may be due to all sorts of reasons. 😊

---

<div class="post-metadata">

**Author:** ![Andersson007](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/andersson007/32/89_2.png) [@Andersson007](https://forum.ansible.com/u/Andersson007)\
**Post date:** [April 17, 2024, 7:12am UTC](https://forum.ansible.com/t/ssh-keys-forwarding-to-execution-environment-for-promptless-runs-with-ansible-navigator/4972/3 "2024-04-17T07:12:12Z")

</div>

> [@jpmens](#):
>
> I was just experimenting with this for `ansible-runner` this morning.

@jpmens thank you for the feedback!  
I did it for ansible-navigator. Sorry for not specifying this which could cause some confusion: now mentioned it explicitly in the title.  
Didn’t play with runner yet. I think they would appreciate any docfixes with things you discovered.

Did you try the guide with Navigator?

---

<div class="post-metadata">

**Author:** ![jpmens](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jpmens/32/5543_2.png) [@jpmens](https://forum.ansible.com/u/jpmens)\
**Post date:** [April 17, 2024, 7:56am UTC](https://forum.ansible.com/t/ssh-keys-forwarding-to-execution-environment-for-promptless-runs-with-ansible-navigator/4972/4 "2024-04-17T07:56:51Z")

</div>

> [@Andersson007](#):
>
> Did you try the guide with Navigator?

Your instructions for Navigator work well.

---

<div class="post-metadata">

**Author:** ![Andersson007](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/andersson007/32/89_2.png) [@Andersson007](https://forum.ansible.com/u/Andersson007)\
**Post date:** [April 17, 2024, 10:37am UTC](https://forum.ansible.com/t/ssh-keys-forwarding-to-execution-environment-for-promptless-runs-with-ansible-navigator/4972/5 "2024-04-17T10:37:03Z")

</div>

@jpmens thanks a lot for the feedback!
