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.
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.
Hi Michael,
Thank you for your feedback.
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:
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
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.
Hi Brian,
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
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