This topic needs a title

Hello Team,

How to see centos server host name using ansible.

Please suggest

Thanks in advance.

Thank you
Gopal Dhapa

How to set host name of centos using ansible.

Thanks in advance.

https://docs.ansible.com/ansible/latest/modules/hostname_module.html

To see the hostname or use it in your playbook, you use the ansible_hostname or ansible_fqdn variables.

As Jonathan Cha’gara Lozada De La Matta mentioned, the hostname module can set it, but many applications still (incorrectly) rely on it also being in the /etc/hosts file. Here’s what I’ve done in my playbooks:

`

  • name: Setup hostname on system as {{ network_hostname }}.{{ network_dnsdomain }}
    hostname:
    name: “{{ network_hostname }}.{{ network_dnsdomain }}”

  • name: Add hostname to local /etc/hosts file
    lineinfile:
    dest: /etc/hosts
    state: present
    regexp: “^{{ network_ipaddr }}”
    line: “{{ network_ipaddr }} {{ network_hostname }}.{{ network_dnsdomain }} {{ network_hostname }}”
    `

In this case, I set network_* variables before calling this - my systems occasionally confuse Ansible and the ansible_hostname/ansible_fqdn are incorrect.

Thank you it’s working now.

Thank you
Gopal Dhapa