# Add/remove pub key with

**URL:** <https://forum.ansible.com/t/add-remove-pub-key-with/4474>\
**Category:** Get Help\
**Tags:** posix, playbook, awx\
**Created:** [March 20, 2024, 10:40am UTC](https://forum.ansible.com/t/add-remove-pub-key-with/4474 "2024-03-20T10:40:05Z")\
**Posts on this page:** 12\
**Page:** 1

<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:** [March 20, 2024, 10:40am UTC](https://forum.ansible.com/t/add-remove-pub-key-with/4474/1 "2024-03-20T10:40:05Z")

</div>

Hello!  
Im testing to use files with multiple keys in the.keys file.  
My playbook successfully copies the keys, but in a single line.  
Is it possible to use comments so you could see which key belongs to whom.  
Playbook:

```auto
---
- name: Set authorized keys from gitlab
  hosts: all
  gather_facts: false
  vars:
    key_url: "http://192.168.151.237/awx/playbooks_pub/-/raw/main/ssh/granted_keys/granted.keys"
    key_content: "{{ lookup('url', key_url, split_lines=true) }}"
    
  tasks:
    - name: Set authorized keys taken from url
      ansible.posix.authorized_key:
        user: root
        state: present
        key: "{{ key_content }}"
        path: /root/.ssh/authorized_keys

```

The key are one in each line like you would do in a authorized\_keys file.

Thank you very much again!

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [March 20, 2024, 11:44am UTC](https://forum.ansible.com/t/add-remove-pub-key-with/4474/2 "2024-03-20T11:44:33Z")

</div>

`ansible.posix.authorized_key` supports a [comment](https://docs.ansible.com/ansible/latest/collections/ansible/posix/authorized_key_module.html#parameter-comment) parameter. The comment appears at the right (vs. “left”, not vs. “wrong”) end of the line.

If your want comments on their own lines, then you could follow the `authorized_key` task with an `ansible.builtin.lineinfile` task and use its [`insertbefore`](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/lineinfile_module.html#parameter-insertbefore) parameter, but that could get more tedious than it’s worth.

---

<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:** [March 20, 2024, 12:28pm UTC](https://forum.ansible.com/t/add-remove-pub-key-with/4474/3 "2024-03-20T12:28:24Z")

</div>

Yeah i got one with newline for adding a single key, but changing it would be quite miserable and then remove would be even more exhausting. I think it would be better to just copy an inital authorized hosts file with the comments.

The guide means the diffrent keys should be seperated by a newline separator, but i have no idea whats that.  
Adding \n after each line doesnt do the job.

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [March 20, 2024, 12:48pm UTC](https://forum.ansible.com/t/add-remove-pub-key-with/4474/4 "2024-03-20T12:48:30Z")

</div>

That would mean if you have a list of keys, then you can say

```yaml
    key: "{{ list_of_keys | join('\n') }}"

```

BTW, note the quoting above. That “`\n`” is in a double-quoted string – as are the single quotes on either side of it. So it becomes a literal new-line character in a single-quoted string parameter to `join()`. You can do it other ways, but run tests to make sure you’re doing what you mean to do. I suspect that’s what sank your attempt at adding `\n` after each line.

---

<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:** [March 20, 2024, 2:55pm UTC](https://forum.ansible.com/t/add-remove-pub-key-with/4474/5 "2024-03-20T14:55:52Z")

</div>

I thought i could use from [here](https://docs.ansible.com/ansible/latest/collections/ansible/posix/authorized_key_module.html#parameter-comment) this :

```auto
- name: Set authorized keys taken from url using lookup
  ansible.posix.authorized_key:
    user: charlie
    state: present
    key: "{{ lookup('url', 'https://github.com/charlie.keys', split_lines=False) }}"

```

to add ultiple keyys from my gl:

 ![grafik](https://us1.discourse-cdn.com/ansible/original/2X/8/842d7fa32de58616557e688bdbc0b9f16e406b19.png)  
and can add/ remove multiple ones in one go or do i have do download the file to temp like a regular txt file and add them from local, but how do i get it to correctly inserted in 3 row again but not like now in a single one.  
Copying would be useless because then you loose the feature to see if one of those aleready exist in the authorized\_keyes file.  
Thank you!

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [March 20, 2024, 3:22pm UTC](https://forum.ansible.com/t/add-remove-pub-key-with/4474/6 "2024-03-20T15:22:34Z")

</div>

If you want those three and only those three, then put the three keys in a “\n”-joined string and use the [exclusive](https://docs.ansible.com/ansible/latest/collections/ansible/posix/authorized_key_module.html#parameter-exclusive) parameter. This will remove any other keys from the file and ensure those three remain. It makes no guarantees about the order.

---

<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:** [March 21, 2024, 7:59am UTC](https://forum.ansible.com/t/add-remove-pub-key-with/4474/7 "2024-03-21T07:59:39Z")

</div>

Wenn i want to rverse the Playbook to remove mthe keys in the file i cant use the /n at the keys because i think whey wont be recognised then from the open ssh service.  
I thought it could be easier just to tell to look in each row without manually setting a seperator.

My bot suggests me to use a loop all the time:

```auto
---
- name: Set authorized keys from gitlab
  hosts: all
  become: true
  vars:
    key_url: "http://192.168.151.237/awx/playbooks_pub/-/raw/main/ssh/granted_keys/granted.keys"

  tasks:
    - name: Fetch SSH keys from URL
      ansible.builtin.uri:
        url: "{{ key_url }}"
        return_content: yes
        validate_certs: no 
      register: downloaded_keys
      delegate_to: localhost
      run_once: true

    - name: Set authorized keys taken from URL
      ansible.posix.authorized_key:
        user: root
        state: present
        key: "{{ item }}"
        path: /root/.ssh/authorized_keys
      loop: "{{ downloaded_keys.content.splitlines() }}"
      when: downloaded_keys.content is defined and downloaded_keys.content | length > 0

```

But the example in the guide with the .keys file mage me believe that you could either use a single public key or a list with keys in each row without any other changes to the playbook.

I found a method for ansible:

```auto
 roles/users/vars/main.yaml

---
users:
  - username: foo
    public_key: ssh-rsa IKUJHYFGukjyFUKtyfUKYTVkUYVukyGVkjyhGjkhgKjh
    active: true
  - username: bar
    public_key: ssh-rsa klJHGJKfghjktRYKrKJhGjklYgkJhgKjhGjkHbKjhgJK
    active: false

roles/users/tasks/main.yaml

---
- name: Copy public keys to the hosts
  ansible.posix.authorized_key:
    user: "{{ item.username }}"
    key: "{{ item.public_key }}"
    state: present
  with_items: "{{ users }}"
  when: item.active
  loop_control:
    label: "{{ item.username }}"

```

Can i use this in AWX like this too without including the var main file?

Thank you!

---

<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:** [March 21, 2024, 1:17pm UTC](https://forum.ansible.com/t/add-remove-pub-key-with/4474/8 "2024-03-21T13:17:26Z")

</div>

i cnt use a folder with the name roles in my gitlab - the playbooks wont be shown in the templates - dont know why

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [March 21, 2024, 7:00pm UTC](https://forum.ansible.com/t/add-remove-pub-key-with/4474/9 "2024-03-21T19:00:16Z")

</div>

> [@gothsome](#):
>
> Can i use this in AWX like this too without including the var main file?

Yes you can, but `users` will need to be initialized somehow.

> [@gothsome](#):
>
> i cnt use a folder with the name roles in my gitlab - the playbooks wont be shown in the templates - dont know why

I’m not at all sure what you’re trying to say here. We use on-prim gitlab and practically every project has a folder named `roles`. I’m not saying you’re wrong, but I’ll need more evidence to be persuaded.

It’s the next bit that makes me wonder which one of us is more confused. Playbooks don’t go in templates because … they aren’t templates. (?) Maybe the output of `tree -d` would help clarify how you’re structuring things. We typically structure our projects like this:

```txt
playbook1.yml
playbook2.yml
playbook3.yml
group_vars
├── all
├── admin
└── swing
roles/
├── role1
│ ├── files
│ ├── handlers
│ └── tasks
├── role2
│ ├── files
│ │ ├── apache
│ │ │ └── ssl
│ │ └── shibboleth
│ ├── handlers
│ ├── tasks
│ └── templates
│ └── apache
└── role3
    ├── defaults
    ├── files
    ├── filter_plugins
    ├── tasks
    └── templates

```

---

<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:** [March 22, 2024, 8:37am UTC](https://forum.ansible.com/t/add-remove-pub-key-with/4474/10 "2024-03-22T08:37:04Z")

</div>

When i named a folder in gitlab Roles or vars the files they contained would not be listed if i look through the playbooks in a template - i changed the name in functions and everything was visible again:

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

I have a solution for multiple keys:

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

```auto
---
- name: Copy public keys to the hosts
  hosts: all
  gather_facts: false
  tasks:
    - name: Download allowed.yaml from GitLab
      ansible.builtin.uri:
        url: "http://192.168.151.237/awx/playbooks_pub/-/raw/main/functions/public_key/variables/allowed.yaml"
        dest: "/tmp/allowed.yaml"
        method: GET
        return_content: yes
      delegate_to: localhost
      run_once: true

    - name: Include vars from the allowed.yaml file
      ansible.builtin.include_vars:
        file: "/tmp/allowed.yaml"
        name: "users"

    - name: Copy public keys with comments to the authorized_keys of the user
      ansible.posix.authorized_key:
        user: "{{ item.username }}"
        key: "{{ item.public_key }} {{ item.comment }}"
        state: present
      loop: "{{ users.users }}"
      when: item.active
      loop_control:
        label: "{{ item.username }}"

```

Like this the comment seems to be part of the public key and will be removed with the key if necessary

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [March 23, 2024, 7:25pm UTC](https://forum.ansible.com/t/add-remove-pub-key-with/4474/11 "2024-03-23T19:25:55Z")

</div>

Ah, when you say “look through the playbooks in a template”, you’re speaking about a “playbooks” pull-down in an AWX job template’s web-ui. I thought you meant the `templates` directory in an Ansible project root or a `roles/`_rolename_`/templates` sub-directory of an Ansible project. Totally different kind of template! Of course it wouldn’t make sense to put playbooks in a `templates` directory.

AWX will only search for playbooks in those places that are valid for holding playbooks. Playbooks can’t go in or under a `roles` directory, so that’s why AWX wasn’t listing any YAML files in your `roles` directory as playbooks in your AWX job template’s “playbook” pull-down.

You have an, um, _interesting_ project structure. Do you have any guidelines to follow when deciding whether something should go in a playbook vs. in some task file in a role?

---

<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:** [April 8, 2024, 7:10am UTC](https://forum.ansible.com/t/add-remove-pub-key-with/4474/12 "2024-04-08T07:10:53Z")

</div>

The Folder structure is not final.  
I have to clean up a little more 😆
