Skip to content

Commit

Permalink
Configure Docker config.json for multiple users (#17)
Browse files Browse the repository at this point in the history
* Add custom config dirs

* switch to users

* fix readme
  • Loading branch information
bertiewils authored May 2, 2023
1 parent 0995040 commit 3be2e76
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ Role default variables from `defaults/main.yml`.
```yaml
aws_ecr_cred_helper_bin_version: 0.5.0
aws_ecr_cred_helper_pkg: "amazon-ecr-credential-helper"
aws_ecr_cred_helper_users:
- "{{ ansible_env.USER }}"
install_from_binary: false
# cred_helper_config: {}
Expand Down
2 changes: 2 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
aws_ecr_cred_helper_bin_version: 0.5.0
aws_ecr_cred_helper_pkg: "amazon-ecr-credential-helper"
aws_ecr_cred_helper_users:
- "{{ ansible_env.USER }}"
install_from_binary: false

# cred_helper_config: {}
Expand Down
22 changes: 17 additions & 5 deletions tasks/configure-docker-json.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
---
- name: "Configuring config.json for:"
debug:
var: item

- name: Get user data
user:
name: "{{ item }}"
register: user_data

- name: Creates docker config directory
file:
path: "{{ ansible_user_dir }}/.docker/"
path: "{{ user_data.home }}/.docker/"
state: directory
owner: "{{ ansible_user_id }}"
owner: "{{ user_data.name }}"
group: "{{ user_data.name }}"
mode: '0655'

- name: Check if docker config exists
stat:
path: '{{ ansible_user_dir }}/.docker/config.json'
path: '{{ user_data.home }}/.docker/config.json'
register: stat_result

- name: Read docker config file
slurp:
src: '{{ ansible_user_dir }}/.docker/config.json'
src: '{{ user_data.home }}/.docker/config.json'
register: config_file
when: stat_result.stat.exists

Expand All @@ -28,5 +38,7 @@
- name: Write merged content to config file
copy:
content: "{{ result_var | to_nice_json }}"
dest: '{{ ansible_user_dir }}/.docker/config.json'
dest: '{{ user_data.home }}/.docker/config.json'
owner: "{{ user_data.name }}"
group: "{{ user_data.name }}"
mode: '0600'
1 change: 1 addition & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
- name: Include docker config tasks
include_tasks: configure-docker-json.yml
when: cred_helper_config is defined and cred_helper_config.keys() | length > 0
with_items: "{{ aws_ecr_cred_helper_users }}"

0 comments on commit 3be2e76

Please sign in to comment.