Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
e002496f
Commit
e002496f
authored
Oct 02, 2013
by
James Laska
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add idempotency support to ec2_group
parent
5b205ae8
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
40 additions
and
10 deletions
+40
-10
library/cloud/ec2_group
+40
-10
No files found.
library/cloud/ec2_group
View file @
e002496f
...
@@ -49,6 +49,14 @@ options:
...
@@ -49,6 +49,14 @@ options:
required: false
required: false
default: null
default: null
aliases: []
aliases: []
state:
version_added: "1.4"
description:
- create or delete security group
required: false
default: 'present'
aliases: []
requirements: [ "boto" ]
requirements: [ "boto" ]
'''
'''
...
@@ -105,6 +113,7 @@ def main():
...
@@ -105,6 +113,7 @@ def main():
ec2_secret_key
=
dict
(
aliases
=
[
'EC2_SECRET_KEY'
],
no_log
=
True
),
ec2_secret_key
=
dict
(
aliases
=
[
'EC2_SECRET_KEY'
],
no_log
=
True
),
ec2_access_key
=
dict
(
aliases
=
[
'EC2_ACCESS_KEY'
]),
ec2_access_key
=
dict
(
aliases
=
[
'EC2_ACCESS_KEY'
]),
region
=
dict
(
choices
=
[
'eu-west-1'
,
'sa-east-1'
,
'us-east-1'
,
'ap-northeast-1'
,
'us-west-2'
,
'us-west-1'
,
'ap-southeast-1'
,
'ap-southeast-2'
]),
region
=
dict
(
choices
=
[
'eu-west-1'
,
'sa-east-1'
,
'us-east-1'
,
'ap-northeast-1'
,
'us-west-2'
,
'us-west-1'
,
'ap-southeast-1'
,
'ap-southeast-2'
]),
state
=
dict
(
default
=
'present'
,
choices
=
[
'present'
,
'absent'
]),
),
),
supports_check_mode
=
True
,
supports_check_mode
=
True
,
)
)
...
@@ -116,6 +125,7 @@ def main():
...
@@ -116,6 +125,7 @@ def main():
ec2_secret_key
=
module
.
params
.
get
(
'ec2_secret_key'
)
ec2_secret_key
=
module
.
params
.
get
(
'ec2_secret_key'
)
ec2_access_key
=
module
.
params
.
get
(
'ec2_access_key'
)
ec2_access_key
=
module
.
params
.
get
(
'ec2_access_key'
)
region
=
module
.
params
.
get
(
'region'
)
region
=
module
.
params
.
get
(
'region'
)
state
=
module
.
params
.
get
(
'state'
)
changed
=
False
changed
=
False
...
@@ -152,8 +162,25 @@ def main():
...
@@ -152,8 +162,25 @@ def main():
if
curGroup
.
name
==
name
and
curGroup
.
vpc_id
==
vpc_id
:
if
curGroup
.
name
==
name
and
curGroup
.
vpc_id
==
vpc_id
:
group
=
curGroup
group
=
curGroup
# if found, check the group parameters are correct
# Ensure requested group is absent
if
state
==
'absent'
:
if
group
:
'''found a match, delete it'''
try
:
group
.
delete
()
except
Exception
,
e
:
module
.
fail_json
(
msg
=
"Unable to delete security group '
%
s' -
%
s"
%
(
group
,
e
))
else
:
group
=
None
changed
=
True
else
:
'''no match found, no changes required'''
# Ensure requested group is present
elif
state
==
'present'
:
if
group
:
if
group
:
'''existing group found'''
# check the group parameters are correct
group_in_use
=
False
group_in_use
=
False
rs
=
ec2
.
get_all_instances
()
rs
=
ec2
.
get_all_instances
()
for
r
in
rs
:
for
r
in
rs
:
...
@@ -163,21 +190,22 @@ def main():
...
@@ -163,21 +190,22 @@ def main():
if
group
.
description
!=
description
:
if
group
.
description
!=
description
:
if
group_in_use
:
if
group_in_use
:
module
.
fail_json
(
msg
=
"Group description does not match, but it is in use so cannot be changed."
)
module
.
fail_json
(
msg
=
"Group description does not match, but it is in use so cannot be changed."
)
group
.
delete
()
group
=
None
# if the group doesn't exist, create it now
# if the group doesn't exist, create it now
if
not
group
:
else
:
'''no match found, create it'''
if
not
module
.
check_mode
:
if
not
module
.
check_mode
:
group
=
ec2
.
create_security_group
(
name
,
description
,
vpc_id
=
vpc_id
)
group
=
ec2
.
create_security_group
(
name
,
description
,
vpc_id
=
vpc_id
)
changed
=
True
changed
=
True
else
:
module
.
fail_json
(
msg
=
"Unsupported state requested:
%
s"
%
state
)
# create a lookup for all existing rules on the group
# create a lookup for all existing rules on the group
groupRules
=
{}
if
group
:
if
group
:
groupRules
=
{}
addRulesToLookup
(
group
.
rules
,
'in'
,
groupRules
)
addRulesToLookup
(
group
.
rules
,
'in'
,
groupRules
)
# Now, go through all the defin
ed rules and ensure they are there.
# Now, go through all provid
ed rules and ensure they are there.
if
rules
:
if
rules
:
for
rule
in
rules
:
for
rule
in
rules
:
group_id
=
None
group_id
=
None
...
@@ -194,11 +222,12 @@ def main():
...
@@ -194,11 +222,12 @@ def main():
rule
[
'from_port'
]
=
None
rule
[
'from_port'
]
=
None
rule
[
'to_port'
]
=
None
rule
[
'to_port'
]
=
None
# If rule already exists, don't later delete it
ruleId
=
"
%
s-
%
s-
%
s-
%
s-
%
s-
%
s"
%
(
'in'
,
rule
[
'proto'
],
rule
[
'from_port'
],
rule
[
'to_port'
],
group_id
,
ip
)
ruleId
=
"
%
s-
%
s-
%
s-
%
s-
%
s-
%
s"
%
(
'in'
,
rule
[
'proto'
],
rule
[
'from_port'
],
rule
[
'to_port'
],
group_id
,
ip
)
if
ruleId
in
groupRules
:
if
ruleId
in
groupRules
:
del
groupRules
[
ruleId
]
del
groupRules
[
ruleId
]
continu
e
# Otherwise, add new rul
e
else
:
grantGroup
=
None
grantGroup
=
None
if
group_id
:
if
group_id
:
grantGroup
=
groups
[
group_id
]
grantGroup
=
groups
[
group_id
]
...
@@ -217,9 +246,10 @@ def main():
...
@@ -217,9 +246,10 @@ def main():
group
.
revoke
(
rule
.
ip_protocol
,
rule
.
from_port
,
rule
.
to_port
,
grant
.
cidr_ip
,
grantGroup
)
group
.
revoke
(
rule
.
ip_protocol
,
rule
.
from_port
,
rule
.
to_port
,
grant
.
cidr_ip
,
grantGroup
)
changed
=
True
changed
=
True
if
not
group
:
if
group
:
module
.
exit_json
(
changed
=
changed
,
group_id
=
None
)
module
.
exit_json
(
changed
=
changed
,
group_id
=
group
.
id
)
module
.
exit_json
(
changed
=
changed
,
group_id
=
group
.
id
)
else
:
module
.
exit_json
(
changed
=
changed
,
group_id
=
None
)
# this is magic, see lib/ansible/module_common.py
# this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment