# Default code to add public ssh key fails

**URL:** <https://forum.ansible.com/t/default-code-to-add-public-ssh-key-fails/3702>\
**Category:** Get Help\
**Tags:** ubuntu, awx\
**Created:** [February 6, 2024, 1:01pm UTC](https://forum.ansible.com/t/default-code-to-add-public-ssh-key-fails/3702 "2024-02-06T13:01:05Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![gothsome](https://avatars.discourse-cdn.com/v4/letter/g/278dde/32.png) [@gothsome](https://forum.ansible.com/u/gothsome)\
**Post date:** [February 13, 2024, 2:23pm UTC](https://forum.ansible.com/t/default-code-to-add-public-ssh-key-fails/3702/21 "2024-02-13T14:23:03Z")

</div>

my playbook:

```auto
---
- hosts: localhost
  gather_facts: no
  vars:
    key_url: "https://raw.githubusercontent.com/gsg-git/awx_pub/main/awxtestpub"
    key_content: "{{ lookup('url', key_url, split_lines=false) }}"
    conversion_cmd: 'ssh-keygen -i -m RFC4716 -f /dev/stdin <<<"{{ key_content }}"'
    new_key_content: "{{ lookup('pipe', conversion_cmd) }}"
  tasks:
    - debug: msg="{{ new_key_content }}"

```

I think i know the problem now.  
I started the playbook with at least 3 Hosts, but the log still shows one and everywhere “localhost”  
I thing in my playbook i should use instead of localhost all

---

<div class="post-metadata">

**Author:** ![gothsome](https://avatars.discourse-cdn.com/v4/letter/g/278dde/32.png) [@gothsome](https://forum.ansible.com/u/gothsome)\
**Post date:** [February 13, 2024, 2:35pm UTC](https://forum.ansible.com/t/default-code-to-add-public-ssh-key-fails/3702/22 "2024-02-13T14:35:19Z")

</div>

I have luck and no luck:  
The playbook now has the IP of the devices and the correct count, but still nothing on both machines…

 ![grafik](https://us1.discourse-cdn.com/ansible/original/2X/7/70211abc21506bcbce276b6482810016b4aae174.png)

---

<div class="post-metadata">

**Author:** ![kurokobo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kurokobo/32/182_2.png) [@kurokobo](https://forum.ansible.com/u/kurokobo)\
**Post date:** [February 13, 2024, 2:41pm UTC](https://forum.ansible.com/t/default-code-to-add-public-ssh-key-fails/3702/23 "2024-02-13T14:41:35Z")

</div>

🤔

Your playbook only contains `debug` task, so it does almost nothing; just displaying converted new key content.

The playbook that @shertel provided is just an example that shows the way to convert keys. You should update your playbook to use `new_key_content` variable as the `key` for `ansible.posix.authorized_key`.

---

<div class="post-metadata">

**Author:** ![gothsome](https://avatars.discourse-cdn.com/v4/letter/g/278dde/32.png) [@gothsome](https://forum.ansible.com/u/gothsome)\
**Post date:** [February 13, 2024, 3:01pm UTC](https://forum.ansible.com/t/default-code-to-add-public-ssh-key-fails/3702/24 "2024-02-13T15:01:42Z")

</div>

maybe its the time, but i dnot get what you mean.  
I thought i cann add the key conversion part to the edited key import code example:

```auto

- name: Set authorized keys taken from url
  ansible.posix.authorized_key:
    user: charlie
    state: present
    key: https://github.com/charlie.keys

```

- 

```auto
    key_content: "{{ lookup('url', key_url, split_lines=false) }}"
    conversion_cmd: 'ssh-keygen -i -m RFC4716 -f /dev/stdin <<<"{{ key_content }}"'
    new_key_content: "{{ lookup('pipe', conversion_cmd) }}"

```

iim sorry iam not a linux /dev 100% guy juyt sysadmin with mostly adminsitrative experience.  
Ill call it a day.  
Thank you!

---

<div class="post-metadata">

**Author:** ![kurokobo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kurokobo/32/182_2.png) [@kurokobo](https://forum.ansible.com/u/kurokobo)\
**Post date:** [February 13, 2024, 3:22pm UTC](https://forum.ansible.com/t/default-code-to-add-public-ssh-key-fails/3702/25 "2024-02-13T15:22:09Z")

</div>

Example:

```yaml
---
- name: Set authorized keys
  hosts: all
  gather_facts: false
  vars:
    key_url: "https://raw.githubusercontent.com/gsg-git/awx_pub/main/awxtestpub"
    key_content: "{{ lookup('url', key_url, split_lines=false) }}"
    conversion_cmd: 'ssh-keygen -i -m RFC4716 -f /dev/stdin <<<"{{ key_content }}"'
    new_key_content: "{{ lookup('pipe', conversion_cmd) }}"
  
  tasks:
    - name: Set authorized keys taken from url
      ansible.posix.authorized_key:
        user: charlie
        state: present
        key: "{{ new_key_content }}"

```

---

<div class="post-metadata">

**Author:** ![gothsome](https://avatars.discourse-cdn.com/v4/letter/g/278dde/32.png) [@gothsome](https://forum.ansible.com/u/gothsome)\
**Post date:** [February 14, 2024, 1:28pm UTC](https://forum.ansible.com/t/default-code-to-add-public-ssh-key-fails/3702/26 "2024-02-14T13:28:36Z")

</div>

> [@kurokobo](#):
>
> ```auto
> ---
> - name: Set authorized keys
> hosts: all
> gather_facts: false
> vars:
> key_url: "https://raw.githubusercontent.com/gsg-git/awx_pub/main/awxtestpub"
> key_content: "{{ lookup('url', key_url, split_lines=false) }}"
> conversion_cmd: 'ssh-keygen -i -m RFC4716 -f /dev/stdin <<<"{{ key_content }}"'
> new_key_content: "{{ lookup('pipe', conversion_cmd) }}"
>   
> tasks:
> - name: Set authorized keys taken from url
> ansible.posix.authorized_key:
> user: charlie
> state: present
> key: "{{ new_key_content }}"
> 
> ```

Thank you!  
So it seems that you can optionalls set the variables in the playbook before you execute the main task.  
te fritst try failed the user “rott” didnt exist

---

<div class="post-metadata">

**Author:** ![gothsome](https://avatars.discourse-cdn.com/v4/letter/g/278dde/32.png) [@gothsome](https://forum.ansible.com/u/gothsome)\
**Post date:** [February 14, 2024, 1:37pm UTC](https://forum.ansible.com/t/default-code-to-add-public-ssh-key-fails/3702/27 "2024-02-14T13:37:25Z")

</div>

at least something happen - i cant login via ssh as usual with my public key.  
The authorized\_keys in root is no change, but i cant use the public keys for login even after restarting the ssh service

---

<div class="post-metadata">

**Author:** ![kurokobo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kurokobo/32/182_2.png) [@kurokobo](https://forum.ansible.com/u/kurokobo)\
**Post date:** [February 14, 2024, 1:55pm UTC](https://forum.ansible.com/t/default-code-to-add-public-ssh-key-fails/3702/28 "2024-02-14T13:55:39Z")

</div>

My example is to add key for the user `charlie`. Have you modified the `user` in my example correctly?

If yes, are you sure that your playbook indeed ran to the hosts you are trying to connect to via SSH, by the hostname or IP address that appears in the playbook logs?

---

<div class="post-metadata">

**Author:** ![gothsome](https://avatars.discourse-cdn.com/v4/letter/g/278dde/32.png) [@gothsome](https://forum.ansible.com/u/gothsome)\
**Post date:** [February 14, 2024, 2:05pm UTC](https://forum.ansible.com/t/default-code-to-add-public-ssh-key-fails/3702/29 "2024-02-14T14:05:47Z")

</div>

Yeah the connection has been established:

```auto
<192.168.151.237> ESTABLISH SSH CONNECTION FOR USER: root

```

i forgot to make snapshots of the test vms - i have to see what stopped so i cant login via keys, but with credetials

---

<div class="post-metadata">

**Author:** ![kurokobo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kurokobo/32/182_2.png) [@kurokobo](https://forum.ansible.com/u/kurokobo)\
**Post date:** [February 14, 2024, 2:11pm UTC](https://forum.ansible.com/t/default-code-to-add-public-ssh-key-fails/3702/30 "2024-02-14T14:11:59Z")

</div>

I’m talking about this line:

 ![image](https://us1.discourse-cdn.com/ansible/original/2X/b/b30e44594d7e0069b755f8e58c25f79d977e5c48.png)

---

<div class="post-metadata">

**Author:** ![gothsome](https://avatars.discourse-cdn.com/v4/letter/g/278dde/32.png) [@gothsome](https://forum.ansible.com/u/gothsome)\
**Post date:** [February 14, 2024, 2:27pm UTC](https://forum.ansible.com/t/default-code-to-add-public-ssh-key-fails/3702/31 "2024-02-14T14:27:07Z")

</div>

Uh yes i used root - i think for the future i should use a dedicated user.  
The public ssh login is no problem - my mount left me with my keys and i thought the remote desktop manager would store them.

 ![grafik](https://us1.discourse-cdn.com/ansible/original/2X/0/043d4157d52d9033127f58908530c4e78d658fd9.png)  
How can i recognize that the key has been entered- will there be another line with the key?

---

<div class="post-metadata">

**Author:** ![gothsome](https://avatars.discourse-cdn.com/v4/letter/g/278dde/32.png) [@gothsome](https://forum.ansible.com/u/gothsome)\
**Post date:** [February 14, 2024, 2:37pm UTC](https://forum.ansible.com/t/default-code-to-add-public-ssh-key-fails/3702/32 "2024-02-14T14:37:33Z")

</div>

I uploaded the job log.  
i dont know why theres no changes because even the right path /root/.ssh/authorized\_keys is mentioned. [awx\_pub/job\_219.txt at main · gsg-git/awx\_pub · GitHub](https://github.com/gsg-git/awx_pub/blob/main/job_219.txt)

---

<div class="post-metadata">

**Author:** ![kurokobo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kurokobo/32/182_2.png) [@kurokobo](https://forum.ansible.com/u/kurokobo)\
**Post date:** [February 14, 2024, 2:52pm UTC](https://forum.ansible.com/t/default-code-to-add-public-ssh-key-fails/3702/33 "2024-02-14T14:52:45Z")

</div>

I’m not sure I understand your question. Could you please clarify your goal?

The `/root/.ssh/authorized_keys` for the hosts `192.168.151.237` and `192.168.19.201` should have been updated to include the line `ssh-rsa AAAAB3NzaC1yc...` since your logs show the task as `changed`.

---

<div class="post-metadata">

**Author:** ![gothsome](https://avatars.discourse-cdn.com/v4/letter/g/278dde/32.png) [@gothsome](https://forum.ansible.com/u/gothsome)\
**Post date:** [February 15, 2024, 7:51am UTC](https://forum.ansible.com/t/default-code-to-add-public-ssh-key-fails/3702/34 "2024-02-15T07:51:14Z")

</div>

My problem is that there is nothing added -thats why im asking if the key yust gets added or with a #comment added.  
Here are the files:

 ![grafik](https://us1.discourse-cdn.com/ansible/original/2X/4/4ffd56956297185e85e5366d8bf5afef2e0eded5.png)  
 ![grafik](https://us1.discourse-cdn.com/ansible/original/2X/2/22334e031ef3fa11942b10cb3b264c1b3e69a3cb.png)  
The user permissions are root so i think its not a permission issue.  
Both folder have the same permission:  
 ![grafik](https://us1.discourse-cdn.com/ansible/original/2X/e/e1d0123db032051ebc8221a34ca754f3eebc9d51.png)  
I look up if there is any log on the machines where the communication could be saved

---

<div class="post-metadata">

**Author:** ![gothsome](https://avatars.discourse-cdn.com/v4/letter/g/278dde/32.png) [@gothsome](https://forum.ansible.com/u/gothsome)\
**Post date:** [February 15, 2024, 7:57am UTC](https://forum.ansible.com/t/default-code-to-add-public-ssh-key-fails/3702/35 "2024-02-15T07:57:05Z")

</div>

Does AWX use a diffrent port for transfering the data then ssh?  
If yes then i have to edit the firewall here.

---

<div class="post-metadata">

**Author:** ![gothsome](https://avatars.discourse-cdn.com/v4/letter/g/278dde/32.png) [@gothsome](https://forum.ansible.com/u/gothsome)\
**Post date:** [February 15, 2024, 9:09am UTC](https://forum.ansible.com/t/default-code-to-add-public-ssh-key-fails/3702/36 "2024-02-15T09:09:41Z")

</div>

I got it!  
I have to use the custom path - even if its a default path:

```auto
---
- name: Set authorized keys
  hosts: all
  gather_facts: false
  vars:
    key_url: "https://raw.githubusercontent.com/gsg-git/awx_pub/main/awxtestpub"
    key_content: "{{ lookup('url', key_url, split_lines=false) }}"
    conversion_cmd: 'ssh-keygen -i -m RFC4716 -f /dev/stdin <<<"{{ key_content }}"'
    new_key_content: "{{ lookup('pipe', conversion_cmd) }}"
  
  tasks:
    - name: Set authorized keys taken from url
      ansible.posix.authorized_key:
        user: root
        state: present
        key: "{{ new_key_content }}"
        path: /root/.ssh/authorized_keys

```

The key gets inserted now and i like to use something like this in the playboook:

```auto
- name: add comment before entering public key
  lineinfile:
  path: /home/gsa/.ssh/authorized_keys
  line: "#testkey"

```

I cant add it in the tasks part because it fails.  
Do i have to add thepath and commentvalue in the vars like:

```auto
---
- name: Set authorized keys
  hosts: all
  gather_facts: false
  vars:
    key_url: "https://raw.githubusercontent.com/gsg-git/awx_pub/main/awxtestpub"
    key_content: "{{ lookup('url', key_url, split_lines=false) }}"
    conversion_cmd: 'ssh-keygen -i -m RFC4716 -f /dev/stdin <<<"{{ key_content }}"'
    new_key_content: "{{ lookup('pipe', conversion_cmd) }}"
    commentpath: "/root/.ssh/authorized_keys"
    commentcontent: "'#Testkey"
 tasks:
    - name: Set authorized keys taken from url
       lineinfile:
       path: "{{commentpath}}"
       line: "{{commentline}}"
      ansible.posix.authorized_key:
        user: root
        state: present
        key: "{{ new_key_content }}"
        path: /root/.ssh/authorized_keys

```

---

<div class="post-metadata">

**Author:** ![kurokobo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kurokobo/32/182_2.png) [@kurokobo](https://forum.ansible.com/u/kurokobo)\
**Post date:** [February 15, 2024, 1:59pm UTC](https://forum.ansible.com/t/default-code-to-add-public-ssh-key-fails/3702/37 "2024-02-15T13:59:05Z")

</div>

> [@gothsome](#):
>
> I have to use the custom path - even if its a default path:

If this is true, it’s a bug that should be reported on GitHub, but I can’t reproduce it at all, at least in my environment. It works as expected with or without the path on my side.

> [@gothsome](#):
>
> Do i have to add thepath and commentvalue in the vars like:

I recommend you to know basic playbook syntax and variable usage, then try it.

- [Ansible playbooks — Ansible Community Documentation](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_intro.html)
- [Using Variables — Ansible Community Documentation](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_variables.html)

Also, if a task fails, read the error message carefully to find out why it failed and how to fix it.

---

<div class="post-metadata">

**Author:** ![gothsome](https://avatars.discourse-cdn.com/v4/letter/g/278dde/32.png) [@gothsome](https://forum.ansible.com/u/gothsome)\
**Post date:** [February 15, 2024, 2:26pm UTC](https://forum.ansible.com/t/default-code-to-add-public-ssh-key-fails/3702/38 "2024-02-15T14:26:28Z")

</div>

Ill make an issue then if you suggest me to.

And the variable part is right? Then i have to look up the right “punctuation marks”  
Im exited if i can expand your playbook myself successfully 😆

---

<div class="post-metadata">

**Author:** ![shertel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/shertel/32/1241_2.png) [@shertel](https://forum.ansible.com/u/shertel)\
**Post date:** [February 15, 2024, 3:05pm UTC](https://forum.ansible.com/t/default-code-to-add-public-ssh-key-fails/3702/39 "2024-02-15T15:05:10Z")

</div>

If you open an issue at [Issues · ansible-collections/ansible.posix · GitHub](https://github.com/ansible-collections/ansible.posix/issues/) please be sure to include a simple generic reproducer, the full output with -vvv, and the commands you’re running that prove the module didn’t do what it claimed. I wasn’t able to reproduce the issue.

The syntax of your latest code block isn’t right, but reading the docs and trying it (and reading the error messages) is a good way to learn.

---

<div class="post-metadata">

**Author:** ![gothsome](https://avatars.discourse-cdn.com/v4/letter/g/278dde/32.png) [@gothsome](https://forum.ansible.com/u/gothsome)\
**Post date:** [February 16, 2024, 9:05am UTC](https://forum.ansible.com/t/default-code-to-add-public-ssh-key-fails/3702/40 "2024-02-16T09:05:07Z")

</div>

> [@gothsome](#):
>
> ```auto
> ---
> - name: Set authorized keys
> hosts: all
> gather_facts: false
> vars:
> key_url: "https://raw.githubusercontent.com/gsg-git/awx_pub/main/awxtestpub"
> key_content: "{{ lookup('url', key_url, split_lines=false) }}"
> conversion_cmd: 'ssh-keygen -i -m RFC4716 -f /dev/stdin <<<"{{ key_content }}"'
> new_key_content: "{{ lookup('pipe', conversion_cmd) }}"
> commentpath: "/root/.ssh/authorized_keys"
> commentcontent: "'#Testkey"
> tasks:
> - name: Set authorized keys taken from url
> lineinfile:
> path: "{{commentpath}}"
> line: "{{commentline}}"
> ansible.posix.authorized_key:
> user: root
> state: present
> key: "{{ new_key_content }}"
> path: /root/.ssh/authorized_keys
> 
> ```

I used two Templates one with the correct behaviour and path added and one with the path not set and nothing happens and uploaded anr linked them in the issue.

[Previous page](https://forum.ansible.com/t/default-code-to-add-public-ssh-key-fails/3702.md?page=1)

[Next page](https://forum.ansible.com/t/default-code-to-add-public-ssh-key-fails/3702.md?page=3)
