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.
91 lines
2.8 KiB
91 lines
2.8 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: true |
|
when: command_return.rc == 0 |
|
|
|
# |
|
# TODO: static ip not working |
|
# the eth0 device is added, and ipv4.address is set, but the device |
|
# is not attached to the bridge as per |
|
# https://discuss.linuxcontainers.org/t/using-static-ips-with-lxd/1291/6 |
|
# could add some more tasks to do it manually :( |
|
# |
|
- name: "install container {{ container_name }}" |
|
community.general.lxd_container: |
|
name: "{{ container_name }}" |
|
state: started |
|
profiles: default |
|
wait_for_ipv4_addresses: true |
|
source: |
|
type: image |
|
mode: pull |
|
server: "{{ img_server }}" |
|
protocol: simplestreams |
|
alias: "{{ alp_img_alias }}" |
|
devices: |
|
eth0: |
|
type: nic |
|
nictype: bridged |
|
parent: "{{ lxd_bridge }}" |
|
ipv4.address: "{{ container_ip }}" |
|
when: not container_present |
|
|
|
- name: update container packages |
|
command: "{{ lxc_cmd }} exec {{ container_name }} -- apk upgrade -U" |
|
when: not container_present |
|
|
|
- name: install common packages in containers |
|
command: "{{ lxc_cmd }} exec {{ container_name }} -- \ |
|
apk add python3 openssh rsync shadow tar" |
|
when: not container_present |
|
|
|
- name: "copy container sshd_config for {{ container_name }} to lxd host" |
|
template: |
|
src: templates/sshd_config.j2 |
|
dest: "{{ tmp_sshd_config }}" |
|
when: not container_present |
|
|
|
- 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" |
|
when: not container_present |
|
|
|
- name: mkdir /root/.ssh in {{ container_name }} |
|
command: "{{ lxc_cmd }} exec {{ container_name }} -- mkdir -p /root/.ssh" |
|
when: not container_present |
|
|
|
- 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" |
|
when: not container_present |
|
|
|
- name: make hash of a random password |
|
set_fact: |
|
root_pass: "{{ lookup('password', '/dev/null length=15 chars=ascii_letters') | password_hash('sha512') }}" |
|
when: not container_present |
|
|
|
- name: "set password to unlock root account (NOTE: still using pubkey only)" |
|
shell: '{{ lxc_cmd }} exec {{ container_name }} -- usermod -p {{ root_pass }} root' |
|
when: not container_present |
|
|
|
- name: ensure sshd service is enabled |
|
command: "{{ lxc_cmd }} exec {{ container_name }} -- rc-update add sshd" |
|
when: not container_present |
|
|
|
- name: ensure sshd service is started |
|
command: "{{ lxc_cmd }} exec {{ container_name }} -- rc-service sshd start" |
|
when: not container_present |
|
|
|
- set_fact: |
|
container_present: false |
|
|
|
|