循环中的Ansible循环

时间:2015-11-10 10:19:34

标签: ansible

如何在ansible(v1.9)playbook中为任何文件名创建postgres集群的配置文件? 例如:

postgres:
  clusters:
    - name: new
      configs:
        - filename: "someconfig.1"
          template: "path.1"
        - filename: "someconfig.2"
          template: "path.2"
    - name: new2
      configs:
        - filename: "someconfig.1"
          template: "path.1"
        - filename: "someconfig.2"
          template: "path.2"

我想这样做:

- name: Create configs for postgres clusters
  template: src={{ item.template }} dest={{ cluster.name }}/{{ item.filename }}
  with_items: item.configs
  with_items: postgres.clusters as cluster

1 个答案:

答案 0 :(得分:0)

我的解决方案:

- name: Update configs for clusters
    sudo: yes
    sudo_user: postgres
    tags:
      - postgres
      - clusters
    template:
      src={{ item.1.template }}
      dest={{ postgres.config_dir }}/{{ item.0.version|default(postgres.version) }}/{{ item.0.name }}/{{ item.1.filename }}
      mode={{ item.1.mode|default("0600") }}
    with_subelements:
      - postgres.clusters
      - configs
    when: item.0.enable|default(true)|bool