Getting MAC address from Ansible facts in role

12,779

Solution 1

You could use dot notation to get the value.

msg="{{ hostvars[inventory_hostname].ansible_default_ipv4.macaddress }}"

http://docs.ansible.com/ansible/playbooks_variables.html#information-discovered-from-systems-facts

Solution 2

Your problem is you are passing a variable:

{{ hostvars[inventory_hostname][ansible_default_ipv4] }}

You should instead do:

{{ hostvars[inventory_hostname]["ansible_default_ipv4"] }}

Or you could just do:

{{ ansible_default_ipv4["field"] }}

Or:

{{ ansible_default_ipv4.field }}

The reason is that when using dictionaries, you have to pass a field name. If you pass a string (quoted), that will be the field you want to get. If you don't pass a string (unquoted), then it will be a variable containing the field you want to get.

Share:
12,779

Related videos on Youtube

RhythmicDevil
Author by

RhythmicDevil

Updated on June 04, 2022

Comments

  • RhythmicDevil
    RhythmicDevil over 1 year

    I'm trying to get the MAC address for the current host so that I can use the value in a task. Even after reading the docs I cant seem to wrap my head around how to do this. I've been trying to figure out the structure by dumping out the values. The playbook that calls the role does gather facts.

    This is what the task has:

    - name: Get the MAC address
      debug: msg="{{ hostvars[inventory_hostname] }}"
    

    This produces the following (truncated):

    ok: [steve.dev.v4-1-0] => {
        "msg": {
            "ansible_all_ipv4_addresses": [
                "10.1.3.144"
            ], 
            "ansible_all_ipv6_addresses": [
                "fe80::250:56ff:fe8b:1051"
            ], 
            "ansible_architecture": "x86_64", 
            "ansible_bios_date": "09/21/2015", 
            "ansible_bios_version": "6.00", 
            "ansible_check_mode": false, 
            "ansible_cmdline": {
                "KEYBOARDTYPE": "pc", 
                "KEYTABLE": "us", 
                "LANG": "en_US.UTF-8", 
                "SYSFONT": "latarcyrheb-sun16", 
                "crashkernel": "129M@0M", 
                "quiet": true, 
                "rd_NO_DM": true, 
                "rd_NO_LUKS": true, 
                "rd_NO_LVM": true, 
                "rd_NO_MD": true, 
                "rhgb": true, 
                "ro": true, 
                "root": "UUID=408345fe-146b-4dec-b62c-31fe6d60b376"
            }, 
            "ansible_date_time": {
                "date": "2016-10-24", 
                "day": "24", 
                "epoch": "1477329455", 
                "hour": "10", 
                "iso8601": "2016-10-24T17:17:35Z", 
                "iso8601_basic": "20161024T101735509516", 
                "iso8601_basic_short": "20161024T101735", 
                "iso8601_micro": "2016-10-24T17:17:35.509824Z", 
                "minute": "17", 
                "month": "10", 
                "second": "35", 
                "time": "10:17:35", 
                "tz": "MST", 
                "tz_offset": "-0700", 
                "weekday": "Monday", 
                "weekday_number": "1", 
                "weeknumber": "43", 
                "year": "2016"
            }, 
            "ansible_default_ipv4": {
                "address": "10.1.3.144", 
                "alias": "eth1", 
                "broadcast": "10.1.3.255", 
                "gateway": "10.1.0.10", 
                "interface": "eth1", 
                "macaddress": "00:50:56:8b:10:51", 
                "mtu": 1500, 
                "netmask": "255.255.252.0", 
                "network": "10.1.0.0", 
                "type": "ether"
            }, 
    

    But when I try to reference:

    - name: Insert the mac address into the customer license
      debug: msg="{{ hostvars[inventory_hostname][ansible_default_ipv4] }}"
    

    I get this error, which infuriatingly has the data I am looking for:

    fatal: [steve.dev.v4-1-0]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: dict object has no element {u'macaddress': u'00:50:56:8b:10:51', u'network': u'10.1.0.0', u'mtu': 1500, u'broadcast': u'10.1.3.255', u'alias': u'eth1', u'netmask': u'255.255.252.0', u'address': u'10.1.3.144', u'interface': u'eth1', u'type': u'ether', u'gateway': u'10.1.0.10'}\n\nThe error appears to have been in '/opt/deployment_data/playbooks/roles/eti_license/tasks/main.yml': line 13, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Insert the mac address into the customer license\n  ^ here\n"}
    

    What am I doing wrong here?

  • mshohayeb
    mshohayeb almost 7 years
    What is the advantage of this instead of {{ ansible_default_ipv4.macaddress }} directly. It works (as of 2.2)
  • mmv-ru
    mmv-ru over 5 years
    Interesting alternative for better understanding syntax.
  • Evren Yurtesen
    Evren Yurtesen about 2 years
    This answer is misleading, he does not need to use dot notation as @david-barroso pointed out.