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é