# How to properly setup EC2 instance hostname?

**URL:** <https://forum.ansible.com/t/how-to-properly-setup-ec2-instance-hostname/24849>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [June 29, 2017, 11:16am UTC](https://forum.ansible.com/t/how-to-properly-setup-ec2-instance-hostname/24849 "2017-06-29T11:16:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rene](https://avatars.discourse-cdn.com/v4/letter/r/7cd45c/32.png) [@rene](https://forum.ansible.com/u/rene)\
**Post date:** [June 29, 2017, 11:16am UTC](https://forum.ansible.com/t/how-to-properly-setup-ec2-instance-hostname/24849/1 "2017-06-29T11:16:55Z")

</div>

Dear community,

I have another issue I can’t wrap my head around. Possibly because I stared on the screen for too long, maybe because I’m to stupid.

Anyways, I want to create an AWS EC2 instance (Debian Strecht → which obviously uses cloud-init) with a custom hostname. Not the EC2 public/private hostname but  
the os hostname.

Usually I would use the Ansible’s hostname module to change /etc/hostname but I also want to permantly change /etc/hosts.

So I thought it would be best to “inject” my custom hostname during the instance setup via a user-data script. I thought this might cause cloud-init to pick up my custom hostname instead of the one that is automatically created from the private ip-address.

My first question now is: Is that possible? Can I manipulate the initial setup process of the instance to use my custom hostname?

If yes, this would be a huge relief. But that raise the question of how I can achieve that.

This is the piece of my playbook which creates the EC2 instance:

`

- name: Create ec2 instance  
ec2:  
key\_name: “{{ keypair }}”  
vpc\_subnet\_id: “{{ vpc\_subnet\_id }}”  
group\_id: “{{ security\_group\_id }}”  
instance\_type: “{{ instance\_type }}”  
image: “{{ image }}”  
wait: true  
region: “{{ region }}”  
instance\_tags: “{{ tags }}”  
volumes:

- device\_name: xvda  
volume\_type: gp2  
volume\_size: “{{ volume\_size }}”  
delete\_on\_termination: “{{ delete\_on\_termination }}”  
assign\_public\_ip: “{{ assign\_public\_ip }}”  
termination\_protection: “{{ termination\_protection }}”  
no\_log: true  
register: ec2

- name: Wait for SSH to come up  
wait\_for: host={{ item.private\_ip }} port=22 delay=30 timeout=320 state=started  
no\_log: true  
with\_items: ‘{{ ec2.instances }}’

- name: Add new instance to host group  
add\_host: hostname={{ item.private\_ip }} groupname=launched  
no\_log: true  
with\_items: ‘{{ ec2.instances }}’  
`

I could imagine that I add something like “user\_data: “”” to the ec2 block, but I’m not sure about the correct syntax to manipulate the hostname accordingly.

Can someone assist me on that matter?

Best regards  
René

---

<div class="post-metadata">

**Author:** ![rene](https://avatars.discourse-cdn.com/v4/letter/r/7cd45c/32.png) [@rene](https://forum.ansible.com/u/rene)\
**Post date:** [June 29, 2017, 12:01pm UTC](https://forum.ansible.com/t/how-to-properly-setup-ec2-instance-hostname/24849/2 "2017-06-29T12:01:28Z")

</div>

Okay, I found out that it is actually possible to manipulate hostname and fqdn during the instance creation.

I tested it ([https://access.redhat.com/documentation/en-US/Red\_Hat\_Enterprise\_Linux\_OpenStack\_Platform/4/html/End\_User\_Guide/user-data.html](https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux_OpenStack_Platform/4/html/End_User_Guide/user-data.html)) with a simple text file which looks like this:

#cloud-config  
hostname: test1234  
fqdn: test1234.example.local

What I didn’t figure out yet is how to integrate that into my playbook.

Best regards  
René

---

<div class="post-metadata">

**Author:** ![rene](https://avatars.discourse-cdn.com/v4/letter/r/7cd45c/32.png) [@rene](https://forum.ansible.com/u/rene)\
**Post date:** [June 29, 2017, 12:45pm UTC](https://forum.ansible.com/t/how-to-properly-setup-ec2-instance-hostname/24849/3 "2017-06-29T12:45:11Z")

</div>

Okay, got it…

I added the following line to the playbook I posted above (within the ec2 block):

`user_data: "{{ my_user_data }}"`

After that I created this block in my variable file:

`

hostname: “test1234”  
fqdn: “test1234.example.local”  
my\_user\_data: |  
#cloud-config  
hostname: “{{ hostname }}”  
fqdn: “{{ fqdn }}”

`
