In a word, yes. It is not as secure as it could be. If a bad actor gains access to one of your servers and it's possible to sudo without a password, then they can also sudo su
and become the root user.
Always have a user password that is required to run escalated privileges
You'd think that this would make automation difficult, having to enter a password each time, but this is where the ansible_become_pass
host variable comes in useful.
Create a yaml file somewhere and create a dictionary of hosts to sudo passwords:
sudo_passwords:
host1.wherever.net: superSeCRETpa$$word
host2.wherever.net: SUPerDuperSecretpa$$word
There are options where you can keep this file, but here is my strategy. I keep the raw secrets file as a plaintext file in an encrypted volume, then add a symlink to a file in the root of your ansible project. You could also keep this file in the root of the ansible project itself, and use ansible-vault to encrypt/decrypt in place.
# Decrypt when you're using it
ansible-vault decrypt sudo_passwords.yaml
# Encrypt when you're done
ansible-vault encrypt sudo_passwords.yaml
For this part, you will need the encryption password. Think of it like a master password. However, I just symlink, since my encrypted volume is only open when I need to do work.
~/my-ansible-project $ ln -s /path/to/vault/sudo_passwords.yaml
Be sure to keep this file out of version control
Then create a task partial to import the sudo passwords as the ansible_become_pass
fact. Save to a file separate from any playbooks, as this can be imported to all playbooks.
---
- name: Import Sudo Password as Fact
ansible.builtin.set_fact:
ansible_become_pass: "{{ sudo_passwords[inventory_hostname] }}"
...
Then import this task into any playbooks that require escalated privileges:
- name: Playbook Name
hosts: all
gather_facts: true
vars_files:
# Point to the passwords file relative to where the playbook file resides
# In this case, it's the same directory
- sudo_passwords.yaml
tasks:
- name: Set sudo password
import_tasks: set-sudo.tasks.yaml
# Add a directory inside the root user's home for proof of concept
- name: Create secrets directory
become: true
ansible.builtin.file:
path: /root/.secrets
state: directory
mode: 0700
owner: root
Note that for simplicity, the playbook, imported task and passwords file all reside in the same directory.
This, I believe is a good balance of ease of automation and security.