AWS – Ansible – CF Template Boolean Values Not Accepted

Example picked from : https://github.com/sangitaccount/AWS/blob/master/cfn-templates/AWS_AutoScale_ELB_Notification_Single.template

In AWS cloud formation template found that there is no parameter type – Boolean. Check the doc from AWS : https://aws.amazon.com/blogs/devops/using-the-new-cloudformation-parameter-types/

While passing – true or false – ansible failing.. example here shows cloud formation template with launch configuration – “AssociatePublicIpAddress” accepts true or false which is self explanatory.

PublicORPrivate: True

This is the snippet I've in cloud formation template

 "PublicORPrivate": {
 "Description": "Should we assign Public IPs to Instances",
 "Type": "String",
 "Default": "False",
 "AllowedValues": [
 "True",
 "False"
 ]
 },

 "LaunchConfiguration": {
 "Type": "AWS::AutoScaling::LaunchConfiguration",
 "Properties": {
 "ImageId": {"Ref": "AMIId"},
 "InstanceType": {"Ref": "EC2InstanceType"},
 "AssociatePublicIpAddress": {"Ref": "PublicORPrivate"},
 "SecurityGroups": [{"Ref": "InstanceSG"}],
 "UserData": {
 "Fn::Base64": {
 "Fn::Join" : [ "", [
 "#!/bin/bash -xe\n",
 "/opt/aws/bin/cfn-signal -e $? --stack ", { "Ref": "AWS::StackName" },
 " --resource AutoScalingConfiguration ",
 " --region ", { "Ref" : "AWS::Region" }, "\n"
 ] ]
 }
 }
 }
 },

aws_ansible_cf_boolean

Workaround:

Instead of passing “True” OR “False” – modified to “Yes” OR “No” and added a condition in cloud formation template.

PublicORPrivate: Public

Updated cloud formation template as below. 
Instead of True OR False lets make this Yes OR No

 "PublicORPrivate": {
 "Description": "Should we assign Public IPs to Instances",
 "Type": "String",
 "Default": "No",
 "AllowedValues": [
 "Yes",
 "No"
 ]
 },

Now I've added a condition in CF template

 "Conditions" : {
 "AssignPublicIP": {"Fn::Equals" : [{"Ref" : "PublicORPrivate"}, "Yes"]}
 },

 "LaunchConfiguration": {
 "Type": "AWS::AutoScaling::LaunchConfiguration",
 "Properties": {
 "ImageId": {"Ref": "AMIId"},
 "InstanceType": {"Ref": "EC2InstanceType"},
 "AssociatePublicIpAddress": { "Fn::If" : [
 "AssignPublicIP",
 true,
 false
 ]},
 "SecurityGroups": [{"Ref": "InstanceSG"}],
 "UserData": {
 "Fn::Base64": {
 "Fn::Join" : [ "", [
 "#!/bin/bash -xe\n",
 "/opt/aws/bin/cfn-signal -e $? --stack ", { "Ref": "AWS::StackName" },
 " --resource AutoScalingConfiguration ",
 " --region ", { "Ref" : "AWS::Region" }, "\n"
 ] ]
 }
 }
 }
 },

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s