Skip to content

Commit 7e6abb7

Browse files
berendtlindenb1sbstnnmnn
authored
Install ansible-core in a virtual environment (#2190)
Signed-off-by: Christian Berendt <[email protected]> Co-authored-by: Robin van der Linden <[email protected]> Co-authored-by: Sebastian Neumann <[email protected]>
1 parent abc4971 commit 7e6abb7

13 files changed

+158
-55
lines changed

ansible/manager-part-0.yml

+69-27
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,22 @@
1313
hosts: testbed-manager.testbed.osism.xyz
1414

1515
vars:
16-
repo_path: /home/ubuntu/src/github.com
1716
apt_lock_timeout: 300
17+
repo_path: /home/ubuntu/src/github.com
18+
venv_path: /opt/venv
19+
ansible_galaxy: "{{ venv_path }}/bin/ansible-galaxy"
1820

1921
tasks:
2022
- name: Fail if Ubuntu version is lower than 22.04
2123
ansible.builtin.fail:
2224
msg: "Ubuntu version is {{ ansible_distribution_version }}, see https://osism.tech/docs/advanced-guides/testbed#software for required version."
2325
when: ansible_distribution == "Ubuntu" and ansible_distribution_version < "22.04"
2426

27+
- name: Get current user
28+
ansible.builtin.user:
29+
name: "{{ ansible_user }}"
30+
register: current_user
31+
2532
- name: Update APT cache and run dist-upgrade
2633
become: true
2734
ansible.builtin.apt:
@@ -39,43 +46,47 @@
3946
executable: /bin/bash
4047
changed_when: true
4148

42-
- name: Install packages on manager
49+
- name: Install packages
4350
become: true
4451
ansible.builtin.apt:
4552
name:
4653
# The correct package name is linux-generic-hwe-22.04 on Ubuntu 22.04
4754
# and linux-generic-hwe-24.04 on Ubuntu 24.04.
4855
- "linux-generic-hwe-{{ ansible_distribution_version }}"
4956
- python3-netaddr
50-
- python3-pip
51-
update_cache: true
57+
- python3-venv
5258
changed_when: true
5359

54-
- name: Remove existing Ansible package if necessary
60+
- name: Create venv directory
5561
become: true
56-
ansible.builtin.apt:
57-
name: ansible
58-
state: absent
62+
ansible.builtin.file:
63+
owner: "{{ ansible_user }}"
64+
group: "{{ current_user.group }}"
65+
path: "{{ venv_path }}"
66+
state: directory
67+
mode: 0755
5968

60-
- name: Install ansible-core on manager
61-
become: true
62-
ansible.builtin.command: |
63-
pip3 install --no-cache-dir 'ansible-core>=2.16.0,<2.17.0'
64-
changed_when: true
69+
- name: Install ansible-core in venv
70+
ansible.builtin.pip:
71+
umask: "0022"
72+
name: "ansible-core>=2.16.0,<2.17.0"
73+
state: present
74+
virtualenv: "{{ venv_path }}"
75+
virtualenv_command: python3 -m venv
6576

66-
- name: Create source directories
77+
- name: Create directories in /opt/src
6778
become: true
6879
ansible.builtin.file:
6980
state: directory
7081
path: "/opt/src/{{ item }}"
7182
recurse: true
72-
mode: '0755'
73-
owner: ubuntu
83+
mode: 0755
84+
owner: "{{ ansible_user }}"
7485
with_items:
7586
- osism/ansible-collection-commons
7687
- osism/ansible-collection-services
7788

78-
- name: Copy sources
89+
- name: Sync sources in /opt/src
7990
ansible.posix.synchronize:
8091
src: "{{ repo_path }}/{{ item }}"
8192
delete: true
@@ -89,18 +100,28 @@
89100
ansible.builtin.file:
90101
state: directory
91102
path: /usr/share/ansible
92-
mode: '0755'
103+
mode: 0755
93104

94-
- name: Install collections
105+
- name: Install collections from Ansible galaxy
95106
become: true
96-
ansible.builtin.shell: |
97-
ansible-galaxy collection install --collections-path /usr/share/ansible/collections ansible.netcommon
98-
ansible-galaxy collection install --collections-path /usr/share/ansible/collections ansible.posix
99-
ansible-galaxy collection install --collections-path /usr/share/ansible/collections community.docker
100-
ansible-galaxy collection install --collections-path /usr/share/ansible/collections /opt/src/osism/ansible-collection-commons
101-
ansible-galaxy collection install --collections-path /usr/share/ansible/collections /opt/src/osism/ansible-collection-services
102-
chmod -R +r /usr/share/ansible
103-
changed_when: true
107+
ansible.builtin.command: |
108+
{{ ansible_galaxy }} collection install --collections-path /usr/share/ansible/collections {{ item }}
109+
register: result
110+
changed_when: "'was installed successfully' in result.stdout"
111+
loop:
112+
- ansible.netcommon
113+
- ansible.posix
114+
- community.docker
115+
116+
- name: Install local collections
117+
become: true
118+
ansible.builtin.command: |
119+
{{ ansible_galaxy }} collection install --collections-path /usr/share/ansible/collections /opt/src/osism/{{ item }}
120+
register: result
121+
changed_when: "'was installed successfully' in result.stdout"
122+
loop:
123+
- ansible-collection-commons
124+
- ansible-collection-services
104125

105126
- name: Create operator user
106127
hosts: testbed-manager.testbed.osism.xyz
@@ -112,5 +133,26 @@
112133
operator_authorized_keys:
113134
- "{{ lookup('file', '.id_rsa.' + cloud_env + '.pub') }}"
114135

136+
venv_path: /opt/venv
137+
ansible_python_interpreter: "{{ venv_path }}/bin/python3"
138+
115139
roles:
116140
- role: osism.commons.operator
141+
142+
- name: Run manager part 0
143+
hosts: testbed-manager.testbed.osism.xyz
144+
145+
vars:
146+
operator_user: dragon
147+
operator_group: dragon
148+
venv_path: /opt/venv
149+
150+
tasks:
151+
- name: "Recursively change ownership of {{ venv_path }}"
152+
become: true
153+
ansible.builtin.file:
154+
path: "{{ venv_path }}"
155+
state: directory
156+
owner: "{{ operator_user }}"
157+
group: "{{ operator_group }}"
158+
recurse: true

ansible/manager-part-1.yml

+26-6
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55

66
vars:
77
ansible_ssh_user: dragon
8+
apt_lock_timeout: 300
89
operator_user: dragon
910
repo_path: /home/ubuntu/src/github.com
1011
version_manager: latest
1112
is_zuul: false
1213

14+
venv_path: /opt/venv
15+
ansible_playbook: "{{ venv_path }}/bin/ansible-playbook"
16+
1317
tasks:
1418
- name: Copy SSH public key
1519
ansible.builtin.copy:
@@ -42,12 +46,28 @@
4246
delete: true
4347
dest: /opt/configuration
4448

45-
- name: Install python-gilt on manager
49+
- name: Install required packages
4650
become: true
47-
ansible.builtin.command: |
48-
pip3 install --no-cache-dir python-gilt==1.2.3
49-
when: version_manager != "latest"
50-
changed_when: true
51+
ansible.builtin.apt:
52+
name: "{{ item }}"
53+
state: present
54+
lock_timeout: "{{ apt_lock_timeout }}"
55+
loop:
56+
- build-essential
57+
- python3-dev
58+
59+
- name: Install python requirements in venv
60+
ansible.builtin.pip:
61+
umask: "0022"
62+
name: "{{ item }}"
63+
state: present
64+
virtualenv: "{{ venv_path }}"
65+
virtualenv_command: python3 -m venv
66+
loop:
67+
- docker
68+
- netifaces
69+
- "python-gilt==1.2.3"
70+
- requests
5171

5272
# shell required because of: command module does not accept
5373
# setting environment variables inline.
@@ -100,5 +120,5 @@
100120
changed_when: true
101121

102122
- name: Run manager part 2
103-
ansible.builtin.command: "ansible-playbook -i testbed-manager.testbed.osism.xyz, -e version_manager={{ version_manager }} /opt/configuration/ansible/manager-part-2.yml"
123+
ansible.builtin.command: "{{ ansible_playbook }} -i testbed-manager.testbed.osism.xyz, -e version_manager={{ version_manager }} /opt/configuration/ansible/manager-part-2.yml"
104124
changed_when: true

ansible/manager-part-2.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
gather_facts: true
66

77
vars:
8-
ansible_python_interpreter: /usr/bin/python3
8+
venv_path: /opt/venv
9+
ansible_python_interpreter: "{{ venv_path }}/bin/python3"
910

1011
version_manager: latest
1112

@@ -39,7 +40,8 @@
3940
gather_facts: true
4041

4142
vars:
42-
ansible_python_interpreter: /usr/bin/python3
43+
venv_path: /opt/venv
44+
ansible_python_interpreter: "{{ venv_path }}/bin/python3"
4345

4446
version_manager: latest
4547

ansible/manager-part-3.yml

+11-10
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,12 @@
55
gather_facts: false
66

77
vars:
8-
ansible_python_interpreter: /usr/bin/python3
8+
venv_path: /opt/venv
9+
ansible_python_interpreter: "{{ venv_path }}/bin/python3"
10+
911
apt_lock_timeout: 300
1012

1113
tasks:
12-
- name: Install required packages
13-
become: true
14-
ansible.builtin.apt:
15-
name: python3-netifaces
16-
state: present
17-
lock_timeout: "{{ apt_lock_timeout }}"
18-
1914
- name: Create custom facts directory
2015
become: true
2116
ansible.builtin.file:
@@ -43,6 +38,9 @@
4338
gather_facts: true
4439

4540
vars:
41+
venv_path: /opt/venv
42+
ansible_python_interpreter: "{{ venv_path }}/bin/python3"
43+
4644
images:
4745
- "{{ ara_server_image }}"
4846
- "{{ ara_server_mariadb_image }}"
@@ -142,7 +140,8 @@
142140
gather_facts: true
143141

144142
vars:
145-
ansible_python_interpreter: /usr/bin/python3
143+
venv_path: /opt/venv
144+
ansible_python_interpreter: "{{ venv_path }}/bin/python3"
146145

147146
vars_files:
148147
- /opt/configuration/inventory/group_vars/testbed-managers.yml
@@ -180,7 +179,9 @@
180179
gather_facts: true
181180

182181
vars:
183-
ansible_python_interpreter: /usr/bin/python3
182+
venv_path: /opt/venv
183+
ansible_python_interpreter: "{{ venv_path }}/bin/python3"
184+
184185
manager_service_restart: false
185186

186187
vars_files:

contrib/setup-testbed.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,19 @@ def clone_repo(path: str, repo_address: str, branch: str) -> None:
7575
print(f"+ {repo_command}")
7676
subprocess.check_output(repo_command, shell=True)
7777

78-
repo_command = f"git -C {checkout_path} checkout {branch or 'main'}"
78+
repo_command = f"git -C {checkout_path} checkout 'main'"
7979
print(f"+ {repo_command}")
8080
subprocess.check_output(repo_command, shell=True)
8181

8282
repo_command = f"git -C {checkout_path} pull"
8383
print(f"+ {repo_command}")
8484
subprocess.check_output(repo_command, shell=True)
8585

86+
if branch and branch != "main":
87+
repo_command = f"git -C {checkout_path} checkout {branch}"
88+
print(f"+ {repo_command}")
89+
subprocess.check_output(repo_command, shell=True)
90+
8691

8792
basedir = os.path.realpath(os.path.dirname(os.path.realpath(__file__)) + "/../")
8893
file_path = f"{basedir}/playbooks/vars/repositories.yml"

gilt.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
- src: src/render-images.py
88
dst: environments/manager/
99
post_commands:
10-
- python3 render-images.py
10+
- /opt/configuration/scripts/wrapper-gilt.sh render-images
1111
- rm render-images.py
1212
- src: src/set-versions.py
1313
dst: environments/
1414
post_commands:
15-
- python3 set-versions.py
15+
- /opt/configuration/scripts/wrapper-gilt.sh set-versions
1616
- rm set-versions.py

playbooks/managerless/vars/repositories.yml

+4
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ repositories:
44
ansible-collection-commons:
55
path: github.com/osism/ansible-collection-commons
66
repo: https://github.com/osism/ansible-collection-commons
7+
branch: main
78
ansible-collection-services:
89
path: github.com/osism/ansible-collection-services
910
repo: https://github.com/osism/ansible-collection-services
11+
branch: main
1012
terraform-base:
1113
path: github.com/osism/terraform-base
1214
repo: https://github.com/osism/terraform-base
15+
branch: main
1316
testbed:
1417
path: github.com/osism/testbed
1518
repo: https://github.com/osism/testbed
19+
branch: main

playbooks/vars/repositories.yml

+4
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ repositories:
44
ansible-collection-commons:
55
path: github.com/osism/ansible-collection-commons
66
repo: https://github.com/osism/ansible-collection-commons
7+
branch: main
78
ansible-collection-services:
89
path: github.com/osism/ansible-collection-services
910
repo: https://github.com/osism/ansible-collection-services
11+
branch: main
1012
terraform-base:
1113
path: github.com/osism/terraform-base
1214
repo: https://github.com/osism/terraform-base
15+
branch: main
1316
testbed:
1417
path: github.com/osism/testbed
1518
repo: https://github.com/osism/testbed
19+
branch: main

scripts/deploy/000-manager-service.sh

+8-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ set -e
55
source /opt/manager-vars.sh
66
source /opt/configuration/scripts/include.sh
77

8-
/opt/configuration/scripts/set-manager-version.sh $MANAGER_VERSION
8+
# The latest version of the Manager is used by default. If a different
9+
# version is to be used, it must be used accordingly.
10+
11+
if [[ $MANAGER_VERSION != "latest" ]]; then
12+
/opt/configuration/scripts/set-manager-version.sh $MANAGER_VERSION
13+
fi
914

1015
# For a stable release, the versions of Ceph and OpenStack to use
1116
# are set by the version of the stable release (set via the
@@ -16,10 +21,12 @@ if [[ $MANAGER_VERSION == "latest" ]]; then
1621
/opt/configuration/scripts/set-openstack-version.sh $OPENSTACK_VERSION
1722
fi
1823

24+
source /opt/venv/bin/activate
1925
ansible-playbook \
2026
-i testbed-manager.testbed.osism.xyz, \
2127
--vault-password-file /opt/configuration/environments/.vault_pass \
2228
/opt/configuration/ansible/manager-part-3.yml
29+
deactivate
2330

2431
cp /home/dragon/.ssh/id_rsa.pub /opt/ansible/secrets/id_rsa.operator.pub
2532

scripts/set-manager-version.sh

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ fi
1717

1818
# Sync testbed repo with generics
1919
pushd /opt/configuration
20-
pip3 install --no-cache-dir python-gilt==1.2.3
21-
export PATH=$PATH:/home/dragon/.local/bin
20+
source /opt/venv/bin/activate
21+
pip3 install --no-cache-dir python-gilt==1.2.3 requests
2222
GILT=$(which gilt)
2323
${GILT} overlay
2424
${GILT} overlay
25+
deactivate
2526
popd

0 commit comments

Comments
 (0)