11

我正在使用 Ansible 为网络服务器设置自动配置过程。为此,我有一个数组,其中包含要设置的虚拟主机的字典:

vhosts:
  -
    name: 'vhost1'
    server_name: 'domain1.com'
  -
    name: 'vhost2'
    server_name: 'domain2.com'

我准备了一个带有一些通用 nginx vhost 配置的模板:

server {
    listen 80;
    server_name {{ item.server_name }};

    root    /home/www/{{ item.name }}/htdocs;
    index   index.php;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }
}

最后,我使用以下任务将准备好的模板复制到目标主机:

- name: Setup vhosts
  template: src=vhost.j2 dest=/etc/nginx/sites-available/{{ item.name }}
  with_items: vhosts

任务按预期迭代vhost变量。不幸的是,Ansible 不会将当前项从迭代器传递给模板,而是模板可以访问所有当前有效的变量。

有没有办法将当前项目从迭代器传递给模板?

4

1 回答 1

8

turns out that the code above works absolutly perfect. there was another problem in my variables YAML file.

于 2014-01-17T07:58:02.563 回答