├── README.MD ├── images └── miztiik-valaxy-run-ansible-from-amazon-ssm.png └── playbooks └── linux_deploy_httpd.yml /README.MD: -------------------------------------------------------------------------------- 1 | # Running Ansible playbook using AWS Systems Manager 2 | AWS Systems Manager lets you run Ansible Playbook on any number of servers(without Ansible Tower), without an SSH connection. We will see how to execute configuration management directives using Ansible on your instances using State Manager and Run Command, and the new `AWS-RunAnsiblePlaybook` public document. 3 | 4 | ![Fig : Serverless AWS KMS Sentry](https://raw.githubusercontent.com/miztiik/run-ansible-playbook-from-ssm/master/images/miztiik-valaxy-run-ansible-from-amazon-ssm.png) 5 | 6 | #### Follow this article in [Youtube](https://www.youtube.com/watch?v=TLiLHwQ3kao&list=PLxzKY3wu0_FKok5gI1v4g4S-g-PLaW9YD&index=34&t=0s) 7 | 8 | 1. ## Pre-Requisities 9 | 1. IAM Role - _i.e_ `ManagedInstanceRole` - _with managed permissions_ [Get Help for setting up IAM Role](https://www.youtube.com/watch?v=5g0Cuq-qKA0&list=PLxzKY3wu0_FLaF9Xzpyd9p4zRCikkD9lE&index=11) 10 | - `AmazonEC2RoleforSSM` - To allow Lambda to log events 11 | 12 | 1. ## Prepare Target Instances 13 | Lets use a RedHat 7.x Linux instances as our target instance. 14 | 1. Assign the IAM Role created in the prerequisite 15 | 1. Install SSM Agent 16 | ```sh 17 | sudo yum install -y https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/linux_amd64/amazon-ssm-agent.rpm 18 | sudo systemctl enable amazon-ssm-agent 19 | sudo systemctl start amazon-ssm-agent 20 | sudo systemctl status amazon-ssm-agent 21 | ``` 22 | 1. Install Ansible in Redhat 7 23 | ```sh 24 | sudo rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm 25 | sudo yum -y install ansible 26 | ``` 27 | 1. Tag the instance 28 | ``` 29 | TagKey:OS 30 | TagValue:RedHat 31 | ``` 32 | 1. ## Create SSM State Manager 33 | - Choose `State Manager` from the `System Manager Services` 34 | - Click on `Create Association` 35 | - Select the `AWS-RunAnsiblePlaybook` 36 | - For `Targets` Choose _Specifying tags_ 37 | - Choose appropriate `schedule`. 38 | - In the _Parameters Section_, paste the playbook YAML directly. 39 | - Define the max errors as `1`. This means that if the execution encounters 1 `error` it will stop on the remaining targets. 40 | 41 | 42 | 1. ## Testing the solution 43 | Create an adhoc run using the `Run Command` manually, check the S3 Logs for execution results. 44 | 45 | 1. ## ToDo 46 | 1. Load the playbook into CodeCommit and bring it under version control. 47 | 1. _or_ Use S3 version control for the playbook(s) to be stored. 48 | 49 | ##### References 50 | [1] - [AWS Docs - Ansible from SSM](https://aws.amazon.com/blogs/mt/running-ansible-playbooks-using-ec2-systems-manager-run-command-and-state-manager/) 51 | -------------------------------------------------------------------------------- /images/miztiik-valaxy-run-ansible-from-amazon-ssm.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/miztiik/run-ansible-playbook-from-ssm/5315f0431955ac2591f944fbad2f8916a6a263ff/images/miztiik-valaxy-run-ansible-from-amazon-ssm.png -------------------------------------------------------------------------------- /playbooks/linux_deploy_httpd.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # This playbook will install Apache Web Server with php and mysql support 3 | - name: linux_deploy_httpd 4 | hosts: all 5 | tasks: 6 | - name: Install HTTPD 7 | yum: 8 | name: "{{ item }}" 9 | state: latest 10 | loop: 11 | - httpd 12 | when: ansible_os_family == "RedHat" 13 | 14 | - name: Setting default HTTP Server page 15 | shell: echo "

welcome to Miztiik Ansible Playbook Demo

" >> /var/www/html/index.html 16 | 17 | - name: Start Apache Webserver 18 | service: 19 | name: httpd 20 | state: restarted 21 | 22 | - name: enable apache on startup and start service for redhat or centos 23 | service: name=httpd enabled=yes state=started 24 | when: ansible_os_family == "RedHat" 25 | --------------------------------------------------------------------------------