我有一个数据库主机,上面有一些sql文件。我想使用 ansible playbook 将其源到数据库中。所以我找到所有文件并在变量中注册,然后尝试使用下面的代码获取这些文件。
- name: get schema files
find:
paths: "~/dbs/"
recurse: no
patterns: "*.sql"
register: db_sql_files
- name: import schemas
mysql_db:
name: all
state: import
target: "{{ item['path'] }}"
login_user: "{{ db_user }}"
login_password: "{{ db_pass }}"
with_items: "{{ db_sql_files['files'] }}"
当我运行 playbook 时,出现以下错误。
The task includes an option with an undefined variable. The error was: 'item' is undefined
答案1
该条目的缩进似乎with_items
有误。从条目中删除两个空格with_items
应该可以解决该问题:
- name: import schemas
mysql_db:
name: all
state: import
target: "{{ item['path'] }}"
login_user: "{{ db_user }}"
login_password: "{{ db_pass }}"
with_items: "{{ db_sql_files['files'] }}" # On the sime line as the mysql_db call.