---
- name: "π Check if Snipe-IT admin user already exists (by username)"
ansible.builtin.shell: |
container exec -i \
-e APP_KEY="{{ SNIPE_IT_APP_KEY }}" \
{{ SNIPE_IT_CONTAINER_ADDRESS }} \
php -r '
require "vendor/autoload.php";
$app = require "bootstrap/app.php";
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
$exists = \App\Models\User::where("username", "{{ lookup('users', 'administrator').username }}")
->whereNull("deleted_at")
->exists();
exit($exists ? 0 : 1);
'
register: snipe_it_admin_exists
changed_when: false
failed_when: false
- name: "π Create Snipe-IT administrator account"
ansible.builtin.shell: >
container exec -i
-e APP_KEY="{{ SNIPE_IT_APP_KEY }}"
{{ SNIPE_IT_CONTAINER_ADDRESS }}
php artisan snipeit:create-admin
--first_name "{{ lookup('users', 'administrator').firstname }}"
--last_name "{{ lookup('users', 'administrator').lastname }}"
--username "{{ lookup('users', 'administrator').username }}"
--email "{{ lookup('users', 'administrator').email }}"
--password {{ lookup('users', 'administrator').password | quote }}
--no-interaction
when: snipe_it_admin_exists.rc != 0
register: snipe_it_admin_create
changed_when: "'New user created' in snipe_it_admin_create.stdout"
failed_when: >
snipe_it_admin_create.rc != 0
and not (
'Duplicate entry' in (snipe_it_admin_create.stderr | default(''))
and 'users_groups' in (snipe_it_admin_create.stderr | default(''))
)
no_log: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}"