Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 易读字典查找_Loops_Data Structures_Ansible - Fatal编程技术网

Loops 易读字典查找

Loops 易读字典查找,loops,data-structures,ansible,Loops,Data Structures,Ansible,我正在尝试生成一些nginx配置,以便为各种docker容器实现反向代理设置 nginx配置生成暂定使用以下ansible剧本生成 第一个块是一个字典,其中包含一些容器的元数据。 第二个块是包含的任务列表(参见第三个块)并循环 在第二个调试消息级别,我可以看到我的rproxy\u fe记录有问题,因为调试没有返回任何内容 net_containers: named: ipv6_range: "ipv6_prefix:AAAA:AAAA:1::/118" ipv6_con

我正在尝试生成一些nginx配置,以便为各种docker容器实现反向代理设置

nginx配置生成暂定使用以下ansible剧本生成

第一个块是一个字典,其中包含一些容器的元数据。 第二个块是包含的任务列表(参见第三个块)并循环

在第二个调试消息级别,我可以看到我的
rproxy\u fe
记录有问题,因为调试没有返回任何内容

 net_containers:
  named:
     ipv6_range: "ipv6_prefix:AAAA:AAAA:1::/118"
     ipv6_container: "ipv6_prefix:AAAA:AAAA:1:2"
     ipv6_gw: "ipv6_prefix:AAAA:AAAA:1:1"
     ipv4_range: "192.168.11.64/30"
     ipv4_container: "192.168.11.66"
     ipv4_gw: "192.168.11.65"
     parent_nic: "{{ net_int_dmz_trusted }}"
     rproxy_be : "mx.example.net"
     rproxy_fe : null
     rproxy_url: null
     rproxy_restrict: null
  hass:
     ipv6_range: "ipv6_prefix:AAAA:AAAA:3::/118"
     ipv6_container: "ipv6_prefix:AAAA:AAAA:3:2"
     ipv6_gw: "ipv6_prefix:AAAA:AAAA:3:1"
     ipv4_range: "192.168.11.72/30"
     ipv4_container: "192.168.11.74"
     ipv4_gw: "192.168.11.73"
     parent_nic: "{{ net_int_dmz_trusted }}"
     rproxy_fe : "home.example.net"
     rproxy_be : "hass-container.example.net"
     rproxy_url: "http://hass-container.example.net:8123"
     rproxy_restrict: "true"
这是我们循环的任务列表:

- name: Starting the processing of one new reverse proxied site
  debug: msg="{{ item.key }}"

- name: Checking on the dictionary lookups
  debug: msg="{{ net_containers[ item.key].rproxy_fe }}"

- name: Install nginx site for letsencrypt requests
  vars:
   rproxy_fe: "{{ net_containers[ item.key].rproxy_fe }}"
  template:
    src: templates/nginx-le.j2
    dest: /etc/nginx/sites-enabled/le-{{ net_containers[ item.key].rproxy_fe }}
  when: net_containers[ item.key].rproxy_fe is defined

- name: Install nginx site for specified site
  vars:
   rproxy_fe: "{{ net_containers[ item.key].rproxy_fe }}"
   rproxy_url: "{{ net_containers[ item.key].rproxy_url }}"
   rproxy_restrict: "{{ net_containers[ item.key].rproxy_restrict }}"
  template:
    src: templates/nginx-http.j2
    dest: /etc/nginx/sites-enabled/http-{{ net_containers[ item.key].rproxy_fe }}
  when: net_containers[ item.key].rproxy_fe is defined

- name: Check if we've generated a cert already
  stat: path=/etc/letsencrypt/live/{{ net_containers[ item.key].rproxy_fe }}/fullchain.pem
  register: cert_stats

- name: Create letsencrypt certificate
  shell: letsencrypt certonly -n --webroot -w /var/www/letsencrypt -m {{ sysadmin_email }} --agree-tos -d {{ net_containers[ item.key ].rproxy_fe }}
  args:
    creates: /etc/letsencrypt/live/{{ net_containers[ item.key].rproxy_fe }}
  when: net_containers[ item.key].rproxy_fe is defined and cert_stats.stat.exists == False

- name: Add letsencrypt cronjob for cert renewal
  cron:
    name: letsencrypt_renewal {{ net_containers[ item.key].rproxy_fe }}
    special_time: weekly
    job: letsencrypt --renew certonly -n --webroot -w /var/www/letsencrypt -m {{ sysadmin_email }} --agree-tos -d {{ net_containers[ item.key].rproxy_fe }}
  when: net_containers[ item.key].rproxy_fe is defined 
这是任务列表和循环的导入:

- include_tasks: add_domain.yml
  loop: "{{ lookup('dict', net_containers)}}"

以下是正确的任务顺序(让它工作起来):

循环部分:

- include_tasks: add_domain.yml
  loop: "{{ query('dict', net_containers|default({}))  }}"
- include_tasks: add_domain.yml
  loop: "{{ query('dict', net_containers|default({}))  }}"