In part 4 of the AWS Ansible sipXcom blog series I’ll show you how to add AWS Route 53 entries to match a newly created server.

At the end of part 3 we have already created and start running a production ready sipXcom server on AWS EC2 instance.

The only shortcoming was the need to use the server’s IP addresses, but those IPs change as soon as you shut down an instance and who want’s to remember numbers.

In this (short) tutorial we will use Ansible’s route53 module documented here:

http://docs.ansible.com/ansible/route53_module.html

Step 1. Connect to route 53 service

Go to  AWS management console → Services → Route 52

AWS management console

 

Get your hosted zone name. In our case this is ezuce.ro

Hosted zones

Step 2.  Rewrite info.yml to use new hosted zone

cat files/info_hosted.yml

         standard_ami: ami-edb9069e
         sipxcom_ami: ami-b49defc7
         tiny_instance: t1.micro
         free_instance: t2.micro
         large_instance: m4.large
         ssh_keyname: devops-key
         inst_id: i-8b3a21b5
         template_id:
         secgroup_id: sg-9a9ca7fd
         sip_domain: mihai.ezuce.ro
         sip_realm: mihai.ezuce.ro
         net_domain: mihai.ezuce.ro
         net_host: uc1

Note: ezuce.ro domain.

Step 3. Create new playbook for configuring EC2 instance

pwd
~/Ansible_AWS_SipXcom/

$ cat configure_sipxcom_named.yml
         ---
         - hosts: localhost
           connection: local
           remote_user: devops
           gather_facts: no
           vars_files:
           - files/awscreds.yml
           - files/info_hosted.yml
           tasks:
           - name: Basic provisioning of EC2 instance
	           ec2:
  	           aws_access_key: "{{ aws_id }}"
  	           aws_secret_key: "{{ aws_key }}"
  	           region: "{{ aws_region }}"
  	           image: "{{ sipxcom_ami }}"
  	           instance_type: "{{ free_instance }}"
  	           key_name: "{{ ssh_keyname }}"
  	           count: 1
  	           state: present
  	           group_id: "{{ secgroup_id }}"
  	           wait: yes
  	           vpc_subnet_id: subnet-29a32841
  	           assign_public_ip: yes
  	           instance_tags:
    	           Name: uc1.mihai.ezuce.ro
	           register: ec2info
            - name: Print the results
	            debug: var=ec2info
            - name: Add all instance public IPs to host group
	            add_host: hostname={{ ec2info.instances[0].public_ip  }}
          	    groups=ec2hosts
          	    ansible_ssh_private_key_file=~/.ssh/devops-key.pem

We talked about the following section in previous blog articles:

	 
  - name: Add Route 53 entries for newly created EC2 Instance
	route53:
  	command: create
  	zone: ezuce.ro
  	record: "{{ net_host }}.{{ net_domain }}"
  	type: A
  	ttl: 7200
  	value: "{{ ec2info.instances[0].public_ip  }}"
  	overwrite: yes

For today’s tutorial we will look just at this section:
command: create -self explanatory

zone: ezuce.ro – my hosted zone

record: “{{ net_host }}.{{ net_domain }}” – We want to create an A record that will point to my above
(info_hosted.yml) explained FQDN

value: “{{ ec2info.instances[0].public_ip }}” – A record will point to the dynamically changing IP address
obtained in ec2info json object

overwrite: yes – Mandatory since our IP will change frequently

          - name: Add TCP SIP  SRV
	          route53:
  	          command: create
  	          zone: ezuce.ro
  	          overwrite: yes
  	          record: "_sip._tcp.{{ net_domain }}."
  	          type: SRV
  	          value: "10 10 5060 {{ net_host }}.{{ net_domain }}"
        	 
          - name: Add UDP SIP  SRV
	          route53:
  	          command: create
  	          zone: ezuce.ro
                  overwrite: yes
  	          record: "_sip._udp.{{ net_domain }}."
  	          type: SRV
  	          value: "10 10 5060 {{ net_host }}.{{ net_domain }}"
 

With record: “_sip._tcp.{{ net_domain }}.” – we are creating DNS SRV’s with TCP and UDP that will have them pointing to “10 10 5060 {{ net_host }}.{{ net_domain }}”

Note: If you want to read more about DNS SRV’s check this link:
http://wiki.sipxcom.org/display/unite/DNS+Concepts

We talked about this section in previous blog articles as well:

       - hosts: ec2hosts
 	        vars_files:
  	      - files/awscreds.yml
  	      - files/info_hosted.yml
        tasks:
        - name: Configure sipxcom
	        remote_user: root
	        shell: sipxecs-setup --noui --sip_domain "{{ sip_domain }}" --sip_realm "{{ sip_realm }}" --net_domain "{{ net_domain }}" 
        --net_host "{{ net_host }}"
    	 

Step 4. Run playbook and connect to Web GUI using FQDN

ansible-playbook configure_sipxcom_named.yml
PLAY [localhost] ***************************************************************

TASK [Basic provisioning of EC2 instance] **************************************
changed: [localhost]

TASK [Print the results] *******************************************************
ok: [localhost] => {
	"ec2info": {
    	"changed": true,
    	"instance_ids": [
        	"i-03ef0b32"
    	],
    	"instances": [
        	{
            	"ami_launch_index": "0",
            	"architecture": "x86_64",
            	"block_device_mapping": {
                	"/dev/sda1": {
                    	"delete_on_termination": false,
                    	"status": "attached",
                    	"volume_id": "vol-482ac9ca"
                	}
            	},
            	"dns_name": "ec2-52-210-225-215.eu-west-1.compute.amazonaws.com",
            	"ebs_optimized": false,
            	"groups": {
                	"sg-9a9ca7fd": "devops"
            	},
            	"hypervisor": "xen",
            	"id": "i-03ef0b32",
            	"image_id": "ami-b49defc7",
            	"instance_type": "t2.micro",
            	"kernel": null,
            	"key_name": "devops-key",
            	"launch_time": "2016-09-05T13:00:39.000Z",
            	"placement": "eu-west-1c",
            	"private_dns_name": "ip-10-0-0-48.eu-west-1.compute.internal",
            	"private_ip": "10.0.0.48",
            	"public_dns_name": "ec2-52-210-225-215.eu-west-1.compute.amazonaws.com",
            	"public_ip": "52.210.225.215",
            	"ramdisk": null,
            	"region": "eu-west-1",
            	"root_device_name": "/dev/sda1",
            	"root_device_type": "ebs",
            	"state": "running",
            	"state_code": 16,
            	"tags": {
                	"Name": "uc1.mihai.ezuce.ro"
            	},
            	"tenancy": "default",
            	"virtualization_type": "hvm"
        	}
    	],
    	"tagged_instances": []
	}
}

TASK [Add all instance public IPs to host group] *******************************
changed: [localhost]

TASK [Wait for SSH to come up] *************************************************
ok: [localhost]

TASK [Add Route 53 entries for newly created EC2 Instance] *********************
changed: [localhost]

PLAY [ec2hosts] ****************************************************************

TASK [setup] *******************************************************************
ok: [52.210.225.215]

TASK [Configure sipxcom] *******************************************************
changed: [52.210.225.215]

PLAY RECAP *********************************************************************
52.210.225.215         	: ok=2	changed=1	unreachable=0	failed=0   
localhost              	: ok=5	changed=3	unreachable=0	failed=0   

Under AWS console — Route 53 you should see entries like:

AWS console entries

 

Now you can access your Web UI by name: Web UI by name

You could also dig for services with:

dig -t SRV _sip._tcp.mihai.ezuce.ro
         ; <<>> DiG 9.10.3-P4-RedHat-9.10.3-9.P4.fc22 <<>> -t SRV _sip._tcp.mihai.ezuce.ro
         ;; global options: +cmd
         ;; Got answer:
         ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 60750
         ;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
         ;; OPT PSEUDOSECTION:
         ; EDNS: version: 0, flags:; udp: 4096
         ;; QUESTION SECTION:
         ;_sip._tcp.mihai.ezuce.ro.    IN    SRV
         ;; ANSWER SECTION:
         _sip._tcp.mihai.ezuce.ro. 3600    IN    SRV    10 10 5060 uc1.mihai.ezuce.ro.
         ;; Query time: 45 msec
         ;; SERVER: 192.168.1.1#53(192.168.1.1)
         ;; WHEN: Wed Sep 07 10:57:36 EEST 2016
         ;; MSG SIZE  rcvd: 91

Don’t miss next tutorial where we will populate our new EC2 instance in DevOps style.

To see the previous blogs in this AWS Ansible sipXcom Blog Series click below:

Part 1: Learn how to Provision a minimal CentOS machine with Ansible-AWS, used to create our own sipXcom based AMI

Part 2: Learn how to create a sipXcom-based AMI

Part 3: Create an EC2 instance from sipXcom template and configure newly launched server