Skip to content

Commit a769269

Browse files
committed
Add role sudo_by_ssh_agent
1 parent 0969430 commit a769269

File tree

7 files changed

+85
-3
lines changed

7 files changed

+85
-3
lines changed

CHANGELOG.rst

+2
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ Release Summary
1313

1414
| Release Date: 2023-03-09
1515
| Initial release of the password_prompt role
16+
| Initial release of the sudo_by_ssh_agent role
1617
1718

1819
Major Changes
1920
-------------
2021

2122
- prompt_password - initial commit
23+
- sudo_by_ssh_agent - initial commit
2224

2325
v1.0.2
2426
======

README.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@ This Ansible collection contains roles for general use.
1111
Roles:
1212

1313
- `huyz.general.add_to_config_file` ([README](https://github.com/huyz/ansible-collection-huyz-general/blob/master/roles/add_to_config_file/README.md))
14-
- Function: Safely insert a block into one or more shell config files
15-
- Use case: Mainly intended to be re-used by other roles
14+
- Function: Safely insert a block into one or more shell config files.
15+
- Use case: Mainly intended to be re-used by other roles.
1616
- `huyz.general.prompt_password` ([README](https://github.com/huyz/ansible-collection-huyz-general/blob/master/roles/prompt_password/README.md))
1717
- Function: Prompts for the `ansible_password` if not defined.
18-
- Use case: Avoid the need to call `ansible-playbook` with `--ask-pass` and `--ask-become-pass`
18+
- Use case: Avoid the need to call `ansible-playbook` with `--ask-pass` and `--ask-become-pass`.
19+
- `huyz.general.sudo_by_ssh_agent` ([README](https://github.com/huyz/ansible-collection-huyz-general/blob/master/roles/sudo_by_ssh_agent/README.md))
20+
- Function: Configures sudo to use the `libpam-ssh-agent-auth` package and
21+
authorize the provided ssh key.
22+
- Use case: Avoid the need to authenticate with a password to run privileged commands.
1923

2024
---
2125

changelogs/changelog.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@ releases:
1717
changes:
1818
major_changes:
1919
- prompt_password - initial commit
20+
- sudo_by_ssh_agent - initial commit
2021
release_summary: '| Release Date: 2023-03-09
2122
2223
| Initial release of the password_prompt role
2324
25+
| Initial release of the sudo_by_ssh_agent role
26+
2427
'
2528
fragments:
2629
- 2023-03-09_prompt_password.yml
30+
- 2023-03-09_sudo_by_ssh_agent.yml
2731
- 2023-03-09_v1.0.3_summary.yml
2832
release_date: '2023-03-09'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
major_changes:
2+
- sudo_by_ssh_agent - initial commit
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
release_summary: |
22
| Release Date: 2023-03-09
33
| Initial release of the password_prompt role
4+
| Initial release of the sudo_by_ssh_agent role

roles/sudo_by_ssh_agent/README.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Ansible role: huyz.general.sudo_by_ssh_agent
2+
3+
Configures sudo to use the `libpam-ssh-agent-auth` package and authorize the
4+
provided ssh key.
5+
6+
## Installation
7+
8+
This repo uses the FQCN convention.
9+
10+
Include the collection in the Ansible Galaxy `requirements.yml`:
11+
12+
```shell
13+
---
14+
collections:
15+
- name: huyz.general
16+
```
17+
18+
You can then include the role `huyz.general.sudo_by_ssh_agent`.
19+
20+
## Example
21+
22+
```yaml
23+
- name: Authorize sudo by ssh agent
24+
ansible.builtin.include_role:
25+
name: huyz.general.sudo_by_ssh_agent
26+
vars:
27+
pub_files_for_sudo: ['~/.ssh/id_ed25519-vip.pub']
28+
```
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Requires vars:
2+
# - pub_files_for_sudo: list of full path of the public keys to use for sudo
3+
---
4+
- name: Ensure libpam-ssh-agent-auth package
5+
ansible.builtin.package:
6+
name: libpam-ssh-agent-auth
7+
become: true
8+
9+
- name: Add to sudo authorized_keys the content of {{ pub_files_for_sudo }}
10+
ansible.posix.authorized_key:
11+
user: root # dummy
12+
key: "{{ lookup('file', item) }}"
13+
path: /etc/security/authorized_keys
14+
manage_dir: false
15+
become: true
16+
loop: "{{ pub_files_for_sudo }}"
17+
18+
# Per https://www.lorier.net/docs/ssh-agent-sudo.html
19+
- name: Enable pam_ssh_agent_auth for sudo
20+
ansible.builtin.blockinfile:
21+
dest: /etc/pam.d/sudo
22+
marker: "# {mark} ANSIBLE MANAGED BLOCK: {{ block_id }}"
23+
insertbefore: "@include common-auth"
24+
block: |
25+
# Allow sudo by ssh agent
26+
auth sufficient pam_ssh_agent_auth.so file=/etc/security/authorized_keys
27+
vars:
28+
block_id: sudo_by_ssh_agent
29+
become: true
30+
31+
# Per https://www.lorier.net/docs/ssh-agent-sudo.html
32+
- name: Preserve SSH_AUTH_SOCK for sudo
33+
ansible.builtin.blockinfile:
34+
dest: /etc/sudoers
35+
marker: "# {mark} ANSIBLE MANAGED BLOCK: {{ block_id }}"
36+
insertafter: '#Defaults:%sudo env_keep \+= "SSH_AGENT_PID SSH_AUTH_SOCK"'
37+
block: |
38+
Defaults:%sudo env_keep += "SSH_AGENT_PID SSH_AUTH_SOCK"
39+
vars:
40+
block_id: sudo_by_ssh_agent
41+
become: true

0 commit comments

Comments
 (0)