-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplaybook_builder.yaml
120 lines (115 loc) · 4.44 KB
/
playbook_builder.yaml
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
- name: Builder
hosts: localhost
connection: localhost
gather_facts: no
any_errors_fatal: yes
vars:
packages: [ git, udev, parted, qemu-utils ]
sector: 512
PROFILE: "{{ lookup('env', 'PROFILE') }}"
IMAGE: "{{ lookup('env', 'IMAGE') }}"
IMAGE_RESIZE: "{{ lookup('env', 'IMAGE_RESIZE') }}"
NO_BOOT_PARTITION: "{{ lookup('env', 'NO_BOOT_PARTITION') | default(1, true) }}"
NO_ROOT_PARTITION: "{{ lookup('env', 'NO_ROOT_PARTITION') | default(2, true) }}"
mounts:
- { path: '/mnt/proc', src: '/proc', opts: 'bind'}
- { path: '/mnt/sys', src: '/sys', opts: 'bind'}
- { path: '/mnt/dev', src: '/dev', opts: 'bind'}
- { path: '/mnt/tmp', src: '/tmp', opts: 'bind'}
- { path: '/mnt/etc/hosts', src: '/etc/hosts', opts: 'bind'}
- { path: '/mnt/media', src: '/app/profiles/{{ PROFILE }}/', opts: 'bind'}
tasks:
- name: Builder - install packages
apt:
name: "{{ packages }}"
install_recommends: no
state: present
- name: Resize image
shell: qemu-img resize -f raw -q output/{{ IMAGE }} {{ IMAGE_RESIZE }}
when: IMAGE_RESIZE
- block:
- name: Builder - join image file
shell: "losetup -fP output/{{ IMAGE }} && losetup -l -a | grep {{ IMAGE }} | cut -d ' ' -f1"
register: rs_loop
- name: Builder - Check pre_tasks
stat:
path: "profiles/{{ PROFILE }}/pre_tasks.yml"
register: rs_pre_tasks
- name: Builder - Run pre_tasks
include_tasks: "profiles/{{ PROFILE }}/pre_tasks.yml"
vars:
loop_dev: "{{ rs_loop.stdout_lines[-1] }}"
when: rs_pre_tasks.stat.exists
- name: Builder - Mount root partition
mount:
path: "/mnt"
src: "{{ rs_loop.stdout_lines[-1] }}p{{ NO_ROOT_PARTITION }}"
opts: "loop"
state: mounted
fstype: "ext4"
- name: Builder - Mount image
mount:
path: "/mnt/boot"
src: "{{ rs_loop.stdout_lines[-1] }}p{{ NO_BOOT_PARTITION }}"
opts: "loop"
state: mounted
fstype: "vfat"
when: (NO_BOOT_PARTITION | int) > 0
- name: Builder - Mount system folders
mount:
path: "{{ item.path }}"
src: "{{ item.src }}"
opts: "{{ item.opts | default('') }}"
state: mounted
fstype: "{{ item.fstype | default('none') }}"
with_items: "{{ mounts }}"
- name: Builder - Customize the image
shell: |
set -o pipefail;
export ANSIBLE_INJECT_FACT_VARS=false;
echo "" > output/ansible-log.txt
[ -f profiles/{{ PROFILE }}/requirements.yml ] && mkdir -p profiles/{{ PROFILE }}/roles && ansible-galaxy install -p profiles/{{ PROFILE }}/roles -r profiles/{{ PROFILE }}/requirements.yml 2>&1 | tee -a output/ansible-log.txt
ansible-playbook -v -i /mnt, -c chroot profiles/{{ PROFILE }}/playbook.yml 2>&1 | tee -a output/ansible-log.txt
args:
executable: /bin/bash
register: rs_playbook
- name: Builder - Check post_tasks
stat:
path: "profiles/{{ PROFILE }}/post_tasks.yml"
register: rs_post_tasks
- name: Builder - Run post_tasks
include_tasks: "profiles/{{ PROFILE }}/post_tasks.yml"
vars:
loop_dev: "{{ rs_loop.stdout_lines[-1] }}"
mount_point: /mnt
when: rs_post_tasks.stat.exists
always:
- name: Print errors
debug: var=rs_playbook.stdout_lines
when: rs_playbook.rc != 0
ignore_errors: yes
- name: Builder - Umount system folders
mount:
path: "{{ item.path }}"
state: unmounted
ignore_errors: yes
with_items: "{{ mounts }}"
- name: Builder - Umount boot
mount:
path: "/mnt/boot"
state: unmounted
ignore_errors: yes
when: (NO_BOOT_PARTITION | int) > 0
- name: Builder - Umount root
mount:
path: "/mnt"
state: unmounted
ignore_errors: yes
- name: Builder - unjoin image file
shell: "losetup -d {{ rs_loop.stdout_lines[-1] }}"
ignore_errors: yes
when: rs_loop.stdout_lines
- name: Remove roles folder
file:
path: "profiles/{{ PROFILE }}/roles"
state: absent