├── README.md ├── inherit.yml ├── inv_trick.yml ├── inventory └── new_group.yml /README.md: -------------------------------------------------------------------------------- 1 | See blog post 2 | 3 | `ansible-playbook -i inventory new_group.yml` 4 | 5 | `ansible-playbook -i inventory inv_trick.yml` 6 | -------------------------------------------------------------------------------- /inherit.yml: -------------------------------------------------------------------------------- 1 | # Showcase "inheriting" (assigning) multiple properties from one host to a dynamically added one 2 | # 3 | - hosts: ec2_instances 4 | connection: local 5 | gather_facts: false 6 | tasks: 7 | - name: Inherit all 8 | add_host: 9 | name: "{{ item }}" 10 | ansible_connection: 'local' 11 | image: "{{ hostvars[item].image }}" 12 | group_id: "{{ hostvars[item].group_id }}" 13 | groups: dynamic_hosts 14 | tags: "{{ hostvars[item].tags }}" 15 | with_items: play_hosts 16 | 17 | - hosts: dynamic_hosts 18 | gather_facts: false 19 | tasks: 20 | - ping: 21 | - debug: var=hostvars[inventory_hostname] 22 | -------------------------------------------------------------------------------- /inv_trick.yml: -------------------------------------------------------------------------------- 1 | # Use "template" inventory hosts to represent the wanted ec2 instances 2 | # 3 | - hosts: ec2_instances 4 | connection: local 5 | gather_facts: false 6 | tasks: 7 | - name: Launch Instance 8 | ec2: 9 | group_id: "{{ hostvars[inventory_hostname].group_id }}" 10 | count: 1 11 | instance_type: 'm3.medium' 12 | image: '{{ hostvars[inventory_hostname].image }}' 13 | wait: true 14 | region: 'us-east-1' 15 | #keypair: '{{ keypair }}' 16 | aws_access_key: "{{ aws_access_key|default(lookup('env', 'AWS_ACCESS_KEY')) }}" 17 | aws_secret_key: "{{ aws_secret_key|default(lookup('env', 'AWS_SECRET_KEY')) }}" 18 | instance_tags: "{{ hostvars[inventory_hostname].tags }}" 19 | register: ec2 20 | 21 | - name: Wait for SSH 22 | wait_for: 23 | host: "{{ item['instances'][0]['public_ip'] }}" 24 | port: 22 25 | delay: 10 26 | timeout: 320 27 | state: started 28 | with_items: ec2.results 29 | 30 | - name: Wait a little longer for centos 31 | pause: seconds=20 32 | 33 | - name: Add hosts group temporary inventory group with pem path 34 | add_host: 35 | name: "{{ item.1.platform }} {{ ec2.results[item.0]['instances'][0]['public_ip'] }}" 36 | groups: dynamic_hosts 37 | ansible_ssh_host: "{{ ec2.results[item.0]['instances'][0]['public_ip'] }}" 38 | ansible_ssh_private_key_file: '{{ pem_path }}' 39 | ansible_ssh_user: "{{ item.1.ssh_user }}" 40 | ec2_vars: "{{ ec2.results[item.0]['instances'][0] }}" 41 | ec2_instance_ids: "{{ ec2.results[item.0]['instance_ids'] }}" 42 | with_indexed_items: ec2_instances 43 | 44 | - hosts: dynamic_hosts 45 | tasks: 46 | - ping: 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /inventory: -------------------------------------------------------------------------------- 1 | localhost ansible_connection=local ansible_python_interpreter=/usr/local/bin/python 2 | 3 | [ec2_instances] 4 | ec2host1 image="ami-2ccc7a44" tags="{ 'foo': 'bar', 'name': 'ec2host1' }" group_id='sg-07bb906d' 5 | ec2host2 image="ami-2ccc7a44" tags="{ 'foo': 'bar', 'name': 'ec2host2' }" group_id='sg-07bb906d' 6 | 7 | [ec2_instances:vars] 8 | # mac osx brew :( 9 | ansible_python_interpreter=/usr/local/bin/python 10 | 11 | -------------------------------------------------------------------------------- /new_group.yml: -------------------------------------------------------------------------------- 1 | # Start multiple ec2 instances with instance-specific tags assigned to each 2 | # 3 | - hosts: localhost 4 | vars: 5 | ec2_instances: 6 | - name: "machine1" 7 | image: "ami-2ccc7a44" # Ubuntu 12.04 8 | tags: ['foo1', 'foo2'] 9 | group_id: 'sg-07bb906d' # jenkins-slave_new 10 | ssh_user: "ubuntu" 11 | - name: "machine2" 12 | image: "ami-2ccc7a44" # Ubuntu 12.04 13 | tags: ['foo1', 'foo2'] 14 | ssh_user: "ubuntu" 15 | group_id: 'sg-07bb906d' # jenkins-slave_new 16 | tasks: 17 | - name: Launch Instance 18 | ec2: 19 | group_id: "{{ item.group_id }}" 20 | count: 1 21 | instance_type: 'm3.medium' 22 | image: '{{ item.image }}' 23 | wait: true 24 | region: 'us-east-1' 25 | #keypair: '{{ keypair }}' 26 | aws_access_key: "{{ aws_access_key|default(lookup('env', 'AWS_ACCESS_KEY')) }}" 27 | aws_secret_key: "{{ aws_secret_key|default(lookup('env', 'AWS_SECRET_KEY')) }}" 28 | instance_tags: "{{ item.tags }}" 29 | register: ec2 30 | with_items: ec2_instances 31 | 32 | - name: Wait for SSH 33 | wait_for: 34 | host: "{{ item['instances'][0]['public_ip'] }}" 35 | port: 22 36 | delay: 10 37 | timeout: 320 38 | state: started 39 | with_items: ec2.results 40 | 41 | - name: Wait a little longer for centos 42 | pause: seconds=20 43 | 44 | - name: Add hosts group temporary inventory group with pem path 45 | add_host: 46 | name: "{{ item.1.platform }} {{ ec2.results[item.0]['instances'][0]['public_ip'] }}" 47 | groups: dynamic_hosts 48 | ansible_ssh_host: "{{ ec2.results[item.0]['instances'][0]['public_ip'] }}" 49 | ansible_ssh_private_key_file: '{{ pem_path }}' 50 | ansible_ssh_user: "{{ item.1.ssh_user }}" 51 | ec2_vars: "{{ ec2.results[item.0]['instances'][0] }}" 52 | ec2_instance_ids: "{{ ec2.results[item.0]['instance_ids'] }}" 53 | with_indexed_items: ec2_instances 54 | 55 | - hosts: dynamic_hosts 56 | tasks: 57 | - ping: 58 | 59 | --------------------------------------------------------------------------------