krisdigitx
krisdigitx

Reputation: 7136

ansible add host to route53

i am using ansible to provision servers on ec2, after creating the server i would like to create a host entry on route53 zone

---
- hosts: all
  connection: local

  tasks:
  - name: create ec2 instance
    action: 
      module: ec2 
      zone: "{{ zone }}"
      image: "{{ image }}"
      instance_type: "{{instance_type}}"
      region: "{{ region }}"
      vpc_subnet_id: "{{ subnet }}"
      group: "{{ security_group }}"
      key_name: "{{ sshkey }}"
      instance_tags: 
        Name: "{{inventory_hostname}}"
        Environment: "{{ Environment  }}"
        Date: "{{ Date}}"
        Noderole: "{{ NodeRole }}"
        ConfigurationGroup: "{{ ConfigurationGroup}}"
        Backups: "{{ Backups }}"

      count_tag:
        Name: "{{inventory_hostname}}"
      exact_count: 1


  - name: Ensure DNS entry exists
    action:
      module:  route53
      command: create
      overwrite: "yes"
      record: "{{ inventory_hostname }}.{{ server_zone }}" 
      type: A 
      zone: "{{ server_zone }}"
      value: "{{ item.private_ip }}"
    with_items: "ec2.instances"

the attributes, "inventory_hostname" , "server_zone" are defined in the inventory files for the hosts so they work when the EC2 instance is created.

[kshk:~/testing/ansible-ec2] master* ± ansible-playbook -i inventory/development/devcm_q/inventory.ini create-ec2-instance.yml --limit dcm-jmp-09 -v

PLAY [all] ******************************************************************** 

GATHERING FACTS *************************************************************** 
ok: [dcm-jmp-09]

TASK: [create ec2 instance] *************************************************** 
changed: [dcm-jmp-09] => {"changed": true, "instance_ids": ["i-7c9e89f1"], "instances": [{"ami_launch_index": "0", "architecture": "x86_64", "dns_name": "", "ebs_optimized": false, "groups": {"sg-0bf7d96f": "dev-jumpbox"}, "hypervisor": "xen", "id": "i-7c9e89f1", "image_id": "ami-33734044", "instance_type": "t2.micro", "kernel": null, "key_name": "bootstrap", "launch_time": "2016-02-21T04:28:38.000Z", "placement": "eu-west-1c", "private_dns_name": "ip-172-31-8-55.eu-west-1.compute.internal", "private_ip": "172.31.8.55", "public_dns_name": "", "public_ip": null, "ramdisk": null, "region": "eu-west-1", "root_device_name": "/dev/sda1", "root_device_type": "ebs", "state": "pending", "state_code": 0, "tags": {}, "tenancy": "default", "virtualization_type": "hvm"}], "tagged_instances": [{"ami_launch_index": "0", "architecture": "x86_64", "dns_name": "", "ebs_optimized": false, "groups": {"sg-0bf7d96f": "dev-jumpbox"}, "hypervisor": "xen", "id": "i-7c9e89f1", "image_id": "ami-33734044", "instance_type": "t2.micro", "kernel": null, "key_name": "bootstrap", "launch_time": "2016-02-21T04:28:38.000Z", "placement": "eu-west-1c", "private_dns_name": "ip-172-31-8-55.eu-west-1.compute.internal", "private_ip": "172.31.8.55", "public_dns_name": "", "public_ip": null, "ramdisk": null, "region": "eu-west-1", "root_device_name": "/dev/sda1", "root_device_type": "ebs", "state": "pending", "state_code": 0, "tags": {}, "tenancy": "default", "virtualization_type": "hvm"}]}

TASK: [Ensure DNS entry exists] *********************************************** 
fatal: [dcm-jmp-09] => One or more undefined variables: 'unicode object' has no attribute 'private_ip'

FATAL: all hosts have already failed -- aborting

PLAY RECAP ******************************************************************** 
           to retry, use: --limit @/home/kshk/create-ec2-instance.retry

dcm-jmp-09  

however, when the playbook is run, it throws up the error "no attribute 'private_ip"

any ideas?

Upvotes: 2

Views: 1837

Answers (1)

helloV
helloV

Reputation: 52443

You are not registering ec2. How do you expect ec2.instances to contain private_ip?

  - name: create ec2 instance
    action: 
      module: ec2 
      zone: "{{ zone }}"
      .....
      exact_count: 1
    register: ec2

  - name: Ensure DNS entry exists
    action:
      module:  route53
      ....
      zone: "{{ server_zone }}"
      value: {{ item.private_ip }}
    with_items: ec2.instances

Upvotes: 2

Related Questions