Skip to content

Commit

Permalink
Add bool field create_superuser
Browse files Browse the repository at this point in the history
Allow to not create superuser on install
  • Loading branch information
TheRealHaoLiu committed Jul 11, 2024
1 parent 041270f commit 154de65
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 34 deletions.
4 changes: 4 additions & 0 deletions config/crd/bases/awx.ansible.com_awxs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ spec:
hostname: # deprecated
description: (Deprecated) The hostname of the instance
type: string
create_superuser:
description: If a superuser should be created
type: boolean
default: true
admin_email:
description: The admin user email
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ spec:
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:advanced
- urn:alm:descriptor:com.tectonic.ui:text
- displayName: Create Admin User?
path: create_superuser
x-descriptors:
- urn:alm:descriptor:com.tectonic.ui:booleanSwitch
- displayName: Admin Account Username
path: admin_user
x-descriptors:
Expand Down
2 changes: 2 additions & 0 deletions roles/installer/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -496,3 +496,5 @@ nginx_worker_cpu_affinity: 'auto'
nginx_listen_queue_size: "{{ uwsgi_listen_queue_size }}"

extra_settings_files: {}

create_superuser: true
72 changes: 38 additions & 34 deletions roles/installer/tasks/initialize_django.yml
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
---
- name: Check if there are any super users defined.
k8s_exec:
namespace: "{{ ansible_operator_meta.namespace }}"
pod: "{{ awx_web_pod_name }}"
container: "{{ ansible_operator_meta.name }}-web"
command: >-
bash -c "echo 'from django.contrib.auth.models import User;
nsu = User.objects.filter(is_superuser=True, username=\"{{ admin_user }}\").count();
exit(0 if nsu > 0 else 1)'
| awx-manage shell"
ignore_errors: true
register: users_result
changed_when: users_result.return_code > 0
- name: Create/update super user
block:
- name: Check if there are any super users defined.
k8s_exec:
namespace: "{{ ansible_operator_meta.namespace }}"
pod: "{{ awx_web_pod_name }}"
container: "{{ ansible_operator_meta.name }}-web"
command: >-
bash -c "echo 'from django.contrib.auth.models import User;
nsu = User.objects.filter(is_superuser=True, username=\"{{ admin_user }}\").count();
exit(0 if nsu > 0 else 1)'
| awx-manage shell"
ignore_errors: true
register: users_result
changed_when: users_result.return_code > 0
- name: Create super user via Django if it doesn't exist.
k8s_exec:
namespace: "{{ ansible_operator_meta.namespace }}"
pod: "{{ awx_web_pod_name }}"
container: "{{ ansible_operator_meta.name }}-web"
command: awx-manage createsuperuser --username={{ admin_user | quote }} --email={{ admin_email | quote }} --noinput
register: result
changed_when: "'That username is already taken' not in result.stderr"
failed_when: "'That username is already taken' not in result.stderr and 'Superuser created successfully' not in result.stdout"
no_log: "{{ no_log }}"
when: users_result.return_code > 0

- name: Create super user via Django if it doesn't exist.
k8s_exec:
namespace: "{{ ansible_operator_meta.namespace }}"
pod: "{{ awx_web_pod_name }}"
container: "{{ ansible_operator_meta.name }}-web"
command: awx-manage createsuperuser --username={{ admin_user | quote }} --email={{ admin_email | quote }} --noinput
register: result
changed_when: "'That username is already taken' not in result.stderr"
failed_when: "'That username is already taken' not in result.stderr and 'Superuser created successfully' not in result.stdout"
no_log: "{{ no_log }}"
when: users_result.return_code > 0
- name: Update Django super user password
k8s_exec:
namespace: "{{ ansible_operator_meta.namespace }}"
pod: "{{ awx_web_pod_name }}"
container: "{{ ansible_operator_meta.name }}-web"
command: awx-manage update_password --username='{{ admin_user }}' --password='{{ admin_password }}'
register: result
changed_when: "'Password updated' in result.stdout"
no_log: "{{ no_log }}"
when: users_result.return_code > 0

when: create_superuser | bool

- name: Update Django super user password
k8s_exec:
namespace: "{{ ansible_operator_meta.namespace }}"
pod: "{{ awx_web_pod_name }}"
container: "{{ ansible_operator_meta.name }}-web"
command: awx-manage update_password --username='{{ admin_user }}' --password='{{ admin_password }}'
register: result
changed_when: "'Password updated' in result.stdout"
no_log: "{{ no_log }}"
when: users_result.return_code > 0

- name: Check if legacy queue is present
k8s_exec:
Expand Down
1 change: 1 addition & 0 deletions roles/installer/tasks/install.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@

- name: Include admin password configuration tasks
include_tasks: admin_password_configuration.yml
when: create_superuser | bool

- name: Include broadcast websocket configuration tasks
include_tasks: broadcast_websocket_configuration.yml
Expand Down

0 comments on commit 154de65

Please sign in to comment.