-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_nspawn_container.yml
98 lines (86 loc) · 2.58 KB
/
create_nspawn_container.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# ansible-playbook -e "machine_name=foo suite=bar ip_address=baz ip_gateway=zab" create_nspawn_container.yml
---
- hosts: nspawn_host
become: yes
vars:
suite: stable
machine_name: test
mirror: http://ftp.gr.debian.org/debian
ip_address: 192.168.1.100/24
ip_gateway: 192.168.1.1
tasks:
- name: debootstrap new machine
command: debootstrap {{ suite }} {{ machine_name }} {{ mirror }}
args:
chdir: /var/lib/machines
creates: /var/lib/machines/{{ machine_name }}
- name: disable ifupdown inside the new machine
file:
name: "{{ item }}"
state: absent
with_items:
- /var/lib/machines/{{ machine_name }}/etc/systemd/system/multi-user.target.wants/networking.service
- /var/lib/machines/{{ machine_name }}/etc/systemd/system/network-online.target.wants/networking.service
tags:
- networking
- name: enable networkd and resolved
file:
name: /var/lib/machines/{{ machine_name }}/etc/systemd/system/multi-user.target.wants/{{ item }}
state: link
src: /lib/systemd/system/{{ item }}
force: yes
with_items:
- systemd-networkd.service
- systemd-resolved.service
tags:
- networking
- name: write network file
copy:
content: |
[Match]
Name=host0
[Network]
Address={{ ip_address }}
Gateway={{ ip_gateway }}
dest: /var/lib/machines/{{ machine_name }}/etc/systemd/network/01-host0.network
tags:
- networking
- name: temporarily change resolv.conf in new machine
copy:
content: "nameserver 8.8.8.8"
dest: /var/lib/machines/{{ machine_name }}/etc/resolv.conf
tags:
- complete_installation
- name: install misc packages
command: systemd-nspawn -UD {{ machine_name }} apt-get -y install {{ item }}
args:
chdir: /var/lib/machines
with_items:
- dbus
tags:
- complete_installation
- name: ln resolv.conf
file:
name: /var/lib/machines/{{ machine_name }}/etc/resolv.conf
state: link
src: /run/systemd/resolve/resolv.conf
force: yes
tags:
- networking
- name: create host .nspawn file
copy:
content: |
[Exec]
Boot=on
[Network]
VirtualEthernet=on
dest: /etc/systemd/nspawn/{{ machine_name }}.nspawn
- name: Start new container (enable)
systemd:
daemon_reload: yes
name: systemd-nspawn@{{ machine_name }}
enabled: yes
state: started
delay: 4
- name: change hostname
command: hostnamectl --machine="{{ machine_name }}" set-hostname "{{ machine_name }}"