Skip to content

Instantly share code, notes, and snippets.

@cyberswat
Created June 30, 2015 15:08
Show Gist options
  • Save cyberswat/0d346f6290bb46f3d39a to your computer and use it in GitHub Desktop.
Save cyberswat/0d346f6290bb46f3d39a to your computer and use it in GitHub Desktop.
ansible dynamic hostname from ec2 tag

In my dynamic ec2 inventory I have data similar to:

{
  "_meta": {
    "hostvars": {
      "10.0.33.123": {
        "ec2_tag_Name": "MyFirstHost",
      }
      "10.0.33.124": {
        "ec2_tag_Name": "MySecondHost",
      }
    }
  }
}

How do I make the equivallent of this task work properly in ansible?

- hostname: "{{ hostvars[ {{ ansible_eth0['ipv4']['address'] }} ]['ec2_tag_Name'] }}"

So that the hostname is set correctly on both hosts:

[email protected]:/# hostname
MyFirstHost

[email protected]:/# hostname
MySecondHost
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment