Does anyone have a better solution for setting up hostname instances before this hostname can be assigned to the server?
Say we use AWS and an instance of ec2-1-2-3-4.compute-1.amazonaws.com instance. When it is configured, we will eventually assign it a CNAME in DNS new-instance.example.com to replace the existing instance already in production.
new-instance.example.com already exists and is in production, so we cannot reassign CNAME to ec2-1-2-3-4.compute-1.amazonaws.com until the machine is configured correctly and we are ready to proceed. However, we want the internal configuration files and the host name in the instance to be newinstance.example.com, however, in preparation for changing the DNS. Therefore, we cannot use the variable inventory_hostname , because it will be ec2-1-2-3-4.compute-1.amazonaws.com. I also have regular production servers that do not need to use the new host name, so I would like to use the existing inventory_hostname with them .
My best idea is to assign a variable in the inventory file:
[production]
ec2-1-2-3-4.compute-1.amazonaws.com new_hostname=new-instance.example.com
playbook new_hostname. new_hostname, , , inventory_hostname. , error_on_undefined_vars false, , . .
-:
- hosts: all
tasks:
- name: "Debug hostname"
debug: msg="inventory_hostname={{ inventory_hostname }}, hostvars[inventory_hostname]['inventory_hostname']={{ hostvars[inventory_hostname]['inventory_hostname'] }}, ansible_hostname={{ ansible_hostname }}, new_hostname={{ new_hostname }}"
ignore_errors: yes
:
TASK: [Debug hostname] ********************************************************
fatal: [ec2-1-2-3-4.compute-1.amazonaws.com] => One or more undefined variables: 'new_hostname' is undefined
new_hostname , , , . , :
ec2-1-2-3- [4: 5].compute-1.amazonaws.com
new_hostname, :
- name: "set hostname fact"
set_fact: testvar1={{ new_hostname }} || {{ inventory_hostname }}
- name: "show hostname fact"
debug: msg="testvar1={{ testvar1 }}"
:
TASK: [set hostname fact] *****************************************************
ok: [ec2-1-2-3-4.compute-1.amazonaws.com]
TASK: [show hostname fact] ****************************************************
ok: [ec2-1-2-3-4.compute-1.amazonaws.com] => {"msg": "testvar1=new-instance.example.com"}
error_on_undefined_vars true, new_hostname , .
?