【问题标题】:Creating multiple directories from vars in ansible vars file从ansible vars文件中的vars创建多个目录
【发布时间】:2017-10-02 13:07:05
【问题描述】:

我希望通过从 ansible .yml vars 文件传入 vars 在我的给定服务器上创建大量目录。我目前正在尝试使用 Jinja 模板来传递目录名称。 vars 文件如下所示;

BTG-VELOCITY:
    type: PBSTP
    accept: 1010
GFAM:
    type: PBSTP
    connect: 1010
ONEZERO2:
    type: TRADESTREAM
GUANFABANK:
    type: FXSIM
MAINBANK:
    type: FXSIM
TYPOBANK:
    type: TRADESTREAM
TEST-BANK:
    type: PBSTP
    connect: 32620
    accept: 33620

我希望为列出的每个客户名称创建一个目录。所以理想情况下,我最终会在给定文件夹中为每个 'TYPOBANK'、'MAINBANK' 等创建一个目录。 我现在尝试的方法如下:

- include_vars:
    file: /home/vagrant/stunnelSimAnsPractice/roles/ns16/vars/customers.yml
    name: customers
- file:
    path: /home/vagrant/stunnelSimAnsPractice/roles/ns16/sessions/{%for cust, config in customers.items() %}{{cust}}{% endfor %}
    state: directory

但这会输出一个包含所有客户名称组合的目录,如下所示。关于如何为每个客户创建个人目录的任何建议?谢谢

├── sessions
│   └── GFAMTEST-BANKBTG-VELOCITYTYPOBANKMAINBANKGUANFABANKONEZERO2

【问题讨论】:

    标签: ansible yaml jinja2


    【解决方案1】:

    您不能在 Ansible 中的单个参数内循环。

    使用 loops,因为 Ansible 旨在:

    - file:
        path: /home/vagrant/stunnelSimAnsPractice/roles/ns16/sessions/{{ item }}
        state: directory
      with_items:     ### and here you need to put the list containing the directory names
    

    很遗憾,您没有提供变量的完整定义,因此您需要自定义代码以满足您的需求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-03
      • 2021-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-05
      相关资源
      最近更新 更多