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

In Part 2 we created a sipXcom based AMI build on the CentOS created in Part 1.

Let’s use that AMI to create our first EC2 sipXcom instance.

Step 1. Get AMI-id for new sipXcom template

Connect to your AWS console. Under Images– AMI’s we have:

aws-blog-series-part-3_final

Now we should collect ami-b49defc7 and add it to files/info.yml file for sipxcom_ami: key

[code lang=”bash”]
$ cat files/info.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-*****
sip_domain: mihai.test
sip_realm: mihai.test
net_domain: mihai.test
net_host: uc1
[/code]

Note also sip_domain: mihai.test ;sip_realm: mihai.test;net_domain: mihai.test;net_host: uc1 – we will use these values to configure our sipXcom server with fqdn uc1.mihai.test

Step 2. Create playbook that will install EC2 sipXcom server

pwd
~/Ansible_AWS_SipXcom

[code lang=”xml”]
cat configure_sipxcom.yml

– hosts: localhost
connection: local
remote_user: devops
gather_facts: no
vars_files:
– files/awscreds.yml
– files/info.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.test
register: ec2info

– name: Print the results
debug: var=ec2info
[/code]

In the first section, we will use our newly generated sipXcom template to start an EC2 instance.

We’ve explain these settings in previous blog posts.

– 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

Above section will help us dynamically populate hostnames under ec2hosts groups since we are not using static IP’s we need to collect them in playbook using ec2info object that provides instance public IP through ec2info.instances[0].public_ip attribut.

– name: Wait for SSH to come up
remote_user: root
wait_for: host={{ ec2info.instances[0].public_ip }} port=22 delay=10 timeout=320 state=started

We need to wait for sshd to be up and running on the public IP before configuring server.

– hosts: ec2hosts
vars_files:
– files/awscreds.yml
– files/info.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 }}”

We are connecting to the machine and then we will run sipx-setup in non UI mode by providing needed information which we have already added into the info.yml file.

Step 3. Connect to your newly created machine, add superadmin password and start services

pwd
~/Ansible_AWS_SipXcom

[code language=”bash”]

ansible-playbook configure_sipxcom.yml

[/code]

PLAY [localhost] ***************************************************************

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

TASK [Print the results] *******************************************************
ok: [localhost] => {
“ec2info”: {
“changed”: true,
“instance_ids”: [
“i-f0896dc1”
],
“instances”: [
{
“ami_launch_index”: “0”,
“architecture”: “x86_64”,
“block_device_mapping”: {
“/dev/sda1”: {
“delete_on_termination”: false,
“status”: “attached”,
“volume_id”: “vol-c3c62541”
}
},
“dns_name”: “ec2-52-210-185-88.eu-west-1.compute.amazonaws.com”,
“ebs_optimized”: false,
“groups”: {
“sg-9a9ca7fd”: “devops”
},
“hypervisor”: “xen”,
“id”: “i-f0896dc1”,
“image_id”: “ami-b49defc7”,
“instance_type”: “m4.large”,
“kernel”: null,
“key_name”: “devops-key”,
“launch_time”: “2016-09-05T11:07:18.000Z”,
“placement”: “eu-west-1c”,
“private_dns_name”: “ip-10-0-0-180.eu-west-1.compute.internal”,
“private_ip”: “10.0.0.180”,
“public_dns_name”: “ec2-52-210-185-88.eu-west-1.compute.amazonaws.com”,
“public_ip”: “52.210.185.88”, —- NOTE public IP —-
“ramdisk”: null,
“region”: “eu-west-1”,
“root_device_name”: “/dev/sda1”,
“root_device_type”: “ebs”,
“state”: “running”,
“state_code”: 16,
“tags”: {
“Name”: “uc1.mihai.test”
},
“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]

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

TASK [setup] *******************************************************************
ok: [52.210.185.88]

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

PLAY RECAP *********************************************************************
52.210.185.88 : ok=2 changed=1 unreachable=0 failed=0
localhost : ok=4 changed=2 unreachable=0 failed=0

AWS blog series

Don’t miss the next blog article where we will use route 53 to connect to sipXcom instance.

Read Part 1 of the AWS Ansible sipXcom blog series to learn how to provision a minimal CentOS machine with Ansible-AWS. In Part 2 of the series you will learn how to create a SipXcom based AMI.