Skip to content

Commit 58eac68

Browse files
committed
feat: ansible upgrade
1 parent 11faee4 commit 58eac68

File tree

5 files changed

+77
-63
lines changed

5 files changed

+77
-63
lines changed

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ requirements:
2020
pip install -qr requirements/pip.txt --exists-action w
2121
pip install -qr requirements.txt --exists-action w
2222

23+
requirements3_12:
24+
pip install -qr requirements/pip.txt --exists-action w
25+
pip install -qr requirements3_12txt --exists-action w
26+
27+
2328
COMMON_CONSTRAINTS_TXT=requirements/common_constraints.txt
2429
.PHONY: $(COMMON_CONSTRAINTS_TXT)
2530
$(COMMON_CONSTRAINTS_TXT):

playbooks/library/ec2_lookup

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

1717
from __future__ import absolute_import
1818
from __future__ import print_function
19-
import six
19+
import sys
20+
import boto3
21+
from ansible.module_utils.basic import AnsibleModule
22+
2023
DOCUMENTATION = '''
2124
---
2225
module: ec2_lookup
@@ -28,8 +31,8 @@ options:
2831
region:
2932
description:
3033
- The AWS region to use. Must be specified if ec2_url
31-
is not used. If not specified then the value of the
32-
EC2_REGION environment variable, if any, is used.
34+
is not used. If not specified then the value of
35+
the EC2_REGION environment variable, if any, is used.
3336
required: false
3437
default: null
3538
aliases: [ 'aws_region', 'ec2_region' ]
@@ -42,20 +45,20 @@ options:
4245
aliases: [ 'ec2_secret_key', 'secret_key' ]
4346
aws_access_key:
4447
description:
45-
- AWS access key. If not set then the value of the
46-
AWS_ACCESS_KEY environment variable is used.
48+
- AWS access key. If not set then the value of
49+
the AWS_ACCESS_KEY environment variable is used.
4750
required: false
4851
default: null
4952
aliases: [ 'ec2_access_key', 'access_key' ]
5053
tags:
51-
desription:
54+
description:
5255
- tags to lookup
5356
required: false
5457
default: null
5558
type: dict
5659
aliases: []
5760
58-
requirements: [ "boto" ]
61+
requirements: [ "boto3" ]
5962
author: John Jarvis
6063
'''
6164

@@ -70,8 +73,6 @@ EXAMPLES = '''
7073
Name: foo
7174
'''
7275

73-
import sys
74-
7576
AWS_REGIONS = ['ap-northeast-1',
7677
'ap-southeast-1',
7778
'ap-southeast-2',
@@ -81,17 +82,8 @@ AWS_REGIONS = ['ap-northeast-1',
8182
'us-west-1',
8283
'us-west-2']
8384

84-
try:
85-
import boto.ec2
86-
from boto.ec2 import connect_to_region
87-
except ImportError:
88-
print("failed=True msg='boto required for this module'")
89-
sys.exit(1)
90-
91-
9285
def main():
93-
94-
module=AnsibleModule(
86+
module = AnsibleModule(
9587
argument_spec=dict(
9688
ec2_url=dict(),
9789
region=dict(aliases=['aws_region', 'ec2_region'],
@@ -108,35 +100,34 @@ def main():
108100
region = module.params.get('region')
109101
ec2_url = module.params.get('ec2_url')
110102

111-
# If we have a region specified, connect to its endpoint.
103+
# If region is specified, create a session with boto3 and connect to the EC2 service
112104
if region:
113105
try:
114-
ec2 = connect_to_region(region, aws_access_key_id=aws_access_key,
115-
aws_secret_access_key=aws_secret_key)
116-
except boto.exception.NoAuthHandlerFound as e:
117-
module.fail_json(msg=str(e))
118-
# If we specified an ec2_url then try connecting to it
119-
elif ec2_url:
120-
try:
121-
ec2 = boto.connect_ec2_endpoint(ec2_url, aws_access_key,
122-
aws_secret_key)
123-
except boto.exception.NoAuthHandlerFound as e:
124-
module.fail_json(msg=str(e))
106+
ec2 = boto3.resource('ec2', region_name=region, aws_access_key_id=aws_access_key, aws_secret_access_key=aws_secret_key)
107+
except Exception as e:
108+
module.fail_json(msg=f"Error connecting to AWS EC2: {str(e)}")
125109
else:
126-
module.fail_json(msg="Either region or ec2_url must be specified")
110+
module.fail_json(msg="Region must be specified")
111+
112+
# If EC2 URL is provided, it can be used for connection
113+
if ec2_url:
114+
module.fail_json(msg="ec2_url is not supported in boto3 connection. Please use region instead.")
127115

128116
instances = []
129117
instance_ids = []
130-
for res in ec2.get_all_instances(filters={'tag:' + tag: value
131-
for tag, value in six.iteritems(module.params.get('tags'))}):
132-
for inst in res.instances:
133-
if inst.state == "running":
134-
instances.append({k: v for k, v in six.iteritems(inst.__dict__)
135-
if isinstance(v, (six.string_types))})
136-
instance_ids.append(inst.id)
137-
module.exit_json(changed=False, instances=instances,
138-
instance_ids=instance_ids)
139118

119+
# Get all instances with the specified tags
120+
filters = {'tag:' + tag: value for tag, value in module.params.get('tags', {}).items()}
121+
try:
122+
instances_query = ec2.instances.filter(Filters=[{'Name': f'tag:{tag}', 'Values': [value]} for tag, value in filters.items()])
123+
for instance in instances_query:
124+
if instance.state['Name'] == 'running':
125+
instances.append({k: v for k, v in instance.__dict__.items() if isinstance(v, str)})
126+
instance_ids.append(instance.id)
127+
except Exception as e:
128+
module.fail_json(msg=f"Error querying EC2 instances: {str(e)}")
129+
130+
module.exit_json(changed=False, instances=instances, instance_ids=instance_ids)
140131

141132
# this is magic, see lib/ansible/module_common.py
142133
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>

playbooks/roles/launch_ec2/tasks/main.yml

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,27 @@
3535
when: terminate_instance == true and elb and tag_lookup.instance_ids|length == 1
3636

3737
- name: Launch ec2 instance
38-
local_action:
39-
module: ec2
40-
keypair: "{{ keypair }}"
41-
group: "{{ security_group }}"
38+
community.aws.ec2_instance:
39+
key_name: "{{ keypair }}"
40+
security_groups:
41+
- "{{ security_group }}"
4242
instance_type: "{{ instance_type }}"
4343
instance_initiated_shutdown_behavior: "{{ instance_initiated_shutdown_behavior }}"
44-
image: "{{ ami }}"
44+
image_id: "{{ ami }}"
4545
vpc_subnet_id: "{{ vpc_subnet_id }}"
46-
assign_public_ip: yes
4746
wait: true
4847
region: "{{ region }}"
49-
instance_tags: "{{ instance_tags }}"
48+
tags: "{{ instance_tags }}"
49+
network:
50+
assign_public_ip: true
5051
volumes:
5152
- device_name: /dev/sda1
52-
volume_size: "{{ root_ebs_size }}"
53-
delete_on_termination: true
54-
volume_type: "gp2"
55-
encrypted: true
56-
zone: "{{ zone }}"
57-
instance_profile_name: "{{ instance_profile_name }}"
53+
ebs:
54+
volume_size: "{{ root_ebs_size }}"
55+
delete_on_termination: true
56+
volume_type: "gp2"
57+
encrypted: true
58+
instance_role: "{{ instance_profile_name }}"
5859
user_data: "{{ user_data }}"
5960
register: ec2
6061

@@ -97,7 +98,7 @@
9798
- name: Add new instance to host group
9899
local_action:
99100
module: add_host
100-
hostname: "{{ item.public_ip }}"
101+
hostname: "{{ item.network_interfaces[0].association.public_ip }}"
101102
groups: launched
102103
with_items: "{{ ec2.instances }}"
103104

util/install/ansible-bootstrap.sh

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ fi
3333
# Bootstrapping constants
3434
#
3535
VIRTUAL_ENV_VERSION="16.7.10"
36+
VIRTUAL_ENV_VERSION_NOBLE="20.27.0"
3637
PIP_VERSION="21.2.1"
38+
PIP_VERSION_NOBLE="24.0"
3739
SETUPTOOLS_VERSION="44.1.0"
3840
VIRTUAL_ENV="/tmp/bootstrap"
3941
PYTHON_BIN="${VIRTUAL_ENV}/bin"
@@ -71,6 +73,9 @@ then
7173
elif grep -q 'Focal Fossa' /etc/os-release
7274
then
7375
SHORT_DIST="focal"
76+
elif grep -q 'Noble Numbat' /etc/os-release
77+
then
78+
SHORT_DIST="noble"
7479
else
7580
cat << EOF
7681
@@ -82,7 +87,9 @@ EOF
8287
exit 1;
8388
fi
8489

85-
if [[ "${SHORT_DIST}" == focal ]] ;then
90+
if [[ "${SHORT_DIST}" == noble ]]; then
91+
PYTHON_VERSION="3.12"
92+
elif [[ "${SHORT_DIST}" == focal ]] ;then
8693
PYTHON_VERSION="3.8"
8794
else
8895
PYTHON_VERSION="3.5"
@@ -108,7 +115,7 @@ fi
108115

109116
# Required for add-apt-repository
110117
apt-get install -y software-properties-common
111-
if [[ "${SHORT_DIST}" != trusty ]] && [[ "${SHORT_DIST}" != xenial ]] && [[ "${SHORT_DIST}" != bionic ]] && [[ "${SHORT_DIST}" != focal ]] ;then
118+
if [[ "${SHORT_DIST}" != trusty ]] && [[ "${SHORT_DIST}" != xenial ]] && [[ "${SHORT_DIST}" != bionic ]] && [[ "${SHORT_DIST}" != focal ]] && [[ "${SHORT_DIST}" != noble ]] ;then
112119
apt-get install -y python-software-properties
113120
fi
114121

@@ -117,7 +124,7 @@ add-apt-repository -y ppa:git-core/ppa
117124

118125
# For older software we need to install our own PPA
119126
# Phased out with Ubuntu 18.04 Bionic and Ubuntu 20.04 Focal
120-
if [[ "${SHORT_DIST}" != bionic ]] && [[ "${SHORT_DIST}" != focal ]] ;then
127+
if [[ "${SHORT_DIST}" != bionic ]] && [[ "${SHORT_DIST}" != focal ]] && [[ "${SHORT_DIST}" != noble ]] ;then
121128
apt-key adv --keyserver "${EDX_PPA_KEY_SERVER}" --recv-keys "${EDX_PPA_KEY_ID}"
122129
add-apt-repository -y "${EDX_PPA}"
123130
fi
@@ -133,7 +140,7 @@ fi
133140
# which may differ from what is pinned in virtualenvironments
134141
apt-get update -y
135142

136-
if [[ "${SHORT_DIST}" != focal ]] ;then
143+
if [[ "${SHORT_DIST}" != focal ]] && [[ "${SHORT_DIST}" != noble ]] ;then
137144
apt-get install -y python2.7 python2.7-dev python-pip python-apt python-jinja2 build-essential sudo git-core libmysqlclient-dev libffi-dev libssl-dev
138145
else
139146
apt-get install -y python3-pip python3-apt python3-jinja2 build-essential sudo git-core libmysqlclient-dev libffi-dev libssl-dev
@@ -143,17 +150,24 @@ apt-get install -y python${PYTHON_VERSION} python${PYTHON_VERSION}-dev python3-p
143150

144151
# We want to link pip to pip3 for Ubuntu versions that don't have python 2.7 so older scripts work there
145152
# Applies to Ubuntu 20.04 Focal
146-
if [[ "${SHORT_DIST}" != trusty ]] && [[ "${SHORT_DIST}" != xenial ]] && [[ "${SHORT_DIST}" != bionic ]] ;then
153+
if [[ "${SHORT_DIST}" != trusty ]] && [[ "${SHORT_DIST}" != xenial ]] && [[ "${SHORT_DIST}" != bionic ]] && [[ "${SHORT_DIST}" != noble ]] ;then
147154
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 1
148155
sudo update-alternatives --install /usr/bin/pip pip /usr/bin/pip3 1
149156
fi
150157

158+
if [[ "${SHORT_DIST}" == noble ]] ;then
159+
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.12 1
160+
sudo update-alternatives --install /usr/bin/pip pip /usr/bin/pip3 1
161+
fi
162+
163+
if [[ "${SHORT_DIST}" != noble ]] ;then
151164
python${PYTHON_VERSION} -m pip install --upgrade pip=="${PIP_VERSION}"
152165

153166
# pip moves to /usr/local/bin when upgraded
154167
PATH=/usr/local/bin:${PATH}
155168
python${PYTHON_VERSION} -m pip install setuptools=="${SETUPTOOLS_VERSION}"
156169
python${PYTHON_VERSION} -m pip install virtualenv=="${VIRTUAL_ENV_VERSION}"
170+
fi
157171

158172
if [[ "true" == "${RUN_ANSIBLE}" ]]; then
159173
# create a new virtual env
@@ -166,7 +180,11 @@ if [[ "true" == "${RUN_ANSIBLE}" ]]; then
166180
git clone ${CONFIGURATION_REPO} ${CONFIGURATION_DIR}
167181
cd ${CONFIGURATION_DIR}
168182
git checkout ${CONFIGURATION_VERSION}
183+
if [[ "${SHORT_DIST}" != noble ]] ;then
184+
make requirements3_12
185+
else
169186
make requirements
187+
fi
170188

171189
cd "${CONFIGURATION_DIR}"/playbooks
172190
"${PYTHON_BIN}"/ansible-playbook edx_ansible.yml -i '127.0.0.1,' -c local -e "CONFIGURATION_VERSION=${CONFIGURATION_VERSION}"

util/jenkins/ansible-provision.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,10 @@ if [[ -z $ami ]]; then
172172
ami="ami-089b5711e63812c2a"
173173
# Ansible will always use Python3 interpreter on Ubuntu 20.04 hosts to execute modules
174174
extra_var_arg+=' -e ansible_python_interpreter=auto'
175-
fi
176175
elif [[ $server_type == "ubuntu_24.04" ]]; then
177176
ami="ami-04b4f1a9cf54c11d0"
178-
# Ansible will always use Python3 interpreter on Ubuntu 24.04 hosts to execute modules
179-
extra_var_arg+=' -e ansible_python_interpreter=auto'
177+
# Ansible will always use Python3.12 interpreter on Ubuntu 24.04 hosts to execute modules
178+
extra_var_arg+=' -e ansible_python_interpreter=/usr/bin/python3.12'
180179
fi
181180
fi
182181

0 commit comments

Comments
 (0)