Skip to content

Commit c626f7f

Browse files
authored
Ansible in init pr 2.x (#1554)
* Adding new Python pip package role. * Updating NGINX and Boto3 roles to use the new Python pip role. * Moving Ansible install to _init. * Detecting connection type before installing Ansible. * The _init role should not generate SSH keys and ce_provision should optionally install a new user. * Updating documentation. * Switching to using the user_provision role for controller user in _init. * Allowing for ce_provision to install Ansible in another location. * Passing vars to the core Ansible install. * We will need linters if the system didn't install them. * Duplicity role doesn't need to ensure permissions, it's done in python_pip_packages already. * Allowing ce-provision to set a different UID from the system user.
1 parent 2470f04 commit c626f7f

File tree

27 files changed

+301
-158
lines changed

27 files changed

+301
-158
lines changed

Diff for: docs/_Sidebar.md

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
- [Process Manager](/roles/debian/process_manager)
7171
- [Python Boto](/roles/debian/python_boto)
7272
- [Python Common](/roles/debian/python_common)
73+
- [Python Pip Packages](/roles/debian/python_pip_packages)
7374
- [rkhunter](/roles/debian/rkhunter)
7475
- [Rsyslog](/roles/debian/rsyslog)
7576
- [solr](/roles/debian/solr)

Diff for: docs/roles/_init.md

+12
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ This is meant to ALWAYS be included as the first task of a play. If you include
99
## Default variables
1010
```yaml
1111
---
12+
_ce_provision_username: "{% if is_local is defined and is_local %}ce-dev{% else %}controller{% endif %}"
13+
_venv_path: "/home/{{ _ce_provision_username }}/ansible"
14+
_venv_command: /usr/bin/python3 -m venv
15+
_venv_install_username: "{{ _ce_provision_username }}"
16+
_ce_ansible_timer_name: upgrade_ce_provision_ansible
17+
1218
_init:
1319
# A list of var directories to include. We only support .yml extensions.
1420
# This is used to detect if the playbook must re-run or not.
@@ -18,11 +24,17 @@ _init:
1824

1925
# Although these variables logically belong with ce_provision, the _init role needs to
2026
# gather the extra variables if there are any, so there are _init variables.
27+
28+
# ce-provision user creation
29+
ce_provision_new_user: true # set to false if user already exists or is ephemeral, e.g. an LDAP user
30+
#ce_provision_uid # optionally hardcode the UID for this user
31+
2132
# Extra config repo.
2233
ce_provision_extra_repository: ""
2334
ce_provision_extra_repository_branch: "master"
2435
ce_provision_extra_repository_skip_checkout: false
2536
ce_provision_extra_repository_vars_file: "custom.yml"
37+
2638
# Whether to commit back changes to extra repo.
2739
ce_provision_extra_repository_push: false
2840
ce_provision_extra_repository_allowed_vars: []

Diff for: docs/roles/debian/ansible.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Note, it is vitally important that Ansible is *not* installed via `apt` or `pip`
1111
```yaml
1212
---
1313
ce_ansible:
14-
# These are usually set within another role using _venv_path, _venv_command and _install_username but can be overridden.
14+
# These are usually set within another role using _venv_path, _venv_command and _venv_install_username but can be overridden.
1515
#venv_path: "/home/{{ ce_provision.username }}/ansible"
1616
#venv_command: /usr/bin/python3.11 -m venv
1717
#install_username: deploy # user to become when creating venv

Diff for: docs/roles/debian/ce_provision.md

+15-16
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
11
# ce-provision
2-
Installs Code Enigma's infrastructure management stack on a server.
2+
Installs Code Enigma's infrastructure management stack on a server. Note, the `_init` role creates the user and installs Ansible in a virtual environment, so that must be run prior to the `ce_provision` role.
3+
34
<!--TOC-->
45
<!--ENDTOC-->
56

67
<!--ROLEVARS-->
78
## Default variables
89
```yaml
910
---
10-
# See roles/_init/defaults/main.yml for extra variables repo settings.
11-
_ce_provision:
12-
username: "{% if is_local is defined and is_local %}ce-dev{% else %}controller{% endif %}"
13-
11+
# See roles/_init/defaults/main.yml for Ansible installation, controller user creation and extra variables repo settings.
1412
ce_provision:
15-
# Location of Ansible installation and components.
16-
venv_path: "/home/{{ _ce_provision.username }}/ansible"
17-
venv_command: /usr/bin/python3 -m venv
18-
install_username: "{{ _ce_provision.username }}"
19-
upgrade_timer_name: upgrade_ce_provision_ansible
13+
# Optional venv overrides - if commented out, values taken from _init defaults.
14+
#venv_path: "/home/{{ _ce_provision_username }}/ansible"
15+
#venv_command: /usr/bin/python3 -m venv
16+
#venv_install_username: "{{ _ce_provision_username }}"
17+
#upgrade_timer_name: upgrade_ce_provision_ansible
2018
# Other ce-provision settings.
21-
username: "{{ _ce_provision.username }}"
22-
new_user: true # set to false if user already exists or is ephemeral, e.g. an LDAP user
23-
key_name: id_rsa.pub # existing users may have a key of a different name
19+
new_user: "{{ _init.ce_provision_new_user }}" # see _init defaults, set to false if user already exists or is ephemeral, e.g. an LDAP user
20+
username: "{{ _ce_provision_username }}" # see _init defaults
21+
#uid: "{{ _init.ce_provision_uid }}" # see _init defaults, optionally hardcode the UID for this user
22+
public_key_name: id_rsa.pub # existing users may have a key of a different name
2423
# Main repo.
2524
own_repository: "https://github.com/codeenigma/ce-provision.git"
2625
own_repository_branch: "master"
2726
own_repository_skip_checkout: false
2827
# Destination.
29-
local_dir: "/home/{{ _ce_provision.username }}/ce-provision"
28+
local_dir: "/home/{{ _ce_provision_username }}/ce-provision"
3029
# Private config repo.
3130
config_repository: ""
3231
config_repository_branch: "master"
@@ -43,10 +42,10 @@ ce_provision:
4342
branch: master
4443
# File containing default roles and collections to install via Ansible Galaxy.
4544
# Roles will be installed to $HOME/.ansible/roles for the provision user. This roles path should be added to your ansible.cfg file.
46-
galaxy_custom_requirements_file: "/home/{{ _ce_provision.username }}/ce-provision/config/files/galaxy-requirements.yml"
45+
galaxy_custom_requirements_file: "/home/{{ _ce_provision_username }}/ce-provision/config/files/galaxy-requirements.yml"
4746
upgrade_galaxy:
4847
enabled: true
49-
command: "/home/{{ _ce_provision.username }}/ansible/bin/ansible-galaxy collection install --force" # must match venv_path
48+
command: "/home/{{ _ce_provision_username }}/ansible/bin/ansible-galaxy collection install --force" # must match venv_path
5049
on_calendar: "Mon *-*-* 04:00:00" # see systemd.time documentation - https://www.freedesktop.org/software/systemd/man/latest/systemd.time.html#Calendar%20Events
5150

5251
```

Diff for: docs/roles/debian/nginx.md

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ nginx:
7272
# on_calendar: "Mon *-*-* 04:00:00"
7373
ratelimitingcrawlers: true
7474
is_default: true
75+
is_behind_cloudfront: false # set to true to disable gzip.
7576
basic_auth:
7677
auth_enabled: false
7778
auth_file: "" # optionally provide the path on the deploy server to a htpasswd file - WARNING - it must be valid and will not be checked!

Diff for: docs/roles/debian/python_boto.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Role to install the `boto3` library for Python integration with AWS services.
99
```yaml
1010
python_boto:
1111
boto3_version: "" # version string, e.g. "1.22.13" - empty string means latest
12-
# These are usually set within another role using _venv_path and _venv_command but can be overridden.
12+
# These are usually set within another role using _venv_path, _venv_command and _venv_install_username but can be overridden.
1313
#venv_path: /path/to/venv
1414
#venv_command: /usr/bin/python3.11 -m venv
1515
#install_username: deploy # user to become when creating venv

Diff for: docs/roles/debian/python_pip_packages.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Python Pip Packages
2+
Role to install a list of Python packages in a specified Python virtual environment.
3+
4+
<!--TOC-->
5+
<!--ENDTOC-->
6+
7+
<!--ROLEVARS-->
8+
## Default variables
9+
```yaml
10+
---
11+
python_pip_packages:
12+
packages: []
13+
# - name: pip
14+
# state: latest
15+
16+
# These are usually set within another role using _venv_path, _venv_command and _venv_install_username but can be overridden.
17+
#venv_path: /path/to/venv
18+
#venv_command: /usr/bin/python3.11 -m venv
19+
#install_username: deploy # user to become when creating venv
20+
```
21+
22+
<!--ENDROLEVARS-->

Diff for: roles/_init/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ This is meant to ALWAYS be included as the first task of a play. If you include
99
## Default variables
1010
```yaml
1111
---
12+
_ce_provision_username: "{% if is_local is defined and is_local %}ce-dev{% else %}controller{% endif %}"
13+
_venv_path: "/home/{{ _ce_provision_username }}/ansible"
14+
_venv_command: /usr/bin/python3 -m venv
15+
_venv_install_username: "{{ _ce_provision_username }}"
16+
_ce_ansible_timer_name: upgrade_ce_provision_ansible
17+
1218
_init:
1319
# A list of var directories to include. We only support .yml extensions.
1420
# This is used to detect if the playbook must re-run or not.
@@ -18,11 +24,17 @@ _init:
1824

1925
# Although these variables logically belong with ce_provision, the _init role needs to
2026
# gather the extra variables if there are any, so there are _init variables.
27+
28+
# ce-provision user creation
29+
ce_provision_new_user: true # set to false if user already exists or is ephemeral, e.g. an LDAP user
30+
#ce_provision_uid # optionally hardcode the UID for this user
31+
2132
# Extra config repo.
2233
ce_provision_extra_repository: ""
2334
ce_provision_extra_repository_branch: "master"
2435
ce_provision_extra_repository_skip_checkout: false
2536
ce_provision_extra_repository_vars_file: "custom.yml"
37+
2638
# Whether to commit back changes to extra repo.
2739
ce_provision_extra_repository_push: false
2840
ce_provision_extra_repository_allowed_vars: []

Diff for: roles/_init/defaults/main.yml

+12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
---
2+
_ce_provision_username: "{% if is_local is defined and is_local %}ce-dev{% else %}controller{% endif %}"
3+
_venv_path: "/home/{{ _ce_provision_username }}/ansible"
4+
_venv_command: /usr/bin/python3 -m venv
5+
_venv_install_username: "{{ _ce_provision_username }}"
6+
_ce_ansible_timer_name: upgrade_ce_provision_ansible
7+
28
_init:
39
# A list of var directories to include. We only support .yml extensions.
410
# This is used to detect if the playbook must re-run or not.
@@ -8,11 +14,17 @@ _init:
814

915
# Although these variables logically belong with ce_provision, the _init role needs to
1016
# gather the extra variables if there are any, so there are _init variables.
17+
18+
# ce-provision user creation
19+
ce_provision_new_user: true # set to false if user already exists or is ephemeral, e.g. an LDAP user
20+
#ce_provision_uid # optionally hardcode the UID for this user
21+
1122
# Extra config repo.
1223
ce_provision_extra_repository: ""
1324
ce_provision_extra_repository_branch: "master"
1425
ce_provision_extra_repository_skip_checkout: false
1526
ce_provision_extra_repository_vars_file: "custom.yml"
27+
1628
# Whether to commit back changes to extra repo.
1729
ce_provision_extra_repository_push: false
1830
ce_provision_extra_repository_allowed_vars: []

Diff for: roles/_init/tasks/main.yml

+24
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,27 @@
9898
- _init.ce_provision_extra_repository
9999
- _init.ce_provision_extra_repository_vars_file
100100
- _init.ce_provision_extra_repository_allowed_vars
101+
102+
# Install Ansible under the controller user for all servers
103+
# Ensure ansible_connection == 'ssh' (i.e. we are connecting to a server) before executing
104+
- name: Install Ansible.
105+
ansible.builtin.include_role:
106+
name: debian/user_provision
107+
when: ansible_connection == 'ssh'
108+
109+
- name: Install Ansible.
110+
ansible.builtin.include_role:
111+
name: debian/ansible
112+
vars:
113+
ce_ansible:
114+
venv_path: "{{ _venv_path }}"
115+
venv_command: "{{ _venv_command }}"
116+
install_username: "{{ _venv_install_username }}"
117+
upgrade:
118+
enabled: true
119+
command: "/home/{{ _venv_install_username }}/ansible/bin/python3 -m pip install --upgrade ansible"
120+
on_calendar: "*-*-* 01:30:00"
121+
timer_name: "{{ _ce_ansible_timer_name }}"
122+
linters:
123+
enabled: false
124+
when: ansible_connection == 'ssh'

Diff for: roles/_meta/common_base/meta/main.yml

-13
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ dependencies:
2727
when: is_local is not defined or not is_local
2828
- role: debian/locales
2929
- role: debian/user_root
30-
- role: debian/user_provision
3130
- role: debian/apt_extra_packages
3231
- role: debian/apt_unattended_upgrades
3332
when: is_local is not defined or not is_local
@@ -42,15 +41,3 @@ dependencies:
4241
when: is_local is not defined or not is_local
4342
- role: debian/sudo_config
4443
when: is_local is not defined or not is_local
45-
- role: debian/ansible
46-
ce_ansible:
47-
venv_path: "/home/{{ user_provision.username }}/ansible"
48-
venv_command: "/usr/bin/python3 -m venv"
49-
install_username: "{{ user_provision.username }}"
50-
upgrade:
51-
enabled: true
52-
command: "/home/{{ user_provision.username }}/ansible/bin/python3 -m pip install --upgrade ansible"
53-
on_calendar: "*-*-* 01:30:00"
54-
timer_name: upgrade_ansible
55-
linters:
56-
enabled: false

Diff for: roles/debian/ansible/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Note, it is vitally important that Ansible is *not* installed via `apt` or `pip`
1111
```yaml
1212
---
1313
ce_ansible:
14-
# These are usually set within another role using _venv_path, _venv_command and _install_username but can be overridden.
14+
# These are usually set within another role using _venv_path, _venv_command and _venv_install_username but can be overridden.
1515
#venv_path: "/home/{{ ce_provision.username }}/ansible"
1616
#venv_command: /usr/bin/python3.11 -m venv
1717
#install_username: deploy # user to become when creating venv

Diff for: roles/debian/ansible/defaults/main.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
ce_ansible:
3-
# These are usually set within another role using _venv_path, _venv_command and _install_username but can be overridden.
3+
# These are usually set within another role using _venv_path, _venv_command and _venv_install_username but can be overridden.
44
#venv_path: "/home/{{ ce_provision.username }}/ansible"
55
#venv_command: /usr/bin/python3.11 -m venv
66
#install_username: deploy # user to become when creating venv

Diff for: roles/debian/ansible/tasks/main.yml

+16-32
Original file line numberDiff line numberDiff line change
@@ -33,42 +33,26 @@
3333
- python3-venv
3434
- cloud-init # package can get removed with python3-yaml but we need it for auto-scale
3535

36-
- name: Ensure pip is at latest version.
37-
ansible.builtin.pip:
38-
name:
39-
- pip
40-
state: latest
41-
virtualenv: "{{ ce_ansible.venv_path | default(_venv_path) }}"
42-
virtualenv_command: "{{ ce_ansible.venv_command | default(_venv_command) }}"
43-
become_user: "{{ ce_ansible.install_username | default(_install_username) }}"
44-
become: true
45-
46-
- name: Install Ansible.
47-
ansible.builtin.pip:
48-
name:
49-
- ansible
50-
virtualenv: "{{ ce_ansible.venv_path | default(_venv_path) }}"
51-
virtualenv_command: "{{ ce_ansible.venv_command | default(_venv_command) }}"
52-
become_user: "{{ ce_ansible.install_username | default(_install_username) }}"
53-
become: true
36+
- name: Install pip and Ansible.
37+
ansible.builtin.include_role:
38+
name: debian/python_pip_packages
39+
vars:
40+
python_pip_packages:
41+
packages:
42+
- name: pip
43+
state: latest
44+
- name: ansible
5445

5546
- name: Install linters.
56-
ansible.builtin.pip:
57-
name:
58-
- ansible-lint
59-
- yamllint
60-
virtualenv: "{{ ce_ansible.venv_path | default(_venv_path) }}"
61-
virtualenv_command: "{{ ce_ansible.venv_command | default(_venv_command) }}"
62-
become_user: "{{ ce_ansible.install_username | default(_install_username) }}"
63-
become: true
47+
ansible.builtin.include_role:
48+
name: debian/python_pip_packages
49+
vars:
50+
python_pip_packages:
51+
packages:
52+
- name: ansible-lint
53+
- name: yamllint
6454
when: ce_ansible.linters.enabled
6555

66-
- name: Ensure Ansible venv permissions.
67-
ansible.builtin.file:
68-
path: "{{ ce_ansible.venv_path | default(_venv_path) }}"
69-
owner: "{{ ce_ansible.install_username | default(_install_username) }}"
70-
group: "{{ ce_ansible.install_username | default(_install_username) }}"
71-
7256
- name: Add the venv to $PATH using profile.d.
7357
ansible.builtin.copy:
7458
content: "export PATH=$PATH:{{ ce_ansible.venv_path | default(_venv_path) }}/bin"

Diff for: roles/debian/ce_deploy/tasks/main.yml

+10-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
recurse: true
3333
when: ce_deploy.new_user
3434

35-
- name: Fetch back the SSH pub key.
35+
- name: Place the public key in the ce-provision data directory on the controller server.
3636
ansible.builtin.fetch:
3737
dest: "{{ _ce_provision_data_dir }}"
3838
src: "/home/{{ ce_deploy.username }}/.ssh/{{ ce_deploy.key_name }}"
@@ -48,7 +48,6 @@
4848
dest: "{{ ce_deploy.local_dir }}"
4949
version: "{{ ce_deploy.own_repository_branch | default('master') }}"
5050
update: true
51-
# @todo?
5251
accept_hostkey: true
5352
become: true
5453
become_user: "{{ ce_deploy.username }}"
@@ -132,7 +131,7 @@
132131
ansible.builtin.set_fact:
133132
_venv_path: "{{ ce_deploy.venv_path }}"
134133
_venv_command: "{{ ce_deploy.venv_command }}"
135-
_install_username: "{{ ce_deploy.install_username }}"
134+
_venv_install_username: "{{ ce_deploy.install_username }}"
136135
_ce_ansible_timer_name: "{{ ce_deploy.upgrade_timer_name }}"
137136

138137
- name: Install Ansible.
@@ -175,6 +174,14 @@
175174
become_user: "{{ ce_deploy.username }}"
176175
when: _ce_deploy_custom_galaxy_requirements.stat.exists
177176

177+
- name: Ensure Ansible venv permissions.
178+
ansible.builtin.file:
179+
path: "{{ _venv_path }}"
180+
state: directory
181+
recurse: true
182+
owner: "{{ ce_deploy.username }}"
183+
group: "{{ ce_deploy.username }}"
184+
178185
- name: Create systemd timer to upgrade mandatory ansible-galaxy collections.
179186
ansible.builtin.include_role:
180187
name: contrib/systemd_timers

0 commit comments

Comments
 (0)