Ansible Playbooks

SSH Key Insert

Welcome

This playbook does the following on the target hosts:

Add public ssh key

Adds the public SSH key of the user running the playbook to the authorized keys of the corresponding user on the target hosts, allowing passwordless SSH login.

Modify Sudoers

Modifies the sudoers file to allow users in the sudo group to execute commands with sudo without being prompted for a password, enhancing administrative convenience.

* Please be aware, the coding on this site is meant to guide. Testing these scripts in a non-production environment is strongly encouraged. I take no responsibility for use of any scripts on this site or on my github repositories.


Playbook:

---
- name: Add ssh key
  hosts: "{{ my_hosts | d([]) }}"
  become: true

  tasks:
    - name: Install public keys
      ansible.posix.authorized_key:
        user: "{{ lookup('env', 'USER') }}"
        state: present
        key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"

    - name: Change sudoers file
      ansible.builtin.lineinfile:
        path: /etc/sudoers
        state: present
        regexp: '^%sudo'
        line: '%sudo ALL=(ALL) NOPASSWD: ALL'
        validate: /usr/sbin/visudo -cf %s

View on GitHub