[local] localhost
- hosts: localhost connection: local gather_facts: False vars: hostname: Windows ec2_access_key: "Secret" ec2_secret_key: "Secret_key” instance_type: "t2.micro" image: "ami-xxxxxxxx" group: "launch-wizard-1" region: "us-west-2" tasks: - name: make one instance ec2: image={{ image }} instance_type={{ instance_type }} aws_access_key={{ ec2_access_key }} aws_secret_key={{ ec2_secret_key }} instance_tags='{ "Name":"{{ hostname }}" }' region={{ region }} group={{ group }} wait=true
- hosts: localhost connection: local gather_facts: False vars: hostname: Nginx_nodejs ec2_access_key: “Secret" ec2_secret_key: "Secret_key" keypair: "aws_ansible" instance_type: "t2.micro" image: "ami-33db9803" group: "launch-wizard-1" region: "us-west-2" tasks: - name: make one instance ec2: image={{ image }} instance_type={{ instance_type }} aws_access_key={{ ec2_access_key }} aws_secret_key={{ ec2_secret_key }} keypair={{ keypair }} instance_tags='{ "Name":"{{ hostname }}" , "Group":"nginx_backend" }' region={{ region }} group={{ group }} wait=true register: ec2_info - debug: var=ec2_info - debug: var=item with_items: ec2_info.instance_ids - add_host: hostname={{ item.public_ip }} groupname=ec2hosts with_items: ec2_info.instances - name: wait for instances to listen on port:22 wait_for: state=started host={{ item.public_dns_name }} port=22 with_items: ec2_info.instances - hosts: ec2hosts gather_facts: True user: ubuntu sudo: True vars: connections : "4096" tasks: - include: nginx/tasks/setup.yml handlers: - name: restart nginx action: service name=nginx state=restarted - hosts: ec2hosts gather_facts: True user: ubuntu sudo: True tasks: - include: nodejs/tasks/setup.yml
#!/usr/bin/env python import sys, os from commands import * group = '"tag_Group_nginx_backend": [' template = "/etc/ansible/playbooks/nginx/templates/balance.conf.j2" list_ip = [] #Create ec2_list data = getoutput("/etc/ansible/ec2.py --refresh-cache") flag = 0 for line in data.split("\n"): if flag: if line.strip() != "],": list_ip.append(line.strip().strip(",").strip("\"")) else: break if line.strip() == group: flag = 1 f = open(template, 'w') f.write('''# upstream list upstream backend {''') f.close() for ip in list_ip: f = open(template, 'a') f.write(''' server '''+ip+''':80 weight=3 fail_timeout=15s;''') f.close() f = open(template, 'a') f.write(''' }''') f.close()
- replace: dest={{ redis_master_ip }} regexp='^(\s+)(master\:)\s(.*)$' replace='\1\2 {{ item.public_ip }}' with_items: ec2_info.instances
redis_master_ip: "/etc/ansible/playbooks/redis/files/master_ip.yml"
master: 1.2.3.4
- name: Get master IP include_vars: "{{ redis_master_ip }}"
- hosts: localhost connection: local tasks: - name: nginx ec2 group local_action: module: ec2_group name: nginx description: an nginx EC2 group region: us-west-2 aws_secret_key: "Secret" aws_access_key: "Secret" rules: - proto: tcp from_port: 80 to_port: 80 cidr_ip: 192.168.0.0/24 - proto: tcp from_port: 22 to_port: 22 cidr_ip: 0.0.0.0/0 rules_egress: - proto: all cidr_ip: 0.0.0.0/0
- hosts: localhost connection: local gather_facts: False vars: sqs_access_key: “Secret" sqs_secret_key: "Secret" region: "us-west-2” tasks: - name: launch some aws services cloudformation: > stack_name="TEST" region={{ region }} template=files/cloudformation.json <\code> template: <code> { "AWSTemplateFormatVersion" : "2010-09-09", "Description" : "AWS CloudFormation SQS” "Resources" : { "MyQueue" : { "Type" : "AWS::SQS::Queue" } }, "Outputs" : { "QueueURL" : { "Description" : "URL of newly created SQS Queue", "Value" : { "Ref" : "MyQueue" } }, "QueueARN" : { "Description" : "ARN of newly created SQS Queue", "Value" : { "Fn::GetAtt" : ["MyQueue", "Arn"]} } } }
Source: https://habr.com/ru/post/242083/
All Articles