> The EC2 module's docs show how to create several instances at once
> using the "count" variable, and register the results into another
> variable ("ec2").
>
> You can then iterate on the created instances and do stuff (like add
> them to an inventory group) using "with_items: ec2.instances"
>
> However, I can't figure out how to do the following :
>
> 1) Have the user provide a "names" list ( { "web1", "web2", "web3" }
> ). Or better yet, a naming prefix ("web"), a count (3) and a start
> index (1).
> 2) Create them using the ec2 module, register results into "ec2" to
> keep precious info like ec2.instances.public_dns_name.
> 3) Here's the tricky bit : add a route53 DNS entry that CNAMEs
> together each entry in the "names" list with the public_dns_name
> values in "ec2.instances".
Can you give an example for what you are trying to convey here? I'm a
bit confused.
Thanks for replying James,
I’m trying to create a fleet of (for instance) web servers on EC2, and would like to give them meaningful ansible hostnames (web1, web2, etc) instead of the AWS generated public DNS names (ec2-xxxxx.compute-1.amazonaws.com)
I’d also like to create a CNAME DNS record (using the route53 module) pointing their chosen name (web1, web2, etc) to the EC2 DNS record (ec2-xxxxx.compute-1.amazonaws.com). The latter is accessible in the « instances.public_dns_name »
Ideally, I would just need to provide the ec2 module with a list of hostnames for the instances it’s going to create (internally it just needs to set the « Name » EC2 tag), and I’d get back the « item.hostname » (web1,web2) alongside each « item.public_dns_name » (ec2-xxxxx.compute-1.amazonaws.com) when using "with_items: ec2"
The more I think of it, the more it sounds like just a few changes are needed in the ec2 module. I can try and have a go at it, but I’d like to know if there’s something similar already.
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/9geyZDmbrtU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
ansible-ec2 ssh --name foo -u ec2-user
ansible-ec2 ssh --name foo -u ec2-user -n 2
# ansible-playbook create-web.yml --extra-vars "count=n startindex=x env=production"# provisions n web servers in prod env with indices x, x+1, x+2, ... x+n
--- - name: "create and provision web servers in {{ env }} environment" hosts: localhost gather_facts: False tasks: - name: launch instances local_action: module: ec2 key_name: "{{ launch_key.ec2_classic }}" instance_type: "{{ instance.web[env] }}" volumes: - device_name: /dev/sda1 volume_size: 512 delete_on_termination: true - device_name: "{{ ephemeral[0] }}" ephemeral: ephemeral0 - device_name: "{{ ephemeral[1] }}" ephemeral: ephemeral1 # ephemerals are deleted by default on termination region: "{{ region }}" image: "{{ os.amazon.ami_id }}" wait: yes group: "web-{{ env }}" count: "{{ count }}" wait_timeout: 1000 register: created tags: - create
- name: write instance ids and public dns of the instances to local hosts file local_action: module: lineinfile dest: ./hosts line: "{{ item.id }} {{ item.public_dns_name }}" create: yes with_items: created.instances tags: - create
- name: create identifier sequence for tagging debug: msg="{{ item }}" with_sequence: start="{{ startindex }}" count="{{ count }}" format=%02d no_log: true # mute output register: sequence tags: - tag
- name: tag instances no_log: true local_action: >- ec2_tag resource={{ item.0.id }} region={{ region }} args: tags: Name: "Web {{ env|title }} {{ item.1.msg }}" Env: "{{ env }}" Type: server Function: web OS: "{{ os.amazon.name }}" Region: "{{ region }}" ID: "{{ item.1.msg }}" with_together: - created.instances - sequence.results tags: - tag - name: update dns records route53: >- command=create zone=yoursite.com record=web.{{ item.1.msg }}.{{ env }}.server.yoursite.com type=CNAME ttl=300 value={{ item.0.public_dns_name }} overwrite=true with_together: - created.instances - sequence.results tags: - deploy
- name: register instances with load balancers local_action: ec2_elb args: instance_id: "{{ item.id }}" ec2_elbs: "{{ elb_names.web[env] }}" region: "{{ region }}" state: present wait: no with_items: created.instances tags: - deploy
- name: add instances to an in-memory group no_log: true local_action: add_host hostname="{{ item.public_dns_name }}" groupname=fresh with_items: created.instances tags: - create - name: wait for ssh to come up local_action: wait_for host="{{ item.public_dns_name }}" port=22 delay=60 timeout=320 state=started with_items: created.instances tags: - create - name: provision the instances hosts: fresh user: "{{ os.amazon.user }}" vars: user: "{{ os.amazon.user }}" roles: - common - swap - python tags: - configure
- name: summary of created instances hosts: fresh gather_facts: false sudo: no tasks: - name: Get instance ec2 facts action: ec2_facts no_log: true # mute output register: ec2_facts - name: Get resource tags from ec2 facts sudo: false no_log: true # mute output local_action: >- ec2_tag resource={{ ec2_facts.ansible_facts.ansible_ec2_instance_id }} region={{ region }} state=list register: ec2_tags - debug: msg="{{ ec2_facts.ansible_facts.ansible_ec2_instance_id }} | {{ ec2_facts.ansible_facts.ansible_ec2_instance_type }} | {{ ec2_tags.tags.Name }} | {{ ec2_facts.ansible_facts.ansible_ec2_public_hostname }}" tags: - create
...