# 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:** 2\
**Page:** 3

<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:11am UTC](https://forum.ansible.com/t/default-code-to-add-public-ssh-key-fails/3702/41 "2024-02-16T09:11:06Z")

</div>

I got it:

```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: addcomment
      lineinfile:
        path: "{{ commentpath }}"
        line: "{{ commentcontent }}"
    - 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 function seems to know if the key i aleready added(?).  
I thought it would just add the line, but at on test file it just added the comment after the aleready added key from yesterday.  
Leaves the next task:  
Reverse the funktion to remove the requested keys.  
Thank you!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [March 17, 2024, 9:11am UTC](https://forum.ansible.com/t/default-code-to-add-public-ssh-key-fails/3702/42 "2024-03-17T09:11:25Z")

</div>

Closed on March 17, 2024, 9:11am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.

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