Skip to main content

AWS Security Specialty Exam Practice Test

Installing Ansible with CloudFormation and Userdata

AWS game-changing feature of supporting YAML in Cloudformation is a blessing for people who would love to see things in a "proper" format with comments :). Also it reduces the number of lines when compared to JSON.

Thanks to Graham for his basic tutorial on YAML and CloudFormation, borrowed much of his code and added ansible install. I haven't used cfn-init in the below snippet. Will try to enhance the code in future and make it more sophisticated.

Please note: I have hard-coded values of my key-pair etc, feel free to remove them. Please do leave a comment if you are running into any issues. I have used RHEL image for this tutorial.


AWSTemplateFormatVersion: "2010-09-09"

Description: A sample template

Parameters:
  FilePath:
    Description: The path of the  file.
    Type: String
    Default: /home/ec2-user/userdata

Resources:
  MyEC2Instance:
    Type: "AWS::EC2::Instance" # 1 Quotes are unnecessary here - will they always be?
    Properties:
      ImageId: ami-26ebbc5c    # 2 Quotes removed from the example - still works
      InstanceType: t2.micro
      KeyName: gk-sairam
      Tags:                    # 3 Here I switch to using single spaces
       - Key: Role             # 4 Tag list item is inline
         Value: Test Instance
       -                       # 5 Next list item is block
         Key: Owner
         Value: GK
       -                       # 5 Next list item is block
         Key: name
         Value: gk-instance-1
      BlockDeviceMappings:     # 6 Switch back to double spaces
        -
          DeviceName: /dev/sdm
          Ebs:
            VolumeType: gp2
            VolumeSize: 10
      UserData:
        Fn::Base64: !Sub |     # No more Fn::Join needed
          #!/bin/bash
          echo "Testing Userdata" > ${FilePath}
          chown ec2-user.ec2-user ${FilePath}
          yum -y update
          yum -y install vim
          yum -y install net-tools
          yum -y install wget
          mkdir /tmp/ansible
          cd /tmp/ansible
          wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
          yum -y install epel-release-latest-7.noarch.rpm
          yum -y install ansible

Comments

  1. You there, this is really good post here. Thanks for taking the time to post such valuable information. Quality content is what always gets the visitors coming. CDN

    ReplyDelete

Post a Comment

Popular posts from this blog

AWS Security Specialty Exam Practice Test

Google Cloud VPC Basics for Associate Cloud Engineer - Part 1