Ansible – How to Pickup Hostname and Create Self-Signed Certificate

ansibleansible-galaxyansible-playbookansible-tower

i have working command for creating self signed certificate for IHS server's .kdb file and i was trying to use the same command through ansible. Here my aim is that ansible should detect the hostname of application servers and should use that name for the "-dn" of the command.
Below is the working command

[root@myhost bin]# ./gskcmd -cert -create -db /opt/myapp/key.kdb -label IHS -expire 3650 -size 2048 -dn "CN=myserverFQDN" -default_cert yes -pw password

but below is my play and its giving syntax error

tasks:
  - name: get hostname
    shell: echo $HOSTNAME
    register: hostname

  - name: create self signed certificate for IHS
    command: /opt/myapp/bin/gskcmd -cert -create -db /opt/myapp/key.kdb -label IHS -expire 3650 -size 2048 -dn "CN={{ hostname}}" -default_cert yes -pw password

Best Answer

You don't need to echo anything to "get" the hostname. It's already one of Ansible's gathered facts, ansible_nodename. You just need to use it.

    command: /opt/myapp/bin/gskcmd -cert -create -db /opt/myapp/key.kdb -label IHS -expire 3650 -size 2048 -dn "CN={{ansible_nodename}}" -default_cert yes -pw password

Depending on what you are doing, you may need to use one of the alternate facts, ansible_hostname or inventory_hostname.

These variables are set as follows:

  • inventory_hostname is the hostname as it is set in your Ansible inventory. For example www.internal-name.example.
  • ansible_hostname is the unqualified hostname as reported by the system. For example www.
  • ansible_nodename is the fully qualified hostname as reported by the system. For example www.example.com.