首页 > 解决方案 > ansible-playbook:无法使用 fileglob 在预期路径中找到子目录

问题描述

我尝试从roles/common/fileswith复制文件fileglob,但ansible-playbookroles/common/tasks/files

使用角色文档说:

任何副本、脚本、模板或包含任务(在角色中)都可以引用角色/x/{files,templates,tasks}/(目录取决于任务)中的文件,而无需相对或绝对路径。

剧本:

# ./ansible/roles/common/tasks/main.yml
- hosts: localhost
  connection: local
  gather_facts: no
  tasks:
  - name: setup bashrc
    import_tasks: bashrc.yml

任务:

# ./ansible/roles/common/tasks/bashrc.yml
- name: try to find bashrc libs in roles/common/files/bashrc
  copy:
    src: "{{ item }}"
    dest: /tmp
  with_fileglob:
    - bashrc/*.lib.sh
# Causes the same error:
#   loop: "{{ lookup('fileglob', 'bashrc/*', wantlist=True) }}"

文件树:

.
├── ansible
│   └── roles
│       └── common
│           ├── files
│           │   └── bashrc
│           │       ├── shell-aliases.lib.sh
│           │       ├── shell-functions.lib.sh
│           │       └── shell-settings.lib.sh
│           └── tasks
│               ├── bashrc.yml
│               ├── main.retry
│               └── main.yml

运行剧本:

$ ansible-playbook -vvvvv ./ansible/roles/common/tasks/main.yml
...
TASK [try to find bashrc libs in roles/common/files/bashrc] *******...
task path: /home/<user>/git/homedirsync/ansible/roles/common/tasks/bashrc.yml:1
looking for "bashrc" at "/home/<user>/git/homedirsync/ansible/roles/common/tasks/files/bashrc"
looking for "bashrc" at "/home/<user>/git/homedirsync/ansible/roles/common/tasks/bashrc"
looking for "bashrc" at "/home/<user>/git/homedirsync/ansible/roles/common/tasks/files/bashrc"
looking for "bashrc" at "/home/<user>/git/homedirsync/ansible/roles/common/tasks/bashrc"
 [WARNING]: Unable to find 'bashrc' in expected paths
...

Ansible 版本:

ansible 2.6.1
  config file = /home/<user>/.ansible.cfg
  configured module search path = [u'/var/ansible/library']
  ansible python module location = /usr/local/lib/python2.7/dist-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 2.7.6 (default, Nov 13 2018, 12:45:42) [GCC 4.8.4]

我看到了很多它有效的例子和它不起作用的问题,但在我的案例中我无法定位问题的根源。社区,请帮助。

标签: ansible

解决方案


问:“无法使用 fileglob 在预期路径中找到子目录”

A: 引用fileglob 的 NOTES

“匹配是针对 Ansible 控制器上的本地系统文件。”

角色的特性“任何副本、脚本、模板或包含任务都可以引用 roles/x/{files,templates,tasks} ... 中的文件”不适用于fileglob

相反,可以使用特殊变量。例如

with_fileglob:
  - '{{ role_path }}/files/bashrc/*.lib.sh'

推荐阅读