115 lines
5.2 KiB
YAML
115 lines
5.2 KiB
YAML
|
|
---
|
||
|
|
- name: Inspect Ubuntu container state
|
||
|
|
ansible.windows.win_shell: |
|
||
|
|
$containersJson = (podman ps -a --filter "name=^{{ windows_containers_container_name }}$" --format json 2>$null | Out-String).Trim()
|
||
|
|
if (-not $containersJson -or $containersJson -eq "[]") {
|
||
|
|
Write-Output "__CONTAINER_ABSENT__"
|
||
|
|
exit 0
|
||
|
|
}
|
||
|
|
|
||
|
|
$containers = $containersJson | ConvertFrom-Json
|
||
|
|
$container = $containers | Select-Object -First 1
|
||
|
|
if ($container) {
|
||
|
|
Write-Output "__CONTAINER_PRESENT__"
|
||
|
|
if ($container.State -eq "running") {
|
||
|
|
Write-Output "__CONTAINER_RUNNING__"
|
||
|
|
}
|
||
|
|
}
|
||
|
|
register: container_state
|
||
|
|
changed_when: false
|
||
|
|
tags:
|
||
|
|
- roles::windows_containers::ubuntu_container
|
||
|
|
|
||
|
|
- name: Create Ubuntu container
|
||
|
|
ansible.windows.win_shell: podman run -d --name {{ windows_containers_container_name }} -p {{ windows_containers_container_port }}:22 --restart unless-stopped "{{ windows_containers_image }}"
|
||
|
|
when: "'__CONTAINER_ABSENT__' in (container_state.stdout + container_state.stderr)"
|
||
|
|
tags:
|
||
|
|
- roles::windows_containers::ubuntu_container
|
||
|
|
|
||
|
|
- name: Start Ubuntu container when stopped
|
||
|
|
ansible.windows.win_shell: podman start {{ windows_containers_container_name }}
|
||
|
|
when:
|
||
|
|
- "'__CONTAINER_PRESENT__' in (container_state.stdout + container_state.stderr)"
|
||
|
|
- "'__CONTAINER_RUNNING__' not in (container_state.stdout + container_state.stderr)"
|
||
|
|
tags:
|
||
|
|
- roles::windows_containers::ubuntu_container
|
||
|
|
|
||
|
|
- name: Wait for container to be ready
|
||
|
|
ansible.windows.win_shell: |
|
||
|
|
$status = (podman inspect --format "{{ '{{.State.Running}}' }}" {{ windows_containers_container_name }} 2>$null | Out-String).Trim()
|
||
|
|
if ($LASTEXITCODE -eq 0 -and $status -eq "true") {
|
||
|
|
exit 0
|
||
|
|
}
|
||
|
|
Write-Error "Container {{ windows_containers_container_name }} is not running yet"
|
||
|
|
exit 1
|
||
|
|
register: container_ready
|
||
|
|
until: container_ready.rc == 0
|
||
|
|
retries: 20
|
||
|
|
delay: 5
|
||
|
|
tags:
|
||
|
|
- roles::windows_containers::ubuntu_container
|
||
|
|
|
||
|
|
- name: Set up SSH in Ubuntu container
|
||
|
|
ansible.windows.win_shell: |
|
||
|
|
podman exec {{ windows_containers_container_name }} sh -c 'mkdir -p /home/{{ windows_containers_user }}/.ssh'
|
||
|
|
podman exec {{ windows_containers_container_name }} sh -c 'touch /home/{{ windows_containers_user }}/.ssh/authorized_keys'
|
||
|
|
podman exec {{ windows_containers_container_name }} sh -c 'chown -R {{ windows_containers_user }}:$(id -g {{ windows_containers_user }}) /home/{{ windows_containers_user }}/.ssh'
|
||
|
|
podman exec {{ windows_containers_container_name }} sh -c 'chmod 700 /home/{{ windows_containers_user }}/.ssh && chmod 600 /home/{{ windows_containers_user }}/.ssh/authorized_keys'
|
||
|
|
register: ssh_dir_setup
|
||
|
|
changed_when: false
|
||
|
|
tags:
|
||
|
|
- roles::windows_containers::ubuntu_container
|
||
|
|
|
||
|
|
- name: Deploy SSH authorized_keys
|
||
|
|
ansible.windows.win_shell: |
|
||
|
|
$keysJson = '{{ windows_containers_ssh_users | map(attribute="key") | list | to_json }}'
|
||
|
|
$keys = @()
|
||
|
|
if ($keysJson) {
|
||
|
|
$keys = $keysJson | ConvertFrom-Json
|
||
|
|
}
|
||
|
|
$keys = $keys | Where-Object { $_ -and $_.Trim() -ne "" } | ForEach-Object { $_.Trim() } | Sort-Object -Unique
|
||
|
|
|
||
|
|
$desiredContent = ""
|
||
|
|
if ($keys.Count -gt 0) {
|
||
|
|
$desiredContent = ($keys -join "`n") + "`n"
|
||
|
|
}
|
||
|
|
|
||
|
|
$currentContent = (podman exec {{ windows_containers_container_name }} sh -c "cat /home/{{ windows_containers_user }}/.ssh/authorized_keys 2>/dev/null || true" | Out-String)
|
||
|
|
if ($currentContent -ne $desiredContent) {
|
||
|
|
$encodedContent = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($desiredContent))
|
||
|
|
podman exec {{ windows_containers_container_name }} sh -c "echo $encodedContent | base64 -d > /home/{{ windows_containers_user }}/.ssh/authorized_keys"
|
||
|
|
podman exec {{ windows_containers_container_name }} sh -c 'chown {{ windows_containers_user }}:$(id -g {{ windows_containers_user }}) /home/{{ windows_containers_user }}/.ssh/authorized_keys'
|
||
|
|
podman exec {{ windows_containers_container_name }} sh -c 'chmod 600 /home/{{ windows_containers_user }}/.ssh/authorized_keys'
|
||
|
|
Write-Output "__AUTHORIZED_KEYS_UPDATED__"
|
||
|
|
}
|
||
|
|
register: authorized_keys_result
|
||
|
|
changed_when: "'__AUTHORIZED_KEYS_UPDATED__' in (authorized_keys_result.stdout + authorized_keys_result.stderr)"
|
||
|
|
tags:
|
||
|
|
- roles::windows_containers::ubuntu_container
|
||
|
|
|
||
|
|
- name: Enable and start sshd in Ubuntu container
|
||
|
|
ansible.windows.win_shell: |
|
||
|
|
$changed = $false
|
||
|
|
|
||
|
|
podman exec {{ windows_containers_container_name }} sh -c "dpkg -s openssh-server >/dev/null 2>&1"
|
||
|
|
if ($LASTEXITCODE -ne 0) {
|
||
|
|
podman exec {{ windows_containers_container_name }} sh -c "apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y openssh-server"
|
||
|
|
$changed = $true
|
||
|
|
}
|
||
|
|
|
||
|
|
podman exec {{ windows_containers_container_name }} sh -c "mkdir -p /run/sshd"
|
||
|
|
|
||
|
|
podman exec {{ windows_containers_container_name }} sh -c "pgrep -x sshd >/dev/null 2>&1"
|
||
|
|
if ($LASTEXITCODE -ne 0) {
|
||
|
|
podman exec {{ windows_containers_container_name }} /usr/sbin/sshd
|
||
|
|
$changed = $true
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($changed) {
|
||
|
|
Write-Output "__SSHD_PROVISIONED__"
|
||
|
|
}
|
||
|
|
register: sshd_result
|
||
|
|
changed_when: "'__SSHD_PROVISIONED__' in (sshd_result.stdout + sshd_result.stderr)"
|
||
|
|
tags:
|
||
|
|
- roles::windows_containers::ubuntu_container
|