1
users:
  - name: hellouser
    password: pw
    accessToken: ""
    readonly: false
  - name: hellouser2
    password: pw2
    accessToken: ""
    readonly: true

results in:

fatal: [IP]: FAILED! => {"changed": false, "failed": true, "msg":
"AnsibleUndefinedVariable: 'unicode object' has no attribute 'name'"}

When users is changed to users2:

users2:
  - name: hellouser
    password: pw
    accessToken: ""
    readonly: false
  - name: hellouser2
    password: pw2
    accessToken: ""
    readonly: true

the ansible run succeeds.

Discussion

  • The issue could be caused due to a collision between a native and custom variable, e.g. users as this issue did not occur anymore as the custom variable users was changed to users2. However users was not found in the What Makes A Valid Variable Name documentation:

add, append, as_integer_ratio, bit_length, capitalize, center, clear, conjugate, copy, count, decode, denominator, difference, difference_update, discard, encode, endswith, expandtabs, extend, find, format, fromhex, fromkeys, get, has_key, hex, imag, index, insert, intersection, intersection_update, isalnum, isalpha, isdecimal, isdigit, isdisjoint, is_integer, islower, isnumeric, isspace, issubset, issuperset, istitle, isupper, items, iteritems, iterkeys, itervalues, join, keys, ljust, lower, lstrip, numerator, partition, pop, popitem, real, remove, replace, reverse, rfind, rindex, rjust, rpartition, rsplit, rstrip, setdefault, sort, split, splitlines, startswith, strip, swapcase, symmetric_difference, symmetric_difference_update, title, translate, union, update, upper, values, viewitems, viewkeys, viewvalues, zfill.

  • In Puppet it is possible to define global and local variables so that there is no collision between native and custom variables. According to this documentation different variable types could be defined in Ansible:
  • Global: this is set by config, environment variables and the command line
  • Play: each play and contained structures, vars entries, include_vars, role defaults and vars.
  • Host: variables directly associated to a host, like inventory, facts or registered task outputs

Questions

  1. How to prevent collisions between native and custom variables in Ansible?
  2. Is there a list of native variables in Ansible?
  3. Are there global and local variables in Ansible like in Puppet?
0

Without the tasks which raised the "AnsibleUndefinedVariable: 'unicode object' has no attribute 'name'" error it is hard to tell what went wrong here. I can't reproduce this error so I guess it is a bug in the module you are using.

Beside the already quoted list of known public attributes there are no further rules I'm aware of. Even using known public attributes did not lead to any errors in my tests.

Ansible has the concept of magic variables which are basically just facts gathered by the setup module. You can get a list of the magic variables by running the setup module against the host.

There are no global and local variables in Ansible in the sense of types. Variables in Ansible have a scope. The scope of a variable depends on where it is set. Take a look at the documentation for variable precedence.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.