在ansible中在with_subelement上使用with_sequence循环

时间:2018-07-29 13:10:53

标签: ansible jinja2

我正在尝试使用with_sequence之类的循环遍历子元素变量,

目前我有:

--- 
- hosts: corosync
  gather_facts: no 

  vars:
    host_list:
     - node_one
     - node_two

    list_services:
     - group: ALPHA
       services:
         - name: DHCP
           directory: /etc/dhcp
         - name: DNS
           directory : /etc/dns
     - group: BETA
       services:
         - name: SSH
           directory: /etc/ssh
         - name: FTP
           directory: /ztc/ftp

 tasks:
 - name: create group-services
   debug:  
      msg: "the service name is {{ item.0.group}}-{{ item.1.name}} , directory is  {{ item.1.directory }}" 

   with_subelements:
    - "{{ list_services }}"
    - services

因为我的集群中有2个节点

  • node_one
  • node_two

我想取消每种服务,如下所示:

{{ item.0.group}}-{{host_id}}-{{ item.1.name}} 

由于我有2个节点,因此{{ host_id }}的列表等于['0','1']

with_subelement函数在{{ host_id }}上循环两次,因为我们有两个节点,这给出了:

  • ALPHA-0-DHCP
  • ALPHA-0-DNS
  • ALPHA-1-DHCP
  • ALPHA-1-DNS

  • BETA-0-SSH

  • BETA-0-FTP
  • BETA-1-SSH
  • BETA-1-FTP

我想在with_sequence旁边使用类似with_subelement的功能

with_sequence: start=0, end={{ groups['host_list']|length}}

请提出任何建议

1 个答案:

答案 0 :(得分:2)

loop declaration introduced in Ansible 2.5非常简单ー您只需要组合两种模式来替换legacy with_sequencelegacy with_subelements

NSToolbarItem