Skip to content

Commit 18d8ac1

Browse files
authored
Merge pull request #7 from cycloidio/develop
Merge develop on master
2 parents 025288a + 0758236 commit 18d8ac1

File tree

4 files changed

+91
-21
lines changed

4 files changed

+91
-21
lines changed

README.md

+26-9
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,27 @@ This script use env vars configuration to run ansible playbook with ssh proxy on
2727
* `(ANSIBLE_PLAYBOOK_PATH)`: Path of the ansible playbook to run. Default: `ansible-playbook`.
2828
* `(DEBUG)`: Run in debug mode
2929

30-
ec2.py vars:
30+
ec2 vars:
3131
* `(AWS_INVENTORY)`: If the Amazon EC2 dynamic inventory need to be used or no, can be eiter `true`, `false` or `auto`. `auto` checks if `AWS_ACCESS_KEY_ID` is set or not. Default: `auto`.
3232
* `(AWS_ACCESS_KEY_ID)`: Used by Amazon EC2 dynamic inventory
3333
* `(AWS_SECRET_ACCESS_KEY)`: Used by Amazon EC2 dynamic inventory
3434
* `(EC2_VPC_DESTINATION_VARIABLE)`: Can be either `ip_address` for public ip address or `private_ip_address`, see [ec2.ini](https://github.com/ansible/ansible/blob/devel/contrib/inventory/ec2.ini). Default: `private_ip_address`.
3535

36-
azure_rm.py vars:
36+
azure_rm vars:
3737
* `(AZURE_INVENTORY)`: If the Azure dynamic inventory need to be used or no, can be eiter `true`, `false` or `auto`. `auto` checks if `AZURE_SUBSCRIPTION_ID` is set or not. Default: `auto`.
3838
* `(AZURE_SUBSCRIPTION_ID)`: Used by Azure dynamic inventory
3939
* `(AZURE_TENANT_ID)`: Used by Azure dynamic inventory
4040
* `(AZURE_CLIENT_ID)`: Used by Azure dynamic inventory
4141
* `(AZURE_SECRET)`: Used by Azure dynamic inventory
4242
* `(AZURE_USE_PRIVATE_IP)`: Can be either `True` or `False`, see [azure_rm.py](https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/azure_rm.py). Default: `True`.
43+
* `(ANSIBLE_PLUGIN_AZURE_PLAIN_HOST_NAMES)`: By default this plugin will use globally unique host names. This option allows you to override that, and use the name that matches the old inventory script naming.. Default: `False`.
44+
note: Ansible `azure_rm` plugin is used for ansible `>= 2.8` else `azure_rm.py` script will be used
4345

4446
Example of pipeline configuration :
4547

4648
**YAML anchors**
4749

48-
```
50+
```YAML
4951
shared:
5052
- &run-ansible-from-bastion
5153
config:
@@ -66,7 +68,7 @@ shared:
6668
6769
**usage**
6870
69-
```
71+
```YAML
7072
- task: run-ansible
7173
<<: *run-ansible-from-bastion
7274
params:
@@ -98,7 +100,7 @@ Example of pipeline configuration :
98100
99101
**YAML anchors**
100102
101-
```
103+
```YAML
102104
shared:
103105
- &aws-ami-cleaner
104106
task: aws-ami-cleaner
@@ -136,7 +138,7 @@ Example of pipeline configuration :
136138

137139
**YAML anchors**
138140

139-
```
141+
```YAML
140142
shared:
141143
- &aws-ecr-cleaner
142144
task: aws-ecr-cleaner
@@ -178,7 +180,7 @@ Example of pipeline configuration :
178180

179181
**YAML anchors**
180182

181-
```
183+
```YAML
182184
shared:
183185
- &vault-approle-login
184186
task: vault-approle-login
@@ -215,7 +217,7 @@ This script use env vars configuration to merge stack and config for Cycloid.io.
215217

216218
**YAML anchors**
217219

218-
```
220+
```YAML
219221
shared:
220222
- &merge-stack-and-config
221223
platform: linux
@@ -234,7 +236,7 @@ shared:
234236

235237
**usage**
236238

237-
```
239+
```YAML
238240
- task: merge-stack-and-config
239241
config:
240242
<<: *merge-stack-and-config
@@ -252,6 +254,21 @@ shared:
252254
```
253255
254256
257+
# Build and test a local image
258+
259+
```bash
260+
export IMAGE_NAME="cycloid/cycloid-toolkit:develop"
261+
export PYTHON_VERSION=3
262+
export ANSIBLE_VERSION=2.8.*
263+
docker build -t $IMAGE_NAME --build-arg=PYTHON_VERSION="$PYTHON_VERSION" --build-arg=ANSIBLE_VERSION="$ANSIBLE_VERSION" .
264+
265+
virtualenv -p python3 --clear .env
266+
source .env/bin/activate
267+
pip install unittest2 docker
268+
python tests.py -v
269+
```
270+
271+
255272
# Push new image tag
256273

257274
Tags are currently based on ansible version installed in the docker image.

files/ansible/hosts-template/default.azure_rm.yml.template

+3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ hostvar_expressions:
4848
# if none are found, the first public IP address.
4949
ansible_host: $ANSIBLE_PLUGIN_AZURE_HOST
5050

51+
# By default this plugin will use globally unique host names. This option allows you to override that, and use the name that matches the old inventory script naming.
52+
plain_host_names: $ANSIBLE_PLUGIN_AZURE_PLAIN_HOST_NAMES
53+
5154
# places hosts in dynamically-created groups based on a variable value.
5255
keyed_groups:
5356
# places each host in a group named 'tag_(tag name)_(tag value)' for each tag on a VM.

scripts/ansible-runner

+18-7
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,21 @@ usage()
2727
echo ' * `(ANSIBLE_PLAYBOOK_PATH)`: Path of the ansible playbook to run. Default: `ansible-playbook`.'
2828
echo ' * `(DEBUG)`: Run in debug mode'
2929
echo ''
30-
echo 'ec2.py vars:'
30+
echo 'ec2 vars:'
3131
echo ' * `(AWS_INVENTORY)`: If the Amazon EC2 dynamic inventory need to be used or no, can be eiter `true`, `false` or `auto`. `auto` checks if `AWS_ACCESS_KEY_ID` is set or not. Default: `auto`.'
3232
echo ' * `(AWS_ACCESS_KEY_ID)`: Used by Amazon EC2 dynamic inventory'
3333
echo ' * `(AWS_SECRET_ACCESS_KEY)`: Used by Amazon EC2 dynamic inventory'
3434
echo ' * `(EC2_VPC_DESTINATION_VARIABLE)`: Can be either `ip_address` for public ip address or `private_ip_address`, see [ec2.ini](https://github.com/ansible/ansible/blob/devel/contrib/inventory/ec2.ini). Default: `private_ip_address`.'
3535
echo ''
36-
echo 'azure_rm.py vars:'
36+
echo 'azure_rm vars:'
3737
echo ' * `(AZURE_INVENTORY)`: If the Azure dynamic inventory need to be used or no, can be eiter `true`, `false` or `auto`. `auto` checks if `AZURE_SUBSCRIPTION_ID` is set or not. Default: `auto`.'
3838
echo ' * `(AZURE_SUBSCRIPTION_ID)`: Used by Azure dynamic inventory'
3939
echo ' * `(AZURE_TENANT_ID)`: Used by Azure dynamic inventory'
4040
echo ' * `(AZURE_CLIENT_ID)`: Used by Azure dynamic inventory'
4141
echo ' * `(AZURE_SECRET)`: Used by Azure dynamic inventory'
4242
echo ' * `(AZURE_USE_PRIVATE_IP)`: Can be either `True` or `False`, see [azure_rm.py](https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/azure_rm.py). Default: `True`.'
43+
echo ' * `(ANSIBLE_PLUGIN_AZURE_PLAIN_HOST_NAMES)`: By default this plugin will use globally unique host names. This option allows you to override that, and use the name that matches the old inventory script naming.. Default: `False`.'
44+
echo ' note: Ansible `azure_rm` plugin is used for ansible `>= 2.8` else `azure_rm.py` script will be used'
4345
echo ''
4446
exit 1
4547
}
@@ -66,12 +68,14 @@ export AWS_DEFAULT_REGION="${AWS_DEFAULT_REGION:-eu-west-1}"
6668
export EC2_VPC_DESTINATION_VARIABLE="${EC2_VPC_DESTINATION_VARIABLE:-private_ip_address}"
6769

6870
# Default envvars for azure_rm.py
71+
export DEFAULT_ANSIBLE_PLUGIN_AZURE_HOST="${DEFAULT_ANSIBLE_PLUGIN_AZURE_HOST:-"(public_dns_hostnames + public_ipv4_addresses + private_ipv4_addresses) | first"}"
72+
export DEFAULT_ANSIBLE_PLUGIN_AZURE_HOST_PRIVATE="${DEFAULT_ANSIBLE_PLUGIN_AZURE_HOST_PRIVATE:-"(private_ipv4_addresses + public_dns_hostnames + public_ipv4_addresses) | first"}"
6973
export AZURE_INVENTORY="${AZURE_INVENTORY:-auto}"
74+
# Make sure args work for Ansible azure rm and https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/azure_rm.py
75+
export AZURE_TENANT="${AZURE_TENANT:-$AZURE_TENANT_ID}"
7076
export AZURE_USE_PRIVATE_IP="${AZURE_USE_PRIVATE_IP:-True}"
71-
export ANSIBLE_PLUGIN_AZURE_HOST="(public_dns_hostnames + public_ipv4_addresses) | first"
72-
if [ "${AZURE_USE_PRIVATE_IP,,}" == "true" ]; then
73-
export ANSIBLE_PLUGIN_AZURE_HOST="private_ipv4_addresses | first"
74-
fi
77+
export ANSIBLE_PLUGIN_AZURE_PLAIN_HOST_NAMES="${ANSIBLE_PLUGIN_AZURE_PLAIN_HOST_NAMES:-False}"
78+
export ANSIBLE_PLUGIN_AZURE_HOST="${ANSIBLE_PLUGIN_AZURE_HOST:-""}"
7579

7680
# Keep compatibility with old naming of ssh key
7781
export SSH_PRIVATE_KEY="${SSH_PRIVATE_KEY:-$BASTION_PRIVATE_KEY}"
@@ -99,8 +103,15 @@ if [ "$AWS_INVENTORY" == "auto" ] && [ -n "$AWS_ACCESS_KEY_ID" ] || [ "${AWS_INV
99103
cp /etc/ansible/hosts-template/ec2.py /etc/ansible/hosts/
100104
EXTRA_ANSIBLE_ARGS="${EXTRA_ANSIBLE_ARGS} -i /etc/ansible/hosts/ec2.py"
101105
fi
106+
if [ -z "${ANSIBLE_PLUGIN_AZURE_HOST}" ]; then
107+
if [ "${AZURE_USE_PRIVATE_IP,,}" == "true" ]; then
108+
export ANSIBLE_PLUGIN_AZURE_HOST="${DEFAULT_ANSIBLE_PLUGIN_AZURE_HOST_PRIVATE}"
109+
else
110+
export ANSIBLE_PLUGIN_AZURE_HOST="${DEFAULT_ANSIBLE_PLUGIN_AZURE_HOST}"
111+
fi
112+
fi
102113
if [ "$AZURE_INVENTORY" == "auto" ] && [ -n "$AZURE_SUBSCRIPTION_ID" ] || [ "${AZURE_INVENTORY,,}" == "true" ]; then
103-
if (( $(echo "${ANSIBLE_VERSION%.*} >= 2.7" |bc -l) )); then
114+
if (( $(echo "${ANSIBLE_VERSION%.*} >= 2.8" |bc -l) )); then
104115
# Render default.azure_rm.yml template from envvars
105116
envsubst < /etc/ansible/hosts-template/default.azure_rm.yml.template > /etc/ansible/hosts/default.azure_rm.yml
106117
EXTRA_ANSIBLE_ARGS="${EXTRA_ANSIBLE_ARGS} -i /etc/ansible/hosts/default.azure_rm.yml"

tests.py

+44-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# virtualenv -p python3 --clear .env
2020
# source .env/bin/activate
2121
# pip install unittest2 docker
22-
#
22+
#
2323
# python tests.py
2424

2525
# Debug print command that you can use
@@ -314,7 +314,7 @@ def test_basic(self):
314314

315315
r = self.drun(cmd="cat playbook/.vault-password")
316316
self.assertTrue(self.output_contains(r.output, '.*password'))
317-
317+
318318

319319
def test_extra_args(self):
320320
environment={
@@ -412,7 +412,7 @@ def test_azure_hosts_inventory(self):
412412
'AZURE_SUBSCRIPTION_ID': 'foo',
413413
}
414414
r = self.drun(cmd="/usr/bin/ansible-runner", environment=environment)
415-
if float(self.ansible_version) >= 2.7:
415+
if float(self.ansible_version) >= 2.8:
416416
self.assertTrue(self.output_contains(r.output, '.*ansible-playbook.*-i /etc/ansible/hosts/default.azure_rm.yml'))
417417
else:
418418
self.assertTrue(self.output_contains(r.output, '.*ansible-playbook.*-i /etc/ansible/hosts/azure_rm.py'))
@@ -421,14 +421,52 @@ def test_azure_hosts_inventory(self):
421421
# Azure dynamic inventory should be used as AZURE_INVENTORY=true even if AZURE_SUBSCRIPTION_ID is not present
422422
environment={
423423
'AZURE_INVENTORY': 'true',
424+
'ANSIBLE_PLUGIN_AZURE_PLAIN_HOST_NAMES': 'true',
424425
}
425426
r = self.drun(cmd="/usr/bin/ansible-runner", environment=environment)
426-
if float(self.ansible_version) >= 2.7:
427+
if float(self.ansible_version) >= 2.8:
427428
self.assertTrue(self.output_contains(r.output, '.*ansible-playbook.*-i /etc/ansible/hosts/default.azure_rm.yml'))
429+
r = self.drun(cmd="cat /etc/ansible/hosts/default.azure_rm.yml")
430+
self.assertTrue(self.output_contains(r.output, '^plain_host_names:.*true'))
431+
# default ANSIBLE_PLUGIN_AZURE_HOST
432+
self.assertTrue(self.output_contains(r.output, '^\s*ansible_host:.*private_ipv4_addresses \+ public_dns_hostnames \+ public_ipv4_addresses'))
428433
else:
429434
self.assertTrue(self.output_contains(r.output, '.*ansible-playbook.*-i /etc/ansible/hosts/azure_rm.py'))
430435
self.assertEquals(r.exit_code, 0)
431436

437+
# Azure dynamic inventory configuration
438+
if float(self.ansible_version) >= 2.8:
439+
# Override ANSIBLE_PLUGIN_AZURE_HOST
440+
environment={
441+
'AZURE_INVENTORY': 'true',
442+
'AZURE_USE_PRIVATE_IP': 'True',
443+
'ANSIBLE_PLUGIN_AZURE_HOST': 'foo',
444+
'DEFAULT_ANSIBLE_PLUGIN_AZURE_HOST_PRIVATE': 'bar',
445+
'DEFAULT_ANSIBLE_PLUGIN_AZURE_HOST': 'bli',
446+
}
447+
self.drun(cmd="/usr/bin/ansible-runner", environment=environment)
448+
r = self.drun(cmd="cat /etc/ansible/hosts/default.azure_rm.yml")
449+
self.assertTrue(self.output_contains(r.output, '^\s*ansible_host:.*foo'))
450+
451+
# AZURE_USE_PRIVATE_IP true
452+
environment={
453+
'AZURE_INVENTORY': 'true',
454+
'AZURE_USE_PRIVATE_IP': 'True',
455+
'DEFAULT_ANSIBLE_PLUGIN_AZURE_HOST_PRIVATE': 'bar',
456+
'DEFAULT_ANSIBLE_PLUGIN_AZURE_HOST': 'bli',
457+
}
458+
self.drun(cmd="/usr/bin/ansible-runner", environment=environment)
459+
r = self.drun(cmd="cat /etc/ansible/hosts/default.azure_rm.yml")
460+
self.assertTrue(self.output_contains(r.output, '^\s*ansible_host:.*bar'))
461+
462+
# AZURE_USE_PRIVATE_IP false
463+
environment={
464+
'AZURE_INVENTORY': 'true',
465+
}
466+
self.drun(cmd="/usr/bin/ansible-runner", environment=environment)
467+
r = self.drun(cmd="cat /etc/ansible/hosts/default.azure_rm.yml")
468+
self.assertTrue(self.output_contains(r.output, '^\s*ansible_host:.*bli'))
469+
432470
# Azure dynamic inventory should not be used as AZURE_INVENTORY=false even if AZURE_SUBSCRIPTION_ID is present
433471
environment={
434472
'AZURE_INVENTORY': 'false',
@@ -447,11 +485,12 @@ def test_ec2_and_azure_hosts_inventory(self):
447485
'AWS_ACCESS_KEY_ID': 'bar',
448486
}
449487
r = self.drun(cmd="/usr/bin/ansible-runner", environment=environment)
450-
if float(self.ansible_version) >= 2.7:
488+
if float(self.ansible_version) >= 2.8:
451489
self.assertTrue(self.output_contains(r.output, '.*ansible-playbook.*-i /etc/ansible/hosts/ec2.py.*-i /etc/ansible/hosts/default.azure_rm.yml'))
452490
else:
453491
self.assertTrue(self.output_contains(r.output, '.*ansible-playbook.*-i /etc/ansible/hosts/ec2.py.*-i /etc/ansible/hosts/azure_rm.py'))
454492
self.assertEquals(r.exit_code, 0)
455493

494+
456495
if __name__ == '__main__':
457496
unittest.main()

0 commit comments

Comments
 (0)