0

The following ansible playbook runs fine, no error at all but the URL just don't resolve/load afterwards. If I use the public IP created for the instance, the page loads.

---
  - name: Provision an EC2 Instance
    hosts: local
    remote_user: ubuntu
    become: yes
    connection: local
    gather_facts: false
    vars:
      instance_type: t2.micro
      security_group: "Web Subnet Security Group"
      image: ami-0c5199d385b432989
      region: us-east-1
      keypair: demo-key
      count: 1
    vars_files:
      - keys.yml

    tasks:
      - name: Create key pair using ouw own pubkey
        ec2_key:
          ec2_access_key: "{{ ec2_access_key }}"
          ec2_secret_key: "{{ ec2_secret_key }}"
          name: demo-key
          key_material: "{{ lookup('file', '/root/.ssh/id_rsa.pub') }}"
          region: us-east-1
          state: present

      - name: Launch the new EC2 Instance
        ec2:
          ec2_access_key: "{{ ec2_access_key }}"
          ec2_secret_key: "{{ ec2_secret_key }}"
          assign_public_ip: yes
          vpc_subnet_id: subnet-0c799bda2a466f8d4
          group: "{{ security_group }}"
          instance_type: "{{ instance_type}}"
          image: "{{ image }}"
          wait: true
          region: "{{ region }}"
          keypair: "{{ keypair }}"
          count: "{{ count }}"
          state: present
        register: ec2

      - name: Add tag to Instance(s)
        ec2_tag:
          ec2_access_key: "{{ ec2_access_key }}"
          ec2_secret_key: "{{ ec2_secret_key }}"
          resource: "{{ item.id }}"
          region: "{{ region }}"
          state: present
          tags:
            Name: demo-webserver
        with_items: "{{ ec2.instances }}"

      - name: Add the newly created EC2 instance(s) to the local host group (located inside the directory)
        lineinfile:
          path="./hosts"
          line="{{ item.public_ip }}"
          insertafter='\[demo-webserver\]'
          state=present
        with_items: "{{ ec2.instances }}"

      - name: Pause for 2 minutes
        pause:
          minutes: 2

      - name: Write the new ec2 instance host key to known hosts
        connection: local
        shell: "ssh-keyscan -H {{ item.public_ip }} >> ~/.ssh/known_hosts"
        with_items: "{{ ec2.instances }}"

      - name: Waiting for the instance to come
        local_action: wait_for
                      host="{{ item.public_ip }}"
                      delay=10
                      connect_timeout=300
                      state=started
                      port=22
        with_items: "{{ ec2.instances }}"

      - name: Install packages
        delegate_to: "{{ item.public_ip }}"
        raw: bash -c "test -e /usr/bin/python || (apt -qqy update && apt install -qqy python-minimal && apt install -qqy apache2 && systemctl start apache2 && systemctl enable apache2)"
        with_items: "{{ ec2.instances }}"

      - name: Register new domain
        route53_zone:
          ec2_access_key: "{{ ec2_access_key }}"
          ec2_secret_key: "{{ ec2_secret_key }}"
          zone: ansible-demo-domain.com

      - name: Create new DNS record
        route53:
          ec2_access_key: "{{ ec2_access_key }}"
          ec2_secret_key: "{{ ec2_secret_key }}"
          zone: ansible-demo-domain.com
          record: ansible-demo-domain.com
          type: A
          ttl: 300
          value: "{{ item.public_ip }}"
          state: present
          overwrite: yes
          private_zone: no
          wait: yes
        with_items: "{{ ec2.instances }}"

      - name: Create new DNS record
        route53:
          ec2_access_key: "{{ ec2_access_key }}"
          ec2_secret_key: "{{ ec2_secret_key }}"
          zone: ansible-demo-domain.com
          record: www.ansible-demo-domain.com
          type: CNAME
          ttl: 300
          value: ansible-demo-domain.com
          state: present
          overwrite: yes
          private_zone: no
          wait: yes

Appreciate your help to point what/where I'm missing is. I usually wait at least 5 minutes before testing the URL but really doens't resolve/load.

Thank you!

20190301_Update: Here's how the hosted zone looks like after provisioning: hosted-zone-after-provisioning and its associated TTLs ttl

bad rabbit
  • 75
  • 6
  • 1
    What does your Route53 hosted zone look like when you're done? Screen shot please... – Chris Pollard Feb 28 '19 at 11:40
  • @ChrisPollard, updated the question to include the hosted zone and TTLs after provisioning. Thank you. – bad rabbit Mar 01 '19 at 15:12
  • After reading [link](https://stackoverflow.com/questions/33877160/linking-amazon-route-53-domain-name-to-ec2-instance?rq=1), I might have just found the problem. I will update soon but feel free to share your thoughts. – bad rabbit Mar 01 '19 at 15:42

0 Answers0