1

I'm trying to write the inventory hostname to a remote file for later processing (eventually a remote fact file). The host below has the physical hostname calvin.mydomain however the controller connects to it using the inventory hostname (different DNS) calvin.test.mydomain so I can't just use the -m setup fact variables which only gather info from the perspective of the remote (AFAIK).

I thought I could do this by exporting an environment variable to the remote and then writing it to a file but that just produces the literal word inventory_hostname.

How can I write hostvars[inventory_hostname] or {{inventory_hostname}} to a file in /etc/ansible/facts.d/ in the remote?

sudo ansible-playbook ./playbooks/hostname.yml -k -u root -i calvin.test.mydomain,
TASK [echo the LAN_HOSTNAME environment var] *******...
changed: [calvin.my.testing.dom]

this is my playbook

---
- hosts: all
  tasks:
      - name: "echo the LAN hostname into a file on the remote"
        shell: "echo $LAN_HOSTNAME > /tmp/hostname.ans"
        environment:
            LAN_HOSTNAME: inventory_hostname
Server Fault
  • 3,714
  • 12
  • 54
  • 89
  • Why would you need to do it? – techraf Mar 13 '18 at 14:55
  • We're thinking about using the Ansible facts JSON output to populate an inventory system and need the LAN hostnames in the fact output, hence the reason for the remote file in `/etc/ansible/facts.d/`. Hosts in the test LAN have the same hostnames as the prod LAN (the are clones in a segregated network) but the way they are accessed in DNS is different (which is the inventory hostname) – Server Fault Mar 13 '18 at 14:58
  • Don't abuse `shell` for this. Use `template`, or if what you're really doing actually is this simple, `copy`. – Michael Hampton Mar 13 '18 at 17:09
  • If you have an answer, feel free to post it as an answer. I'm new to ansible and haven't used `template` – Server Fault Mar 13 '18 at 17:11

1 Answers1

1

needed to use this (changed to copy as suggested by @Michael Hampton):

---
- hosts: all
  tasks:
   - name: "Create custom fact directory"
     file:
         path: "/etc/ansible/facts.d"
         state: "directory"

   - name: "Insert custom fact file"
     copy:
         content: "#!/bin/bash\necho {\\\"ansible_LAN_hostname\\\" : \\\"{{ inventory_hostname }}\\\"}"
         dest: /etc/ansible/facts.d/lan_hostname.fact
         owner: root
         group: sysadmin
         mode: 0775
Server Fault
  • 3,714
  • 12
  • 54
  • 89
  • And what do you achieve with that? You can access the inventory hostname with `inventory_hostname` as well as through local facts? – techraf Mar 13 '18 at 18:22
  • When I run `ansible all -m setup` the JSON output will contain the LAN hostname (from the controller perspective) rather than the hostname of the remote. I have multiple network segments where hosts are clones of production. their hostnames are the same in each segment. The only difference is their DNS name on the LAN. `host_a` in testing is the same VM as `host_a` in development but from the LAN these are `host_a.test.local.net` and `host_a.devel.local.net`. WIthout the LAN DNS name, both of these hosts are the same in the JSON output. – Server Fault Mar 13 '18 at 18:36