-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit_pi.yml
105 lines (88 loc) · 3.46 KB
/
init_pi.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
99
100
101
102
103
---
# prerequis : installer sshpass localement
# Play 1 : Create
- hosts: pi
gather_facts: no
ignore_unreachable: yes
vars:
ansible_ssh_user: pi
raspi_config_new_user: "{{ hostvars[inventory_hostname].ansible_ssh_user }}"
raspi_config_host: "{{ ansible_host|d(inventory_hostname) }}"
pre_tasks:
- name : "Insert SSH key in authorized_keys for pi@{{ raspi_config_host }}"
shell: |
sshpass -p "raspberry" ssh-copy-id -o StrictHostKeyChecking=no pi@{{ raspi_config_host }}
delegate_to: localhost
register: raspiconfig_ssh_setup_result
changed_when: "'Number of key(s) added:' in raspiconfig_ssh_setup_result.stdout"
failed_when: false
# TODO : changer failed_when pour gérer "/bin/sh: 1: sshpass: not found"
# "msg": "non-zero return code",
# "rc": 127,
tasks:
- name: Bootstrap Ansible on remote machine
raw: apt-get install -y python python-simplejson python-setuptools
register: raspiconfig_config_python_install_result
changed_when: "'NOUVEAUX' in raspiconfig_config_python_install_result.stdout or 'NEW' in raspiconfig_config_python_install_result"
become: yes
# python-setuptools is required for further usage of pip module. https://docs.ansible.com/ansible/latest/collections/ansible/builtin/pip_module.html
# The setuptools package must be installed for both the Ansible Python interpreter and for the version of Python specified by the 'executable' option of the Ansible pip module.
- name: Add new group, user and authorized_keys
become: yes
block:
- name: Create new admin group, for passwordless sudo
group:
name: admin
- name: "Create new {{ raspi_config_new_user }} user instead of pi"
user:
name: "{{ raspi_config_new_user }}"
group: admin
groups: adm,dialout,cdrom,sudo,audio,video,plugdev,games,users,input,netdev,spi,i2c,gpio
# groups list and signification : https://raspberrypi.stackexchange.com/questions/70214/what-does-each-of-the-default-groups-on-the-raspberry-pi-do
- name: "Insert SSH key in authorized_keys for {{ raspi_config_new_user }}@{{ raspi_config_host }}"
authorized_key:
user: "{{ raspi_config_new_user }}"
state: present
key: "{{ lookup('file', '/home/karim/.ssh/id_rsa.pub') }}"
- name: Allow passwordless sudo
lineinfile:
path: /etc/sudoers
state: present
line: '%admin ALL=(ALL) NOPASSWD: ALL'
validate: '/usr/sbin/visudo -cf %s'
- hosts: pi
become: yes
tasks:
- name: Lock pi user
user:
name: pi
password_lock : yes
- name: update all packages
apt:
upgrade: dist
- name: Install acl package (for sudo-ing to non-root user)
apt:
name: acl
- name: Set timezone to Europe/Paris
timezone:
name: Europe/Paris
- name: Ensure fr_FR.UTF-8 locale exists
locale_gen:
name: fr_FR.UTF-8
state: present
- name: Set hostname
hostname:
name: "{{ ansible_host|d(inventory_hostname) }}"
- name: "Create custom fact directory"
file:
path: "/etc/ansible/facts.d"
state: "directory"
owner: root
group: root
- name: Add custom fact for detecting Pi model
copy:
src: files/pi.fact
dest: /etc/ansible/facts.d/pi.fact
mode: 0755
owner: root
group: root