You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
2.6 KiB
79 lines
2.6 KiB
--- |
|
- debug: |
|
msg: | |
|
container_ip: "{{ container_ip }}" |
|
|
|
- name: test if container is installed |
|
command: "{{ lxc_cmd }} info {{ container_name }}" |
|
ignore_errors: yes |
|
register: command_return |
|
|
|
- set_fact: |
|
container_present: "{{ command_return.rc == 0 }}" |
|
|
|
- when: not container_present |
|
block: |
|
- name: "install container {{ container_name }}" |
|
community.general.lxd_container: |
|
name: "{{ container_name }}" |
|
state: stopped |
|
profiles: default |
|
wait_for_ipv4_addresses: true |
|
source: |
|
type: image |
|
mode: pull |
|
server: "{{ img_server }}" |
|
protocol: simplestreams |
|
alias: "{{ alp_img_alias }}" |
|
|
|
- name: manually attach eth0 to lxd bridge interface |
|
block: |
|
- command: "{{ lxc_cmd }} network attach {{ lxd_bridge }} \ |
|
{{ container_name }} eth0 eth0" |
|
- command: "{{ lxc_cmd }} config device set {{ container_name }} eth0 \ |
|
ipv4.address {{ container_ip }}" |
|
- community.general.lxd_container: |
|
name: "{{ container_name }}" |
|
state: started |
|
|
|
- name: install common packages in containers |
|
command: "{{ lxc_cmd }} exec {{ container_name }} -- apk upgrade -U" |
|
- command: "{{ lxc_cmd }} exec {{ container_name }} -- \ |
|
apk add python3 openssh rsync shadow tar" |
|
|
|
- name: "copy container sshd_config for {{ container_name }} to lxd host" |
|
template: |
|
src: templates/sshd_config.j2 |
|
dest: "{{ tmp_sshd_config }}" |
|
|
|
- name: use lxc file push to get sshd_config into container |
|
command: "{{ lxc_cmd }} file push {{ tmp_sshd_config}} \ |
|
{{ container_name }}/etc/ssh/sshd_config" |
|
|
|
- name: mkdir /root/.ssh in {{ container_name }} |
|
command: "{{ lxc_cmd }} exec {{ container_name }} -- mkdir -p /root/.ssh" |
|
|
|
- name: add controller public key to {{ container_name }} authorized keys |
|
command: "{{ lxc_cmd }} file push {{ tmp_pubkey }} \ |
|
{{ container_name }}/root/.ssh/authorized_keys \ |
|
--gid 0 --uid 0 --mode 600" |
|
|
|
- name: make hash of a random password |
|
set_fact: |
|
root_pass: "{{ lookup('password', |
|
'/dev/null length=15 chars=ascii_letters') |
|
| password_hash('sha512') }}" |
|
|
|
- name: "set password to unlock root account (NOTE: still using pubkey only)" |
|
shell: '{{ lxc_cmd }} exec {{ container_name }} -- \ |
|
usermod -p {{ root_pass }} root' |
|
|
|
- name: ensure sshd service is enabled |
|
command: "{{ lxc_cmd }} exec {{ container_name }} -- rc-update add sshd" |
|
|
|
- name: ensure sshd service is started |
|
command: "{{ lxc_cmd }} exec {{ container_name }} -- rc-service sshd start" |
|
|
|
- set_fact: |
|
container_present: false |
|
|
|
|