wip: ansible on alpine

This commit is contained in:
Yann Autissier
2022-10-13 00:52:38 +00:00
parent 54bf6bbbe0
commit 1203c9f005
35 changed files with 185 additions and 115 deletions
+13 -14
View File
@@ -4,6 +4,12 @@ Bootstrap hosts, installing standard packages and user settings
## Role Variables
* `hosts_cloudinit` - Install and configure cloud-init
``` yaml
hosts_cloudinit: false
```
* `hosts_cloudinit_config` - cloud-init yaml config
``` yaml
@@ -17,10 +23,12 @@ hosts_cloudinit_config:
- 'http://169.254.169.254'
```
* `hosts_cloudinit_enable` - Install and configure cloud-init
* `hosts_default_env` - List of environment variables to add in file /etc/default/myos
``` yaml
hosts_cloudinit_enable: false
hosts_default_env:
- ENV
- DOCKER
```
* `hosts_git_repositories` - Clone git repositories
@@ -112,18 +120,10 @@ hosts_ssh_username: root
hosts_update: false
```
* `hosts_user_env` - List of environment variables to add in file ~/.myos
* `hosts_user_rc` - Call specific functions on user login, allowing it to customize his session
``` yaml
hosts_user_env:
- ENV
- DOCKER
```
* `hosts_user_rc_enable` - Call specific functions on user login, allowing it to customize his session
``` yaml
hosts_user_rc_enable: false
hosts_user_rc: false
```
* `hosts_user_rc_functions` - List of specific functions to call on user login, defined in /etc/profile.d/rc_functions.sh
@@ -153,9 +153,8 @@ hosts_user_rc_functions:
roles:
- role: 'aynicos.hosts'
hosts_services:
- { "name": "local", "state": "started", "enabled": "yes" }
- { "name": "zram", "state": "started", "enabled": "yes" }
hosts_user_rc_enable: true
hosts_user_rc: true
```
## Tests
+7 -7
View File
@@ -1,6 +1,9 @@
---
# file: defaults/main.yml
# enable cloud-init
hosts_cloudinit: false
# cloud-init config
hosts_cloudinit_config:
users:
@@ -75,8 +78,8 @@ hosts_cloudinit_config:
templates_dir: /etc/cloud/templates/
ssh_svcname: sshd
# enable cloud-init
hosts_cloudinit_enable: false
# environment variables to add in /etc/default/myos
hosts_default_env: []
# git repositories to clone
hosts_git_repositories: []
@@ -129,14 +132,11 @@ hosts_ssh_public_hosts: "{{ lookup('env','ANSIBLE_SSH_PUBLIC_HOSTS').split(' ')
# remote ssh user
hosts_ssh_username: "{{ lookup('env','ANSIBLE_SSH_USERNAME') or ansible_user }}"
# update hosts every day
# update hosts every hour
hosts_update: false
# list of environment variables to add in ~/.myos
hosts_user_env: []
# run specific functions on user login
hosts_user_rc_enable: false
hosts_user_rc: false
# list of rc functions to call at user connection
hosts_user_rc_functions:
@@ -0,0 +1,4 @@
PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin
@reboot root /etc/init.d/myos ansible-pull > /var/log/ansible.log
0 * * * * root /etc/init.d/myos ansible-pull > /var/log/ansible.log
@@ -1,3 +0,0 @@
PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin
@reboot root make ansible-pull > /var/log/ansible-pull.log
0 0 * * * root make ansible-pull > /var/log/ansible-pull.log
+2 -1
View File
@@ -1,2 +1,3 @@
#!/bin/sh
cd /root && ( make ansible-pull > /var/log/ansible.log || reboot ) &
/etc/init.d/myos ansible-pull > /var/log/ansible.log
+3
View File
@@ -0,0 +1,3 @@
#!/bin/sh
/etc/init.d/myos ansible-pull > /var/log/ansible.log
@@ -212,7 +212,7 @@ ssh_add() {
else
GREP_RECURSIVE_CHAR="*"
fi
SSH_PRIVATE_KEYS="${SSH_PRIVATE_KEYS:-} ${dir}/id_rsa $(grep -l${GREP_RECURSIVE_FLAG:-} 'PRIVATE KEY' "${dir}/"${GREP_RECURSIVE_CHAR:-} 2>/dev/null |grep -vw "${dir}"/id_rsa)"
SSH_PRIVATE_KEYS="${SSH_PRIVATE_KEYS:-} ${dir}/id_ed25519 ${dir}/id_rsa $(grep -l${GREP_RECURSIVE_FLAG:-} 'PRIVATE KEY' "${dir}/"${GREP_RECURSIVE_CHAR:-} 2>/dev/null |grep -vwE "${dir}/id_(rsa|ed25519)")"
done
# shellcheck disable=SC2086
printf '%s\n' ${SSH_PRIVATE_KEYS} |while read -r file; do
@@ -238,7 +238,7 @@ ssh_del() {
else
GREP_RECURSIVE_CHAR="*"
fi
SSH_PRIVATE_KEYS="${SSH_PRIVATE_KEYS:-} ${dir}/id_rsa $(grep -l${GREP_RECURSIVE_FLAG:-} 'PRIVATE KEY' "${dir}/"${GREP_RECURSIVE_CHAR:-} 2>/dev/null |grep -vw "${dir}"/id_rsa)"
SSH_PRIVATE_KEYS="${SSH_PRIVATE_KEYS:-} ${dir}/id_ed25519 ${dir}/id_rsa $(grep -l${GREP_RECURSIVE_FLAG:-} 'PRIVATE KEY' "${dir}/"${GREP_RECURSIVE_CHAR:-} 2>/dev/null |grep -vwE "${dir}/id_(rsa|ed25519)")"
done
# shellcheck disable=SC2086
printf '%s\n' ${SSH_PRIVATE_KEYS} |while read -r file; do
@@ -270,14 +270,12 @@ tmux_detach() {
# function user_count: Print number of "users sessions"/"users"/"logged users"
user_count() {
ps ax -o user,tty 2>/dev/null |awk '
$2 ~ /^(pts|tty)/ { users_session++; logged[$1]++; };
{ count[$1]++; }
END {
for (uc in count) { c = c" "uc; }; users_count=split(c,v," ")-1;
for (ul in logged) { l = l" "ul; }; users_logged=split(l,v," ")-1;
print users_session+0"/"users_count"/"users_logged;
}'
ps ax -o pid,user,tty,comm 2>/dev/null |awk '
$3 ~ /^(pts\/|tty[sS]?|[0-9]+,)[0-9]+$/ && $4 != "getty" { users_sessions++; logged[$2]++; };
$1 ~ /^[0-9]+$/ { count[$2]++; }
END {
for (uc in count) { c = c" "uc; }; users_count=split(c,v," ");
for (ul in logged) { l = l" "ul; }; users_logged=split(l,v," ");
print users_sessions+0"/"users_count+0"/"users_logged+0;
}'
}
# vim:ts=2:sw=2:sts=2:et
@@ -2,7 +2,7 @@
Description=Call ansible
[Timer]
OnCalendar=*-*-* 00:00:00
OnCalendar=*-*-* *:00:00
Unit=ansible.service
[Install]
+4 -5
View File
@@ -2,12 +2,12 @@
# file: tasks/cloudinit.yml
- name: cloudinit - install cloud-init packages
when: hosts_cloudinit_enable|default(false) and ansible_os_family|lower != "alpine"
when: ansible_os_family|lower != "alpine"
package: name="cloud-init" state="present"
become: yes
- name: cloudinit - install cloud-init packages - alpine
when: hosts_cloudinit_enable|default(false) and ansible_os_family|lower == "alpine"
when: ansible_os_family|lower == "alpine"
with_items:
- { "name": "cloud-init", "state": "present" }
- { "name": "cloud-init-openrc", "state": "present" }
@@ -23,14 +23,13 @@
become: yes
- name: cloudinit - update /etc/cloud/cloud.cfg
when: hosts_cloudinit_enable|default(false)
template:
src: cloud.cfg.j2
dest: /etc/cloud/cloud.cfg
force: yes
- name: cloudinit - activate service
when: hosts_cloudinit_enable|default(false) and ansible_service_mgr|lower != "openrc"
when: ansible_service_mgr|lower != "openrc"
service:
name: cloud-init
state: started
@@ -38,7 +37,7 @@
become: yes
- name: cloudinit - activate service - openrc
when: hosts_cloudinit_enable|default(false) and ansible_service_mgr|lower == "openrc"
when: ansible_service_mgr|lower == "openrc"
service:
name: cloud-init
state: started
+18 -2
View File
@@ -1,7 +1,8 @@
---
# file: tasks/config.yml
- name: config - set MYOS config in /etc/default/myos
- name: config - set MYOS default config
become: true
with_items:
- APP_NAME={{ lookup('env','ANSIBLE_APP_NAME') }}
- APP_TYPE={{ lookup('env','ANSIBLE_APP_TYPE') }}
@@ -11,5 +12,20 @@
create: yes
line: '{{ item }}'
mode: '0644'
become: yes
- name: config - update MYOS default config
become: true
with_items: '{{hosts_default_env|default([])}}'
lineinfile:
path: /etc/default/myos
create: yes
line: "{{item}}={{lookup('env',item)}}"
mode: '0644'
- name: config - create /root/Makefile
become: true
template:
src: Makefile.j2
dest: ~/Makefile
force: yes
+6 -2
View File
@@ -1,13 +1,17 @@
---
# file: tasks/groups.yml
- name: groups - remove floppy group - alpine
when: ansible_os_family|lower == "alpine"
group: gid="11" name="floppy" state="absent"
become: yes
- name: groups - create dns group
when: ansible_os_family|lower != "alpine"
group: gid="11" name="dns" state="present" system="yes"
become: yes
- name: groups - add me to the dns group
when: ansible_os_family|lower != "alpine" and ansible_user_uid != "0"
when: ansible_user_uid != "0"
user: name="{{ansible_user_id}}" groups=dns append=yes
become: yes
+4 -2
View File
@@ -8,11 +8,12 @@
tags:
- boot
- import_tasks: cloudinit.yml
when: hosts_cloudinit|default(false)
tags:
- cloudinit
- import_tasks: config.yml
tags:
- cloudinit
- config
- import_tasks: dir.yml
tags:
- dir
@@ -26,6 +27,7 @@
tags:
- packages
- import_tasks: services.yml
when: hosts_services is defined
tags:
- services
- import_tasks: git.yml
@@ -35,9 +37,9 @@
tags:
- ssh
- import_tasks: update.yml
when: hosts_update|default(false)
tags:
- update
when: hosts_update|default(false)
- import_tasks: user.yml
tags:
- user
+3 -3
View File
@@ -2,7 +2,7 @@
# file: tasks/services.yml
- name: services - enable/disable hosts services
when: hosts_services is defined and ansible_service_mgr|lower != "openrc" and ansible_service_mgr|lower != "runit"
when: ansible_service_mgr|lower != "openrc" and ansible_service_mgr|lower != "runit"
with_items: "{{ hosts_services|default([]) }}"
service:
name: "{{item.name}}"
@@ -11,7 +11,7 @@
become: yes
- name: services - enable/disable hosts services - openrc
when: hosts_services is defined and ansible_service_mgr|lower == "openrc"
when: ansible_service_mgr|lower == "openrc"
with_items: "{{ hosts_services|default([]) }}"
service:
name: "{{item.name}}"
@@ -21,7 +21,7 @@
become: yes
- name: services - enable/disable hosts services - runit
when: hosts_services is defined and ansible_service_mgr|lower == "runit"
when: ansible_service_mgr|lower == "runit"
with_items: "{{ hosts_services|default([]) }}"
sysvinit:
name: "{{item.name}}"
+1 -1
View File
@@ -6,7 +6,7 @@
known_hosts:
path: /etc/ssh/ssh_known_hosts
name: "{{ item }}"
key: "{{ lookup('pipe', 'ssh-keyscan -t rsa -H ' + item) }}"
key: "{{ lookup('pipe', '/usr/bin/ssh-keyscan -H ' + item) }}"
ignore_errors: true
become: true
+28 -16
View File
@@ -2,9 +2,18 @@
# file: tasks/update.yml
- name: update - copy files
when: ansible_service_mgr|lower != "systemd" and ansible_os_family|lower != "alpine"
with_items:
- /etc/cron.d/myos
copy: src=../files/{{item}} dest={{item}} owner=root group=root mode=0644
- /etc/cron.d/ansible
copy: src=../files/{{item}} dest={{item}} owner=root group=root
become: yes
- name: update - copy files - alpine
when: ansible_service_mgr|lower != "systemd" and ansible_os_family|lower == "alpine"
with_items:
- /etc/periodic/hourly/ansible
- /etc/local.d/ansible.start
copy: src=../files/{{item}} dest={{item}} owner=root group=root mode=0755
become: yes
- name: update - copy files - systemd
@@ -12,21 +21,24 @@
with_items:
- /etc/systemd/system/ansible.service
- /etc/systemd/system/ansible.timer
copy: src=../files/{{item}} dest={{item}} owner=root group=root mode=0644
copy: src=../files/{{item}} dest={{item}} owner=root group=root
become: yes
- name: update - create /root/.myos
become: true
template:
src: myos.j2
dest: ~/.myos
force: no
mode: 0400
- name: update - enable nservices - openrc
when: ansible_service_mgr|lower == "openrc"
service:
name: local
enabled: true
runlevel: boot
become: yes
- name: update - create /root/Makefile
become: true
template:
src: Makefile.j2
dest: ~/Makefile
force: yes
- name: update - enable services - systemd
when: ansible_service_mgr|lower == "systemd"
with_items:
- ansible.service
- ansible.timer
service:
name: "{{item}}"
enabled: true
become: yes
+3 -3
View File
@@ -12,16 +12,16 @@
ignore_errors: true
- name: user - create ~/.rc.d
file: path=~/.rc.d/ state={{hosts_user_rc_enable|default(false)|ternary('directory', 'absent')}} mode="0700"
file: path=~/.rc.d/ state={{hosts_user_rc|default(false)|ternary('directory', 'absent')}} mode="0700"
- name: user - source /etc/profile.d/rc_functions.sh
when: hosts_user_rc_enable|default(false)
when: hosts_user_rc|default(false)
with_items:
- /etc/profile.d/rc_functions.sh
lineinfile: dest=~/.rc.d/00_source create=yes line="{{ item }}" mode="0600"
- name: user - enable/disable rc functions
when: hosts_user_rc_enable|default(false)
when: hosts_user_rc|default(false)
with_items: "{{ hosts_user_rc_functions|default([]) }}"
file: path="~/.rc.d/{{item.path}}" state="{{item.state}}"
+1 -1
View File
@@ -1,6 +1,6 @@
CMDS := exec
SERVICE ?= cli
-include .myos
-include /etc/default/myos
ansible:
@make -C $(ANSIBLE_GIT_DIRECTORY) ansible-run-localhost
@@ -7,11 +7,11 @@ package:
installed: true
ncurses:
installed: true
neovim:
installed: true
openssh-client:
installed: true
py3-pip:
installed: true
util-linux:
installed: true
vim:
installed: true
+1 -1
View File
@@ -5,8 +5,8 @@ hosts_packages_distro:
- { "name": "coreutils", "state": "present" }
- { "name": "groff", "state": "present" }
- { "name": "ncurses", "state": "present" }
- { "name": "neovim", "state": "present" }
- { "name": "openssh-client", "state": "present" }
- { "name": "py3-pip", "state": "present" }
- { "name": "util-linux", "state": "present" }
- { "name": "vim", "state": "present" }