Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve support for adding public keys for deploys #1344

Merged
merged 1 commit into from
Dec 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
### HEAD
* Improve support for adding public SSH keys ([#1344](https://github.com/roots/trellis/pull/1344))
* Fix #1319 - Improve how ssh_args are loaded ([#1337](https://github.com/roots/trellis/pull/1337))
* Fix #1331 - Improve passlib instructions([#1336](https://github.com/roots/trellis/pull/1336))

Expand Down
2 changes: 1 addition & 1 deletion lib/trellis/utils/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def replace_item_with_key(obj, result):
)

if should_replace:
if 'key' in result._result[item]:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR exposed a bug in this code since result._result[item] was a string containing "key" (in public_keys) and Python's in operator is flexible for dict keys or substrings.

if type(result._result[item]) is dict and 'key' in result._result[item]:
result._result[item] = result._result[item]['key']
elif type(result._result[item]) is dict:
subitem = '_ansible_item_label' if '_ansible_item_label' in result._result[item] else 'item'
Expand Down
Empty file added public_keys/.gitkeep
Empty file.
8 changes: 7 additions & 1 deletion roles/users/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,20 @@
validate: "/usr/sbin/visudo -cf %s"
when: web_sudoers[0] is defined

- name: Add SSH keys
- name: Add user SSH keys
authorized_key:
user: "{{ item.0.name }}"
key: "{{ item.1 }}"
with_subelements:
- "{{ users | default([]) }}"
- keys

- name: Add deploy SSH keys
authorized_key:
user: "{{ web_user }}"
key: "{{ lookup('file', item) }}"
with_fileglob: 'public_keys/*.pub'

- name: Check whether Ansible can connect as admin_user
command: ansible {{ inventory_hostname }} -m ping -u {{ admin_user }} {{ cli_options | default('') }}
delegate_to: localhost
Expand Down