Stuck: One or more undefined variables: 'str object' has no attribute 'base'

3,352 views
Skip to first unread message

Patrick Ansible-ML

unread,
Sep 10, 2014, 8:27:48 AM9/10/14
to ansible...@googlegroups.com
Hi all,

New to the list. Been playing with Ansible for a few hours and I could
use a hand in getting a task going. I've not figured out any of the YAML
magic so please excuse the undoubtedly spectacular errors :)

Goal: create a task that installs a couple of directories in
/home/patrick and /root.

---
# file: roles/common/tasks/main.yml

- name: dirs | Include vars
include_vars: main.yml

- name: dirs | Create some directories
file:
dest: "{{ item.base }}/{{ item.usr }}/{{ item.dirs }}"
mode: 0750
owner: "{{ item.usr }}"
group: "{{ item.usr }}"
state: directory
with_items: test

---
# file: roles/common/vars/main.yml

test:
- usr: patrick
base: /home
- usr: root
base: /
- dirs: test1
- dirs: test2


When I run this with:
$ ansible-playbook -v -i stage site.yml

I get:
TASK: [common | dirs | Create some directories]
*******************************
fatal: [test.local] => One or more undefined variables: 'str object' has
no attribute 'base'

I'm guessing that the issue is in vars/main.yml file but lots of
Googling and staring at Github/Galaxy examples did not reveal a
solution. Can anyone please enlighten me a bit?

Thanks,
Patrick

Michael DeHaan

unread,
Sep 10, 2014, 9:26:14 AM9/10/14
to ansible...@googlegroups.com
Yes, where you are defining "test" some of the items in that list are hash tables that don't have a value for "base", which is why you are getting that error.

Easiest would be to set it to empty string when it does not apply.

However, it's a little weird that it says "str object", which seems to imply you have, probably in that "dirs" variable file, refined the variable "test" to a list of strings, as opposed to a list of hash elements.




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/540F8975.4090403%40puzzled.xs4all.nl.
For more options, visit https://groups.google.com/d/optout.

Patrick Ansible-ML

unread,
Sep 10, 2014, 11:46:27 AM9/10/14
to ansible...@googlegroups.com
Hi Michael,

Thank you for your feedback.

On 10-09-14 15:26, Michael DeHaan wrote:
> Yes, where you are defining "test" some of the items in that list are
> hash tables that don't have a value for "base", which is why you are
> getting that error.

Clearly I need to read more about lists, hashes and dicts. Not being a
developer, please stand by while I try to wrap my head around them :)

> Easiest would be to set it to empty string when it does not apply.

I couldn't figure that out but in the docs I found 'Nested Loops' that
seemed promising. For the archives, here's how I got it working:

------
# file: roles/common/tasks/main.yml

- name: dirs | Include vars
include_vars: main.yml

- name: dirs | Create some directories
file:
dest: "{{ item.0.base }}{{ item.0.usr }}/{{ item.1 }}"
mode: 0750
state: directory
with_nested:
- users
- dirs

---
# file: roles/common/vars/main.yml

users:
- usr: patrick
base: /home/
- usr: root
base: /

dirs:
- test1
- test2


> However, it's a little weird that it says "str object", which seems to
> imply you have, probably in that "dirs" variable file, refined the
> variable "test" to a list of strings, as opposed to a list of hash elements.

FWIW it's Ansible 1.7.1 on Fedora 20 x86_64. If weird means bug, please
let me know if you'd like me to file a ticket on github.

Thanks,
Patrick
> ******************************__*

Brian Coca

unread,
Sep 10, 2014, 5:14:05 PM9/10/14
to ansible...@googlegroups.com
short description, list (or array) is as it sounds, simple list of
items, a dict or a hash is a list that uses an 'name' to locate the
item

1, 2, 3 < = simiple list, in python/yaml/json (stuff ansible uses) it
is normally in brackets mylist= [1,2,3], lists can be referenced by a
number that describes the position (normally starting by 0) mylist[1]
would return 2

a dict, hash or associative array is a special list that uses names,
normally represented with {}
mydict = { 'one': 1, 'two': 2, 'three', 3}, to access an item you use
the names: mydict['two'] returns 2.

in yaml starting with a '-' normally indicates a list item
mylist:
- 1
- 2
- 3

for dicts just ignore the - and add the name
mydict:
one: 1
two: 2
three: 3

On Wed, Sep 10, 2014 at 11:46 AM, Patrick Ansible-ML
<ans...@puzzled.xs4all.nl> wrote:
>
> Clearly I need to read more about lists, hashes and dicts. Not being a
> developer, please stand by while I try to wrap my head around them :)




--
Brian Coca
Stultorum infinitus est numerus
0110000101110010011001010110111000100111011101000010000001111001011011110111010100100000011100110110110101100001011100100111010000100001
Pedo mellon a minno

Michael DeHaan

unread,
Sep 11, 2014, 10:24:03 AM9/11/14
to ansible...@googlegroups.com
I'd need to see how you defined test previously to know for sure, but it seems like it was a list of strings and that would have been normal I think.



--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CADn%2BHszdP68YRO6ZGOVw7Q5n_d5uzdyM_QB_r2DYz3cKZePGjA%40mail.gmail.com.

Patrick Ansible-ML

unread,
Sep 11, 2014, 8:04:25 PM9/11/14
to ansible...@googlegroups.com
Hi Brian,

On 10-09-14 23:14, Brian Coca wrote:
> short description, list (or array) is as it sounds, simple list of
> items, a dict or a hash is a list that uses an 'name' to locate the
> item

Got it.

> 1, 2, 3 < = simiple list, in python/yaml/json (stuff ansible uses) it
> is normally in brackets mylist= [1,2,3], lists can be referenced by a
> number that describes the position (normally starting by 0) mylist[1]
> would return 2

Ok.

> a dict, hash or associative array is a special list that uses names,
> normally represented with {}
> mydict = { 'one': 1, 'two': 2, 'three', 3}, to access an item you use
> the names: mydict['two'] returns 2.

Ok.

> in yaml starting with a '-' normally indicates a list item
> mylist:
> - 1
> - 2
> - 3

Ok.

> for dicts just ignore the - and add the name
> mydict:
> one: 1
> two: 2
> three: 3

Ok.

That wasn't too bad :) Small addition. In my first few hours with
Ansible I needed this one for file copying from src to dst:

mydict:
- src: file1.txt
dst: /some/where/file_1.txt

Thank you for your explanation. That was most helpful. For the archives,
the Ansible examples on github also provide a ton of enlightening info
on how to do things:

https://github.com/ansible/ansible-examples/tree/master/language_features

Cheers,
Patrick

Michael DeHaan

unread,
Sep 11, 2014, 8:46:21 PM9/11/14
to ansible...@googlegroups.com
Yeah I am overdue to update the language_features dir, it should be all mostly accurate, but it should be a little better organized - we've been focusing on docs.ansible.com for a long time.

I don't think it will teach any bad habits though :)



--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages