为什么 Ansible 不读取相对路径中的模板? [英] Why Ansible doesn't read the templates in relative path?

查看:44
本文介绍了为什么 Ansible 不读取相对路径中的模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Ansible,但模板路径存在一些问题.这是我执行时的错误输出:

I am using Ansible and I have some problems with the templates path. Here is the error output when I execute:

$ ansible-playbook -i hosts site.yml 

PLAY [users] ****************************************************************** 

GATHERING FACTS *************************************************************** 
ok: [10.0.3.240]

TASK: [templates] ************************************************************* 
fatal: [10.0.3.240] => {'msg': 'unable to read /home/robe/Desktop/ansible_demo/fig.conf.j2', 'failed': True}
fatal: [10.0.3.240] => {'msg': 'unable to read /home/robe/Desktop/ansible_demo/fig.conf.j2', 'failed': True}

FATAL: all hosts have already failed -- aborting

PLAY RECAP ******************************************************************** 
           to retry, use: --limit @/home/robe/site.retry

10.0.3.240                 : ok=1    changed=0    unreachable=1    failed=0 

这是我的项目结构:

$ tree
.
├── ansible.cfg
├── hosts
├── roles
│   └── users
│       ├── files
│       ├── handlers
│       │   └── main.yml
│       ├── tasks
│       │   └── main.yml
│       ├── templates
│       │   └── fig.conf.j2
│       └── vars
│           └── main.yml
├── site.yml
└── Vagrantfile

这是我的 site.yml 代码:

This is my site.yml code:

---
- hosts: users
  remote_user: root
  sudo: True
  tasks:
  - name: templates
    template: src="fig.conf.j2" dest="/home/vagrant/fig.conf"

那么,为什么 Ansible 不查看模板目录而只查看根目录.

Then, why Ansible doesn't look into templates directory and it only looks in the root directory.

推荐答案

当您显式使用角色users"时,Ansible 只会在 roles/users/templates 目录中查找,而您在示例中并未使用该角色.要执行您想要的操作,您需要将您的 site.yml 更改为如下所示:

Ansible will only look in the roles/users/templates directory when you explicitly use the role "users", which you are not using in your example. To do what you want you need to change your site.yml to look something like this:

- hosts: users
  remote_user: root
  sudo: True
  roles:
    - { role: users }

然后在roles/users/tasks/main.yml 中,您将拥有:

Then in roles/users/tasks/main.yml you would have:

- name: templates
  template: src="fig.conf.j2" dest="/home/vagrant/fig.conf"

site.yml 中的角色告诉 Ansible 调用 yaml 文件角色/用户/任务/main.yml.默认情况下,引用该角色内的文件或模板的任务将在角色/用户/文件和角色/用户/模板中查找这些文件/模板.您可能希望在 Ansible 文档 中阅读更多有关角色的信息,以确保您更好地了解它们是如何工作的结合在一起.

The role in site.yml tells Ansible to invoke the yaml file roles/users/tasks/main.yml. Tasks that refer to files or templates within that role will by default look in roles/users/files and roles/users/templates for those files/templates. You might want to read more about roles in the Ansible documentation to make sure you better understand how they fit together.

这篇关于为什么 Ansible 不读取相对路径中的模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆