I'm trying to get ansible installed on an instance. I figured I could use AWS::CloudFormation::Init
to execute sudo pip install ansible
. That doesn't seem to be working, though. This is my instance resource:
ansibleInstance:
Type: 'AWS::EC2::Instance'
Metadata:
'AWS::CloudFormation::Init':
commands:
ansible:
command: "sudo pip install ansible"
test: "pip --version"
ignoreErrors: 'false'
Properties:
ImageId: ami-467ca739
KeyName: Candidate-EyMm7zuOcn
InstanceType: t2.micro
SubnetId: !Ref subnetTest
SecurityGroupIds:
- !Ref allowSSH
Tags:
- Key: Name
Value: Test
Is there a way to see what is happening when this is attempted so I can figure out where/why it is failing? Is there anything others might suggest looking at to figure this out?
EDIT1: I removed the sudo
just in case that might have been getting in the way for whatever reason (I didn't think it would, but I wanted to eliminate it all the same). That had no impact. I also verified that the aws-cli
tools are installed which was expected since it is the AWS Linux AMI
EDIT2: This is a version of the ansibleInstance resource in which I tried to execute commands via UserData
:
ansibleInstance:
Type: 'AWS::EC2::Instance'
Properties:
ImageId: ami-467ca739
KeyName: Candidate-EyMm7zuOcn
InstanceType: t2.micro
SubnetId: !Ref subnetTest
SecurityGroupIds:
- !Ref allowSSH
Tags:
- Key: Name
Value: Test
UserData:
Fn::Base64:
!Sub |
#!/bin/bash -xe
pip install ansible
I suspect there is something wrong with the formatting, but I can't make out what it is.
EDIT3: I ran cfn-init
per the suggestion jordanm. This does appear to provide the metadata to the instance and, if I then log in and manually execute cfn-init
the commands are processed:
[ec2-user@ip-192-168-1-121 ~]$ sudo /opt/aws/bin/cfn-init -v -s cfTest --resource ansibleInstance [ec2-user@ip-192-168-1-121 ~]$ ansible --version ansible 2.5.4 config file = None configured module search path = [u'/home/ec2-user/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules'] ansible python module location = /usr/local/lib/python2.7/site-packages/ansible executable location = /usr/local/bin/ansible python version = 2.7.13 (default, Jan 31 2018, 00:17:36) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]
So now my question is: why won't cfn-init
execute per the UserData
?
SOLUTION: Thanks to @jordanm I was able to get though this. My working snippet:
ansibleInstance: Type: 'AWS::EC2::Instance' Metadata: 'AWS::CloudFormation::Init': config: commands: ansible: command: "sudo pip install ansible" test: "pip --version" ignoreErrors: 'false' Properties: ImageId: ami-467ca739 KeyName: Candidate-EyMm7zuOcn InstanceType: t2.micro SubnetId: !Ref subnetTest SecurityGroupIds: - !Ref allowSSH Tags: - Key: Name Value: Test UserData: Fn::Base64: !Sub | #!/bin/bash -xe # Install Ansible from the metadata /opt/aws/bin/cfn-init -v -s ${AWS::StackName} --resource ansibleInstance