Last active
February 18, 2018 16:23
-
-
Save merevu/94d03da04337c0edb205c768b2210cc4 to your computer and use it in GitHub Desktop.
AWS EC2 인스턴스에 Ansible 사용 예제
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# ansible 설치 | |
apt-get update && apt-get install ansible -y | |
# aws pem을 ssh agent의 key로 등록 | |
ssh-agent bash | |
ssh-add {aws_ssh_key}.pem | |
# ubuntu에 ansible ping | |
ansible all -m ping -u ubuntu | |
# ubuntu에 pip 설치 | |
ansible all -u ubuntu --sudo -a 'apt-get install python-pip -y' | |
# ubuntu에 docker 설치 | |
ansible all -u ubuntu --sudo -m apt -a "name=docker.io state=latest" | |
# playbook을 이용하여 ubuntu에 apache2 설치 | |
cat > apache-ubuntu.yaml <<EOF | |
--- | |
- hosts: mcp | |
vars: | |
http_port: 80 | |
max_clients: 200 | |
remote_user: ubuntu | |
become: yes | |
become_user: root | |
tasks: | |
- name: ensure apache is at the latest version | |
apt: name=apache2 state=latest | |
- name: ensure apache is running (and enable it at boot) | |
service: name=apache2 state=started enabled=yes | |
handlers: | |
- name: restart apache | |
service: name=apache2 state=restarted | |
EOF | |
ansible-playbook apache-ubuntu.yaml | |
# playbook을 이용하여 aws ec2 생성 | |
cat > ec2-provision.yaml <<EOF | |
- hosts: localhost | |
connection: local | |
gather_facts: False | |
tasks: | |
- name: Provision a set of instances | |
ec2: | |
aws_access_key: '{{aws_access_key}}' | |
aws_secret_key: '{{aws_secret_key}}' | |
key_name: '{{key_name}}' | |
group: default | |
region: ap-northeast-2 | |
instance_type: t2.micro | |
image: "ami-3066c15e" | |
wait: true | |
exact_count: 1 | |
count_tag: | |
Name: '{{tag_name}}' | |
instance_tags: | |
Name: '{{tag_name}}' | |
vpc_subnet_id: '{{subnet_id}}' | |
assign_public_ip: yes | |
register: ec2 | |
EOF | |
ansible-playbook -v ec2-provision.yaml -e "aws_access_key=xxxxx aws_secret_key=xxxx key_name=xxxx tag_name=xxxx subnet_id=xxxx" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment