新增資料夾 roles/bpi
新增資料夾 roles/bpi/tasks (資料夾)
新增檔案 roles/bpi/tasks/main.yml
bpi這個資料夾主要是為了Banana Pi M64的2018-01-05-ubuntu-16.04-mate-desktop-v1.0-bpi-m64-sd-emmc.img.zip 系統所客製化的,main.yml會將此系統的一些服務修改並且關閉、移除桌面環境等,執行完,重開機後,會自動把磁碟空間延伸到最大。
Copy ---
- name: stop NetworkManager
systemd:
name: NetworkManager
state: stopped
- name: disable NetworkManager
systemd:
name: NetworkManager
enabled: no
masked: yes
- name: configure /etc/network/interfaces
blockinfile:
path: '/etc/network/interfaces'
block: |
auto eth0
iface eth0 inet dhcp
- name: disable accounts-daemon.service
systemd:
name: accounts-daemon
enabled: no
masked: no
- name: disable ModemManager.service
systemd:
name: ModemManager
enabled: no
masked: no
- name: disable pppd-dns.service
systemd:
name: pppd-dns
enabled: no
masked: no
- name: update /var/lib/bananapi/bpi-init.d/S10-bpi-hw-wifi.sh
template:
src: 'S10-bpi-hw-wifi.sh.j2'
dest: '/var/lib/bananapi/bpi-init.d/S10-bpi-hw-wifi.sh'
- name: uninstall unneeded packages
apt:
pkg: '{{ item }}'
state: 'absent'
purge: 'yes'
with_flattened:
- '{{ moodlebox_uninstall_packages }}'
when: 'moodlebox_uninstall_packages is defined'
- name: fdisk /dev/mmcblk0 partition
shell: echo -e "p\nd\n2\nn\np\n2\n729088\n\nw" | sudo fdisk /dev/mmcblk0
args:
executable: /bin/bash
ignore_errors: yes
- name: reboot machine
shell: 'sleep 2 && shutdown -r now'
async: 1
poll: 0
ignore_errors: true
- name: wait for machine to come back
become: false
local_action: wait_for
args:
host: '{{ ansible_host | default(inventory_hostname) }}'
port: 22
state: 'started'
delay: 10
timeout: 90
- name: "resize filesystem"
command: resize2fs /dev/mmcblk0p2
sudo: true
新增資料夾 roles/bpi/templates
新增檔案 roles/bpi/templates/S10-bpi-hw-wifi.sh.js
S10-bpi-hw-wifi.sh.js是要取代系統中的/var/lib/bananapi/bpi-init.d/S10-bpi-hw-wifi.sh,主要加入了op_mode=2參數,讓無線網卡可以當作無線AP使用。
Copy #!/bin/bash
BOARD=$(bpi-hw)
load_modules()
{
case ${BOARD} in
bpi-m3)
modprobe bcmdhd op_mode=2
;;
bpi-m64)
modprobe bcmdhd op_mode=2
;;
bpi-m2m)
modprobe bcmdhd op_mode=2
;;
bpi-m2u)
modprobe bcmdhd op_mode=2
;;
bpi-m2z)
modprobe bcmdhd op_mode=2
;;
bpi-m2p)
modprobe bcmdhd op_mode=2
;;
bpi-m2)
modprobe bcmdhd op_mode=2
;;
bpi-m1p)
modprobe ap6211
;;
bpi-r2)
if [ -x /etc/init.d/wifi_enable ] ; then
/etc/init.d/wifi_enable &
fi
;;
*)
;;
esac
}
#main
load_modules
新增資料夾 roles/bpi/vars
新增檔案 roles/bpi/vars/main.yml
此參數檔放要刪除的桌面環境套件
Copy moodlebox_uninstall_packages:
- libmate-*
- libmate*
- mate-*
- x11-*
- lightdm
- libx11-*
- x11proto-*
- plymouth-theme-ubuntu-mate-text
- ubuntu-mate-wallpapers
- ubuntu-mate-wallpapers-common
- ubuntu-mate-wallpapers-xenial
修改檔案 buildmoodlebox.yml
此檔案是是主要建立moodlebox的架構檔,在roles下,加入了「- bpi」這個bpi就是roles下的bpi資料夾,此資料夾下的內容是BPI要安裝moodlebox前先執行的內容,如網路設定、要關閉的服務及移除桌面環境及不需要的套件
buildmoodlebox.yml 修改內容
Copy roles:
- bpi
- bootstrap
- install
- network
- moodle
- cleanup
修改檔案 default.config.yml
此檔案是主要的參數設定檔,以下會列出有修改過的內容,moodlebox_tld,此修改後透過moodlebox的無線ap連線後,網址會由原本的「moodlebox.home」變更為「moodlebox.bpi」,其他分別為wifi、語系、時間、及帳號密碼。
Copy moodlebox_tld: 'home'
moodlebox_wifi_channel: '13'
moodlebox_wifi_country: 'CH'
moodlebox_default_locale: 'en_GB.UTF-8'
moodlebox_locales:
- 'en_GB.UTF-8'
- 'fr_FR.UTF-8'
- 'de_DE.UTF-8'
- 'es_ES.UTF-8'
- 'it_IT.UTF-8'
moodlebox_timezone: 'Europe/Paris'
moodlebox_rpi_password: 'raspberry'
moodlebox_rpi_hostname: 'raspberrypi'
Copy moodlebox_tld: 'bpi'
moodlebox_wifi_channel: '11'
moodlebox_wifi_country: 'TW'
moodlebox_default_locale: 'en_US.UTF-8'
moodlebox_locales:
- 'en_US.UTF-8'
- 'zh_TW.UTF-8'
- 'fr_FR.UTF-8'
- 'de_DE.UTF-8'
moodlebox_timezone: 'Asia/Taipei'
moodlebox_rpi_password: 'bananapi'
moodlebox_rpi_hostname: 'bpi-iot-ros-ai'
修改檔案 roles/bootstrap/tasks/apt.yml
apt.yml原本的內容為執行update cache、upgrade、autoclean 、autoremove動作,因為在執行此檔案前,加入了bpi資料夾,裡面有執行uninstall unneeded packages,主要是移除桌面環境,所以在邊都執行完後加入重新開機,並且重新連線,這邊設定等待90秒,目前測試90秒是足夠的。
Copy - name: update apt cache and upgrade
apt:
update_cache: 'yes'
upgrade: 'dist'
autoclean: 'yes'
autoremove: 'yes'
Copy - name: update apt cache and upgrade
apt:
update_cache: 'yes'
upgrade: 'dist'
autoclean: 'yes'
autoremove: 'yes'
- name: reboot machine
shell: 'sleep 2 && shutdown -r now'
async: 1
poll: 0
ignore_errors: true
- name: wait for machine to come back
become: false
local_action: wait_for
args:
host: '{{ ansible_host | default(inventory_hostname) }}'
port: 22
state: 'started'
delay: 10
timeout: 90
修改檔案 roles/bootstrap/tasks/firmware.yml
firmware.yml檔主要是執行Raspbian 系統中才有的指令,主要安裝rpi-update套件、更新韌體、重新開機及重新連線,這邊只有修改最後一行的timeout:60,修改成90秒。
Copy ---
- name: install rpi-update
apt:
pkg: 'rpi-update'
state: 'present'
- name: update firmware
shell: 'SKIP_BACKUP=1 PRUNE_MODULES=1 rpi-update'
register: fw_result
changed_when: 'fw_result.rc == 0 and "Your firmware is already up to date" not in fw_result.stdout'
- name: reboot machine
shell: 'sleep 2 && shutdown -r now'
async: 1
poll: 0
ignore_errors: true
when: fw_result|changed
- name: wait for machine to come back
become: false
local_action: wait_for
args:
host: '{{ ansible_host | default(inventory_hostname) }}'
port: 22
state: 'started'
delay: 10
timeout: 60
不過這個設定檔基本在bpi版本中不會執行,因為主要控制是否要執行此檔案的設定在「default.config.yml」中的「moodlebox_update_firmware: false」此參數設定為false,並且在roles/bootstrap/tasks/main.yml中有設定去檢查moodlebox_update_firmware參數,再決定是否要執行firmware.yml檔案的指令
roles/bootstrap/tasks/main.yml中控制 firmware.yml 的指令
Copy - name: update RPi firmware
import_tasks: firmware.yml
when: moodlebox_update_firmware
修改檔案 roles/bootstrap/tasks/misc.yml
misc.yml中會刪除前面三個設定,分別是降低顯示記憶體為16Mb,硬體開關機銨鈕,設定畫面為滿版不會留控白邊,其中硬體開關機銨鈕是2.2版新加的在BPI中原本硬體就有開機關及重新開機的銨鈕,所以這也不需要設定,其他下方的主要是在系統加入Moodlebox的一些資訊及ssh進入後的logo畫面。
Copy ---
- name: reduce memory split down to 16Mb, as we are on a headless system
lineinfile:
path: '/boot/config.txt'
line: 'gpu_mem=16'
notify:
- reboot machine
- wait for machine to come back
- name: enable shutdown/startup hardware button
lineinfile:
path: '/boot/config.txt'
line: 'dtoverlay=gpio-shutdown'
insertafter: '^# Additional overlays and parameters'
notify:
- reboot machine
- wait for machine to come back
- name: turn off screen blanking
replace:
path: '/boot/cmdline.txt'
regexp: '\bconsole=tty1\s+root=PARTUUID\b'
replace: 'console=tty1 consoleblank=0 root=PARTUUID'
notify:
- reboot machine
- wait for machine to come back
- name: write version file
template:
src: 'etc/moodlebox-info.j2'
dest: '/etc/moodlebox-info'
- name: update motd
template:
src: 'etc/motd.j2'
dest: '/etc/motd'
- name: copy ascii logo file
copy:
src: 'etc/moodlebox-logo.txt'
dest: '/etc/moodlebox-logo.txt'
mode: 0644
- name: copy script to display ascii logo
copy:
src: 'etc/update-motd.d/20-logo'
dest: '/etc/update-motd.d/20-logo'
mode: 0755
- meta: flush_handlers
Copy ---
# - name: reduce memory split down to 16Mb, as we are on a headless system
# lineinfile:
# path: '/boot/config.txt'
# line: 'gpu_mem=16'
# notify:
# - reboot machine
# - wait for machine to come back
# - name: enable shutdown/startup hardware button
# lineinfile:
# path: '/boot/config.txt'
# line: 'dtoverlay=gpio-shutdown'
# insertafter: '^# Additional overlays and parameters'
# notify:
# - reboot machine
# - wait for machine to come back
# - name: turn off screen blanking
# replace:
# path: '/boot/cmdline.txt'
# regexp: '\bconsole=tty1\s+root=PARTUUID\b'
# replace: 'console=tty1 consoleblank=0 root=PARTUUID'
# notify:
# - reboot machine
# - wait for machine to come back
- name: write version file
template:
src: 'etc/moodlebox-info.j2'
dest: '/etc/moodlebox-info'
- name: update motd
template:
src: 'etc/motd.j2'
dest: '/etc/motd'
- name: copy ascii logo file
src: 'etc/moodlebox-logo.txt'
dest: '/etc/moodlebox-logo.txt'
mode: 0644
- name: copy script to display ascii logo
copy:
src: 'etc/update-motd.d/20-logo'
dest: '/etc/update-motd.d/20-logo'
mode: 0755
- meta: flush_handlers
修改檔案 roles/bootstrap/files/tasks/wifi.yml
在wifi.yml設定主要是修改wpasupplicant.conf中的「country」,設定值來自於default.config.yml檔中的moodleboxwifi_country,預設為「CH」,要修改成「TW」,但在BPI中燒錄完初始的系統是沒有/etc/wpasupplicant/wpasupplicant.conf檔,所以修改處理方式,原本是找到系統中的wpasupplicant.conf檔,然後修改country中的值,在bpi中修改成用範本檔去新增wpasupplicant.conf,範本檔建立於下方的「新增檔案 roles/bootstrap/templates/etc/wpa_supplicant.j2 」。
Copy ---
- name: set wifi country
lineinfile:
path: '/etc/wpa_supplicant/wpa_supplicant.conf'
regexp: '^country='
line: 'country={{ moodlebox_wifi_country }}'
notify: restart networking
Copy ---
- name: write wpa_supplicant file
template:
src: 'etc/wpa_supplicant.j2'
dest: '/etc/wpa_supplicant/wpa_supplicant.conf'
mode: 0600
notify: restart networking
新增檔案 roles/bootstrap/templates/etc/wpa_supplicant.j2
Copy country={{ moodlebox_wifi_country }}
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
修改檔案 roles/bootstrap/files/templates/etc/hosts.j2
hosts.j2範本檔,所修改內容依燒錄系統中/etc/hosts的原始內容
Copy 127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
{{ moodlebox_ip_address | ipaddr('address') }} {{ moodlebox_hostname }}
Copy 127.0.0.1 localhost
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
{{ moodlebox_ip_address | ipaddr('address') }} {{ moodlebox_hostname }}
修改檔案 roles/bootstrap/vars/main.yml
因為moodlebox中會把使用者帳號名稱pi改成moodlebox,所以此檔案中所列出要修改的相關檔案,但在BPI中因為沒有「/etc/systemd/system/autologin@.service」所以就把最後一行拿掉。
Copy ---
moodlebox_username_files:
- /etc/passwd
- /etc/group
- /etc/shadow
- /etc/gshadow
- /etc/systemd/system/autologin@.service
Copy ---
moodlebox_username_files:
- /etc/passwd
- /etc/group
- /etc/shadow
- /etc/gshadow
#- /etc/systemd/system/autologin@.service
修改檔案 roles/cleanup/tasks/main.yml
此檔案是在安裝過程的最後一步,清除系統中不需要的檔案,清除完後會重開機,再重新連ssh,修改重新開機後要等待的時間由原本的60秒設定為90秒
Copy - name: wait for machine to come back
become: false
local_action: wait_for
args:
host: '{{ ansible_host | default(inventory_hostname) }}'
port: 22
state: 'started'
delay: 10
timeout: 90
修改檔案 roles/install/tasks/db.yml
這邊所安裝資料庫都是mariadb-server,但在restart時,RPI可以使用mariadb指令重新啟動,但在BPI這邊要改成mysql指令重新啟動資料庫,所以在最後重新啟動的指令修改成「mysql」
Copy - name: restart MariaDB database
service:
name: 'mariadb'
state: 'restarted'
Copy - name: restart MariaDB database
service:
name: 'mysql'
state: 'restarted'
修改檔案 roles/install/tasks/packages.yml
執行packages.yml後,會依roles/install/tasks/vars/main.yml中的套件名稱做安裝或移除,下方中會另外單獨執行安裝phpmyadmin,在BPI中會有問題,所以目前移除此設定,將phpmyadmin加入到roles/install/tasks/vars/main.yml的moodlebox_install_packages清單中
Copy - name: install phpMyAdmin package
apt:
pkg: 'phpmyadmin'
state: 'present'
Copy # - name: install phpMyAdmin package
# apt:
# pkg: 'phpmyadmin'
# state: 'present'
修改檔案 roles/install/vars/main.yml
此檔案列出要安裝的套件,另外加入了php7.0-zip、php7.0-bz2、phpmyadmin、dhcpcd5四個套件
Copy moodlebox_install_packages:
- iptables-persistent
- hostapd
- dnsmasq
- git
- ntfs-3g
- exfat-fuse
- lockfile-progs
- incron
- nginx
- php7.0-fpm
- php7.0-cli
- php7.0-xmlrpc
- php7.0-curl
- php7.0-gd
- php7.0-intl
- php7.0-soap
- php7.0-mysql
- php-apcu
- php7.0-zip
- php7.0-bz2
- phpmyadmin
Copy moodlebox_install_packages:
- iptables-persistent
- hostapd
- dnsmasq
- git
- ntfs-3g
- exfat-fuse
- lockfile-progs
- incron
- nginx
- php7.0-fpm
- php7.0-cli
- php7.0-xmlrpc
- php7.0-curl
- php7.0-gd
- php7.0-intl
- php7.0-soap
- php7.0-mysql
- php-apcu
- php7.0-zip
- php7.0-bz2
- phpmyadmin
- dhcpcd5
修改檔案 roles/network/tasks/main.yml
此設定檔主要設定網路相關設定,因為BPI初始系統是使用/etc/network/interfaces,而moodlebox改用dnsmasq及dhcpcd設定,所以原本的interfaces檔把原有的設定都拿掉,內容請參考下方的「新增檔案 roles/network/templates/etc/network/interfaces.j2 」
Copy - name: configure /etc/network/interfaces
template:
src: 'etc/network/interfaces.j2'
dest: '/etc/network/interfaces'
新增資料夾 roles/network/templates/etc/network
新增檔案 roles/network/templates/etc/network/interfaces.j2
Copy # interfaces(5) file used by ifup(8) and ifdown(8)
# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d
# The loopback network interface
#auto lo
#iface lo inet loopback
#auto eth0
#iface eth0 inet dhcp