Skip to content

Commit a7c7b4f

Browse files
committed
add integration tests for the netcat feature
1 parent e546ce9 commit a7c7b4f

File tree

10 files changed

+119
-31
lines changed

10 files changed

+119
-31
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
time=8m
2+
3+
cloud/aws
4+
connection_aws_ssm
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
- hosts: localhost
2+
roles:
3+
- role: ../setup_connection_aws_ssm
4+
vars:
5+
target_os: fedora
6+
use_s3_bucket: false
7+
host_port_number: 50547
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
- hosts: localhost
2+
tasks:
3+
- include_role:
4+
name: ../setup_connection_aws_ssm
5+
tasks_from: cleanup.yml
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dependencies:
2+
- connection
3+
- setup_connection_aws_ssm
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env bash
2+
3+
PLAYBOOK_DIR=$(pwd)
4+
set -eux
5+
6+
CMD_ARGS=("$@")
7+
8+
# Destroy Environment
9+
cleanup() {
10+
11+
cd "${PLAYBOOK_DIR}"
12+
ansible-playbook aws_ssm_integration_test_teardown.yml "${CMD_ARGS[@]}"
13+
14+
}
15+
16+
trap "cleanup" EXIT
17+
18+
# Setup Environment
19+
ansible-playbook aws_ssm_integration_test_setup.yml "$@"
20+
21+
# Export the AWS Keys
22+
set +x
23+
. ./aws-env-vars.sh
24+
set -x
25+
26+
# Execute Integration tests
27+
ansible-playbook test.yml -i "${PLAYBOOK_DIR}/ssm_inventory" "$@"
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
- name: Deploy web server on ec2 instance using SSM
2+
hosts: aws_ssm
3+
gather_facts: true
4+
5+
vars:
6+
server_content: |
7+
Enable SysAdmin Demo:
8+
Ansible Profiling with Callback Plugin
9+
Custom Web Page
10+
11+
tasks:
12+
- name: Install httpd package
13+
ansible.builtin.dnf:
14+
name: httpd
15+
state: present
16+
become: true
17+
18+
- name: Start and enable httpd service
19+
ansible.builtin.service:
20+
name: httpd
21+
enabled: true
22+
state: started
23+
become: true
24+
25+
- name: Create a custom index.html file
26+
ansible.builtin.copy:
27+
dest: /var/www/html/index.html
28+
content: "{{ server_content }}"
29+
become: true
30+
31+
- name: Ping Web server
32+
ansible.builtin.get_url:
33+
url: "http://localhost:80"
34+
dest: /tmp/server.txt
35+
36+
- name: Fetch file from remote host
37+
ansible.builtin.fetch:
38+
src: "/tmp/server.txt"
39+
dest: "/tmp/ansible_ssm_server.txt"
40+
flat: true
41+
42+
- name: Validate server content
43+
ansible.builtin.assert:
44+
that:
45+
- lookup('file', '/tmp/ansible_ssm_server.txt', rstrip=false) == server_content

tests/integration/targets/setup_connection_aws_ssm/defaults/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ instance_type: t3.micro
44
ami_details:
55
fedora:
66
owner: 125523088429
7-
name: 'Fedora-Cloud-Base-41-1.2.x86_64*'
7+
name: 'Fedora-Cloud-Base-*.x86_64-41-*'
88
user_data: |
99
#!/bin/sh
10-
sudo dnf install -y https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/linux_amd64/amazon-ssm-agent.rpm
10+
sudo dnf install -y https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/linux_amd64/amazon-ssm-agent.rpm nc
1111
sudo systemctl start amazon-ssm-agent
1212
os_type: linux
1313
centos:

tests/integration/targets/setup_connection_aws_ssm/tasks/main.yml

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,18 @@
1010
session_token: '{{ security_token | default(omit) }}'
1111
region: '{{ aws_region }}'
1212
block:
13-
14-
- name: get ARN of calling user
15-
amazon.aws.aws_caller_info:
16-
register: aws_caller_info
17-
1813
- name: setup connection argments fact
1914
ansible.builtin.include_tasks: 'connection_args.yml'
2015

2116
- name: Ensure IAM instance role exists
2217
amazon.aws.iam_role:
23-
name: "ansible-test-{{tiny_prefix}}-aws-ssm-role"
24-
assume_role_policy_document: "{{ lookup('file','ec2-trust-policy.json') }}"
18+
name: "ansible-test-{{ tiny_prefix }}-aws-ssm-role"
19+
assume_role_policy_document: "{{ lookup('file', 'ec2-trust-policy.json') }}"
2520
state: present
26-
create_instance_profile: yes
21+
create_instance_profile: true
2722
managed_policy:
28-
- AmazonSSMManagedInstanceCore
29-
wait: True
23+
- AmazonSSMManagedInstanceCore
24+
wait: true
3025
register: role_output
3126

3227
- name: Lookup AMI configuration
@@ -40,14 +35,15 @@
4035
name: '{{ ami_configuration.name }}'
4136
register: ec2_amis
4237
when:
43-
- ami_configuration.name | default(False)
38+
- '"name" in ami_configuration'
39+
- ami_configuration.name != ""
4440

4541
- name: AMI Lookup (SSM Parameter)
46-
when:
47-
- ami_configuration.ssm_parameter | default(False)
48-
block:
49-
- ansible.builtin.set_fact:
42+
ansible.builtin.set_fact:
5043
ssm_amis: "{{ lookup('amazon.aws.ssm_parameter', ami_configuration.ssm_parameter, **connection_args) }}"
44+
when:
45+
- '"ssm_parameter" in ami_configuration'
46+
- ami_configuration.ssm_parameter != ""
5147

5248
- name: Set facts with latest AMIs
5349
vars:
@@ -112,6 +108,7 @@
112108
name: "{{ s3_bucket_name }}"
113109
region: "{{ s3_bucket_region | default(omit)}}"
114110
register: s3_output
111+
when: use_s3_bucket | default(True) | bool
115112

116113
- name: setup encryption
117114
ansible.builtin.include_tasks: 'encryption.yml'
@@ -141,26 +138,28 @@
141138
src: ec2_instance_vars_to_delete.yml.j2
142139
ignore_errors: true
143140
when:
141+
- instance_output is defined
144142
- instance_output is successful
145143

146144
- name: Create IAM Role vars_to_delete.yml
147145
ansible.builtin.template:
148146
dest: "{{ playbook_dir }}/iam_role_vars_to_delete.yml"
149147
src: iam_role_vars_to_delete.yml.j2
150148
when:
149+
- role_output is defined
151150
- role_output is successful
152-
ignore_errors: true
153151

154152
- name: Create S3.yml
155153
ansible.builtin.template:
156154
dest: "{{ playbook_dir }}/s3_vars_to_delete.yml"
157155
src: s3_vars_to_delete.yml.j2
158156
when:
159-
- s3_output is successful
160-
ignore_errors: true
157+
- s3_output is defined
158+
- '"name" in s3_output'
161159

162160
- name: Create SSM vars_to_delete.yml
163161
ansible.builtin.template:
164162
dest: "{{ playbook_dir }}/ssm_vars_to_delete.yml"
165163
src: ssm_vars_to_delete.yml.j2
166-
ignore_errors: true
164+
when:
165+
- use_ssm_document | default(False)
Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
---
2-
- block:
3-
- name: Create custom SSM document
4-
command: "aws ssm create-document --content file://{{ role_path }}/files/ssm-document.json --name {{ ssm_document_name }} --document-type Session"
5-
environment: "{{ connection_env }}"
6-
always:
7-
- name: Create SSM vars_to_delete.yml
8-
template:
9-
dest: "{{ playbook_dir }}/ssm_vars_to_delete.yml"
10-
src: ssm_vars_to_delete.yml.j2
11-
ignore_errors: true
2+
- name: Create custom SSM document
3+
command: "aws ssm create-document --content file://{{ role_path }}/files/ssm-document.json --name {{ ssm_document_name }} --document-type Session"
4+
environment: "{{ connection_env }}"

tests/integration/targets/setup_connection_aws_ssm/templates/inventory-combined.aws_ssm.j2

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ ansible_connection=community.aws.aws_ssm
3131
ansible_aws_ssm_plugin=/usr/local/sessionmanagerplugin/bin/session-manager-plugin
3232
ansible_python_interpreter={{ os_python_path | default('/usr/bin/python3') }}
3333
local_tmp=/tmp/ansible-local-{{ tiny_prefix }}
34+
{% if use_s3_bucket | default(True) %}
3435
ansible_aws_ssm_bucket_name={{ s3_bucket_name }}
36+
{% endif %}
37+
{% if host_port_number | default(False) %}
38+
ansible_aws_ssm_host_port_number={{ host_port_number }}
39+
{% endif %}
3540
{% if s3_addressing_style | default(False) %}
3641
ansible_aws_ssm_s3_addressing_style={{ s3_addressing_style }}
3742
{% endif %}

0 commit comments

Comments
 (0)