Debian Codegraph @

ansible_collections/amazon/aws/tests/integration/targets/ec2_metadata_facts/playbooks/setup.yml

raw
1 ---
2 - name: Prepare environment for the ec2_metadata_facts tests
3 module_defaults:
4 group/aws:
5 access_key: "{{ aws_access_key }}"
6 secret_key: "{{ aws_secret_key }}"
7 session_token: "{{ security_token | default(omit) }}"
8 region: "{{ aws_region }}"
9
10 hosts: localhost
11
12 vars:
13 vpc_name: "{{ resource_prefix }}-vpc"
14 vpc_seed: "{{ resource_prefix }}"
15 vpc_cidr: 10.{{ 256 | random(seed=vpc_seed) }}.0.0/16
16 subnet_cidr: 10.{{ 256 | random(seed=vpc_seed) }}.32.0/24
17
18 tasks:
19 - ansible.builtin.set_fact:
20 # As lookup plugins don't have access to module_defaults
21 connection_args:
22 region: "{{ aws_region }}"
23 access_key: "{{ aws_access_key }}"
24 secret_key: "{{ aws_secret_key }}"
25 session_token: "{{ security_token | default(omit) }}"
26
27 - ansible.builtin.include_role:
28 name: setup_sshkey
29
30 - ansible.builtin.include_role:
31 name: setup_ec2_facts
32
33 - ansible.builtin.set_fact:
34 availability_zone: "{{ ec2_availability_zone_names[0] }}"
35
36 # ============================================================
37 - name: Create a VPC
38 amazon.aws.ec2_vpc_net:
39 name: "{{ resource_prefix }}-vpc"
40 state: present
41 cidr_block: "{{ vpc_cidr }}"
42 tags:
43 Name: "{{ resource_prefix }}-vpc"
44 Description: Created by ansible-test
45 register: vpc_result
46
47 - ansible.builtin.set_fact:
48 vpc_id: "{{ vpc_result.vpc.id }}"
49
50 - name: Create an internet gateway
51 amazon.aws.ec2_vpc_igw:
52 vpc_id: "{{ vpc_id }}"
53 state: present
54 tags:
55 Name: "{{ resource_prefix }}"
56 register: igw_result
57
58 - name: Create a subnet
59 amazon.aws.ec2_vpc_subnet:
60 cidr: "{{ vpc_cidr }}"
61 az: "{{ availability_zone }}"
62 vpc_id: "{{ vpc_id }}"
63 tags:
64 Name: "{{ resource_prefix }}-vpc"
65 Description: Created by ansible-test
66 state: present
67 register: vpc_subnet_result
68
69 - name: Create a public route table
70 amazon.aws.ec2_vpc_route_table:
71 vpc_id: "{{ vpc_id }}"
72 tags:
73 Name: "{{ resource_prefix }}"
74 subnets:
75 - "{{ vpc_subnet_result.subnet.id }}"
76 routes:
77 - dest: "0.0.0.0/0"
78 gateway_id: "{{ igw_result.gateway_id }}"
79 register: public_route_table
80
81 - name: Create a security group
82 amazon.aws.ec2_security_group:
83 name: "{{ resource_prefix }}-sg"
84 description: Created by {{ resource_prefix }}
85 rules:
86 - proto: tcp
87 ports: 22
88 cidr_ip: "0.0.0.0/0"
89 - proto: icmp
90 from_port: -1
91 to_port: -1
92 cidr_ip: "0.0.0.0/0"
93 state: present
94 vpc_id: "{{ vpc_result.vpc.id }}"
95 register: vpc_sg_result
96
97 - name: Create a key
98 amazon.aws.ec2_key:
99 name: "{{ resource_prefix }}"
100 key_material: "{{ key_material }}"
101 state: present
102 register: ec2_key_result
103
104 - amazon.aws.ec2_key_info:
105 filters:
106 fingerprint: "{{ fingerprint }}"
107 register: key_info
108 until: key_info.keypairs | length == 1
109 retries: 5
110 delay: 10
111
112 - name: Set facts to simplify use of extra resources
113 ansible.builtin.set_fact:
114 vpc_subnet_id: "{{ vpc_subnet_result.subnet.id }}"
115 vpc_sg_id: "{{ vpc_sg_result.group_id }}"
116 vpc_igw_id: "{{ igw_result.gateway_id }}"
117 vpc_route_table_id: "{{ public_route_table.route_table.id }}"
118 ec2_key_name: "{{ ec2_key_result.key.name }}"
119
120 - name: Create an instance to test with
121 amazon.aws.ec2_instance:
122 state: running
123 name: "{{ resource_prefix }}-ec2-metadata-facts"
124 image_id: "{{ ec2_ami_id }}"
125 vpc_subnet_id: "{{ vpc_subnet_id }}"
126 security_group: "{{ vpc_sg_id }}"
127 instance_type: t2.micro
128 key_name: "{{ ec2_key_name }}"
129 network_interfaces:
130 - assign_public_ip: true
131 delete_on_termination: true
132 metadata_options:
133 instance_metadata_tags: enabled
134 tags:
135 snake_case_key: a_snake_case_value
136 camelCaseKey: aCamelCaseValue
137 wait: true
138 register: ec2_instance
139
140 - ansible.builtin.set_fact:
141 ec2_ami_id_py2: "{{ lookup('aws_ssm', '/aws/service/ami-amazon-linux-latest/amzn-ami-hvm-x86_64-gp2', **connection_args) }}"
142 ec2_ami_ssh_user_py2: ec2-user
143
144 - name: Create an instance to test with using Python 2
145 amazon.aws.ec2_instance:
146 state: running
147 name: "{{ resource_prefix }}-ec2-metadata-facts-py2"
148 image_id: "{{ ec2_ami_id_py2 }}"
149 vpc_subnet_id: "{{ vpc_subnet_id }}"
150 security_group: "{{ vpc_sg_id }}"
151 instance_type: t2.micro
152 key_name: "{{ ec2_key_name }}"
153 network_interfaces:
154 - assign_public_ip: true
155 delete_on_termination: true
156 metadata_options:
157 instance_metadata_tags: enabled
158 tags:
159 snake_case_key: a_snake_case_value
160 camelCaseKey: aCamelCaseValue
161 wait: true
162 register: ec2_instance_py2
163
164 - ansible.builtin.set_fact:
165 ec2_instance_id: "{{ ec2_instance.instances[0].instance_id }}"
166 ec2_instance_id_py2: "{{ ec2_instance_py2.instances[0].instance_id }}"
167
168 - name: Create inventory file
169 ansible.builtin.template:
170 src: inventory.j2
171 dest: ../inventory
172 mode: "0644"
173
174 - ansible.builtin.wait_for:
175 port: 22
176 host: "{{ ec2_instance.instances[0].public_ip_address }}"
177 timeout: 1200
178
179 - ansible.builtin.wait_for:
180 port: 22
181 host: "{{ ec2_instance_py2.instances[0].public_ip_address }}"
182 timeout: 1200