Ansible Alternative目录结构需要路径

时间:2017-11-01 16:46:04

标签: python-2.7 ansible ansible-role

我正在尝试从Ansible docs site最佳实践部分创建备用目录布局

遵循本指南,这里是我在~/ansible_proj/windows_setup内创建的布局(windows_setup是一个Python虚拟环境):

.venv
inventories/
   production/
      hosts
      group_vars/
         windows
            windows.yml
            vault

windows_configure.yml

roles/
    role1/
    role2/
    role3/
    role4/

.gitignore # this contains 1 line produced by: echo 'vault' >> .gitignore

当我尝试按照建议here(参见第1个示例)运行剧本windows_configure.yml时,使用

$ (.venv) cd ~/ansible_proj/windows_setup
$ (.venv) user@sname: ~/ansible_proj/windows_setup$ ansible-playbook
                      -i production windows_configure.yml --ask-vault-pass

错误是

Unable to parse /.../production as an inventory source

production hosts list is empty, only localhost is available

我必须指明hosts文件的路径,而不是:

$ (.venv) user@sname: ~/ansible_proj/windows_setup$ ansible-playbook
                       -i inventories/production/hosts windows_configure.yml --ask-vault-pass

以便运行。

Q1。如何使用备用目录布局来使用production(如文档所示)而不是inventories/production/hosts来运行Playbooks ?

在相同的目录结构中,如果我将windows_configure.yml放在playbooks/内,并使用以下命令运行:

$ (.venv) user@sname: ~/ansible_proj/windows_setup$ ansible-playbook
                      -i inventories/production/hosts playbooks/windows_configure.yml
                      --ask-vault-pass

然后我收到此错误

ERROR: the role 'role1' was not found in /.../playbooks/roles:/.../playbooks

Q2。在此备用目录结构中,是否可以将所有Playbook放在playbooks目录中?

修改

我尝试了adding ansible.cfg to the current directory~/ansible_proj/windows_setup)(其中还包含了剧本windows_configure.yml。这是ansible.cfg

[defaults]
inventory  = ~/ansible_proj/windows_setup/inventories/production
roles_path = ~/ansible_proj/windows_setup

当我跑步时

$ (.venv) cd ~/ansible_proj/windows_setup
$ (.venv) user@sname: ~/ansible_proj/windows_setup$ ansible-playbook
                      -i production windows_configure.yml --ask-vault-pass

出现相同的错误

Unable to parse /.../production as an inventory source

production hosts list is empty, only localhost is available

1 个答案:

答案 0 :(得分:0)

仅限 Q1。

this github issue中讨论了这个问题 - 解决方法是使用inventories/production。我测试了这个(当前目录中没有任何ansible.cfg文件)并且它工作正常:

$ (.venv) user@sname: ~/ansible_proj/windows_setup$ ansible-playbook
                      -i inventories/production windows_configure.yml --ask-vault-pass

对于 Q2。

看起来符号链接可能是一种方法 - 我不想这样做,所以我将playbook留在根项目目录中,而不是在playbooks目录中。但是,这里有一些基于符号链接的解决方案:12。我不确定是否有其他方法可以解决此问题。