本文介绍了子目录中的剧本,而不是在 group_vars 旁边的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道如何将剧本放入文件夹,但共享相同的角色、group_vars 和其他通常位于根目录的内容?

这是我想要的:

根目录:- group_vars- 角色- 存货- 剧本- my_playbook.yml- 站点.yml- 部署.yml

我们的根目录现在变得非常大,我想将一些剧本拆分到他们自己的文件夹中(如上所示的剧本/).在目录(例如 playbooks/)中与在根目录中时,相同的小剧本无法运行,因为它不会从 group_vars 中获取内容.

我可以部分解决这个问题,并在我的剧本中运行剧本/目录:

- 主机:host_group须藤:真的收集事实:假vars_files:- ../group_vars/all/main.yml

这会选取 main.yml 中定义的变量.但是,我不清楚这是否会添加 group_vars/中定义的组变量,而不是明确指定的 ../group_vars/.

谢谢!

解决方案

Ansible 将选择 group_vars 而不明确说明路径:

这是/tmp/ansible中的示例目录结构:

/tmp/ansible├── group_vars│ └── test_group.yml├── 库存├── 剧本│ └── foo.yml└── 网站.yml

库存文件:

[test_group]本地主机

主手册site.yml:

# site.yml---- 包括:剧本/foo.yml

二级剧本foo.yml:

---- 主机:本地主机任务:- 调试:var=foo

组变量test_group.yml:

foo: bar

这里是执行二级剧本的所有方法:

  • 使用相对路径从根文件夹/tmp/ansible:

    ansible-playbook -i inventory playbooks/foo.yml -c local播放 [本地主机] ****************************************************************收集事实 *******************************************************************好的:[本地主机]任务:[调试 var=foo] *************************************************************好的:[本地主机] =>{变量":{"foo": "酒吧"}}播放回顾 ************************************************************************本地主机:ok=2 已更改=0 无法访问=0 失败=0

  • 来自 Playbooks 子文件夹,在父路径中有库存/tmp/ansible/playbooks:

    ansible-playbook -i ../inventory foo.yml -c local播放 [本地主机] ****************************************************************收集事实 *******************************************************************好的:[本地主机]任务:[调试 var=foo] *************************************************************好的:[本地主机] =>{变量":{"foo": "酒吧"}}播放回顾 ************************************************************************本地主机:ok=2 已更改=0 无法访问=0 失败=0

正如我们在上面的例子中看到的,ansible-playbook 将根据 inventory file 的路径或执行 ansible 的文件夹查找变量.无需额外的努力即可分离剧本.

Does anyone know how to put playbooks into folders, but share the same roles, group_vars, and other stuff typically located at the root dir?

Here's what I'd like to have:

root_dir:
- group_vars
- roles
- inventory
- playbooks
  - my_playbook.yml
- site.yml
- deploy.yml

Our root dir is getting pretty big now and I'd like to split out some playbooks into their own folder (shown as playbooks/ above). An identical tiny playbook fails to run when inside a directory (say, playbooks/) vs at the root dir, because it doesn't grab stuff from group_vars.

I can partially work around this, and run a playbook inside my playbooks/ directory:

- hosts: host_group
  sudo: true
  gather_facts: false
  vars_files:
  - ../group_vars/all/main.yml

This picks up the vars defined in main.yml. However, it's not clear to me if this would add group variables defined in group_vars/, as opposed to the explicitly specificed ../group_vars/.

Thanks!

解决方案

Ansible will pick up group_vars without stating path explicitly:

Here is the example directory structure in /tmp/ansible:

/tmp/ansible
├── group_vars
│   └── test_group.yml
├── inventory
├── playbooks
│   └── foo.yml
└── site.yml

Inventory file:

[test_group]
localhost

Main Playbook site.yml:

# site.yml
---
- include: playbooks/foo.yml 

Secondary Playbook foo.yml:

---
- hosts: localhost

  tasks:

      - debug: var=foo

Group Variables test_group.yml:

foo: bar

Here are all the ways to execute the secondary playbook:

  • From root folder using relative path /tmp/ansible:

    ansible-playbook -i inventory playbooks/foo.yml -c local
    
    PLAY [localhost] **************************************************************
    
    GATHERING FACTS ***************************************************************
    ok: [localhost]
    
    TASK: [debug var=foo] *********************************************************
    ok: [localhost] => {
        "var": {
            "foo": "bar"
        }
    }
    
    PLAY RECAP ********************************************************************
    localhost                  : ok=2    changed=0    unreachable=0    failed=0
    

  • From Playbooks subfolder with inventory in parent path /tmp/ansible/playbooks:

    ansible-playbook -i ../inventory foo.yml -c local
    
    PLAY [localhost] **************************************************************
    
    GATHERING FACTS ***************************************************************
    ok: [localhost]
    
    TASK: [debug var=foo] *********************************************************
    ok: [localhost] => {
        "var": {
            "foo": "bar"
        }
    }
    
    PLAY RECAP ********************************************************************
    localhost                  : ok=2    changed=0    unreachable=0    failed=0
    

As we can see in the above examples, ansible-playbook will look for variables based on the path of inventory file or folder were ansible was executed from. Playbooks can be separated without additional effort.

这篇关于子目录中的剧本,而不是在 group_vars 旁边的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 13:31