Reputation: 11
i am testing looping code on ansible my first try worked:
- file:
state: touch
path: /tmp/{{ item }}
with_sequence: start=0 end={{ find_result.examined }}
this works the directorys are created
but if i use the same loop to create users i get an error
- name: User Creation
user:
name: "{{ find_result.files[item].path | regex_replace('/root/00staging/sshkeys/') | regex_replace('_id_rsa.pub')}}"
comment: nameduser
password: "{{ 'Start123' | password_hash('sha512') }}"
update_password: on_create
createhome: true
group: wheel
register: users_added
with_sequence: start=0 end={{ find_result.examined }}
The error:
{"msg": "The task includes an option with an undefined variable. The error was: 'list object' has no attribute u'0'\n\n
it works when i use the loop with_items
but this is not dynamic enough
the variable is not empty it gets filled by this code:
- name: finding files
find:
paths: "/root/00staging/sshkeys"
file_type: "file"
register: find_result
Update:
i have the same error with simple sequence numbers the [item] is not getting filled but the {{ item }} gets filled correctly
find_result.files[item].path
with_sequence: start=0 end=1
Upvotes: 1
Views: 398
Reputation: 312370
The error you're seeing:
'list object' has no attribute u'0'
Suggests that the value of the find_result.files
key is an empty list. In the first iteration of your loop you're trying to access find_result.files[0]
, but if that list is empty there is no element 0. You can generate the same error with the following example:
---
- hosts: localhost
gather_facts: false
vars:
empty_list: []
tasks:
- debug:
msg: "item {{ item }} is {{ empty_list[item] }}"
with_sequence: start=0 end=1
You should inspect the contents of the find_result
variable (using e.g. a debug
task) to confirm that it contains what you think it should contain.
Upvotes: 1