Поиск по словарю - PullRequest
       5

Поиск по словарю

0 голосов
/ 06 марта 2019

Я пытаюсь сгенерировать конфигурацию nginx для реализации настройки обратного прокси-сервера для различных контейнеров Docker.

Генерация конфигурации nginx предварительно генерируется с использованием приведенных ниже приложений.

Первый блок - это словарь, содержащий метаданные для моих контейнеров. Второй блок - это список задач, которые включены (см. Третий блок) и зациклены.

На уровне моего второго сообщения отладки я вижу, что у меня есть проблема с моей rproxy_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)}}"

1 Ответ

0 голосов
/ 24 марта 2019

Вот правильная последовательность задач (заставил ее работать):

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

- name: Install nginx site for specified site
  vars:
   ipv4_gw: "{{ item.value.ipv4_gw }}"
   rproxy_fe: "{{ item.value.rproxy_fe }}"
   rproxy_url: "{{ item.value.rproxy_url }}"
   rproxy_restrict: "{{ item.value.rproxy_restrict }}"
  template:
    src: templates/nginx-http.j2
    dest: /etc/nginx/sites-enabled/http-{{ item.value.rproxy_fe }}
  when: item.value.rproxy_certbot is defined and item.value.rproxy_certbot

- name: Check if we've generated a cert already
  stat: path=/etc/letsencrypt/live/{{ item.value.rproxy_fe }}/fullchain.pem
  register: cert_stats
  when: item.value.rproxy_certbot is defined and item.value.rproxy_certbot

- name: Reload nginx to activate specified site
  service: name=nginx state=restarted
  when: item.value.rproxy_certbot is defined and item.value.rproxy_certbot and cert_stats.stat.exists == False

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

- name: Install nginx site for letsencrypt requests
  vars:
   ipv4_gw: "{{ item.value.ipv4_gw }}"
   rproxy_fe: "{{ item.value.rproxy_fe }}"
   rproxy_url: "{{ item.value.rproxy_url }}"
   rproxy_restrict: "{{ item.value.rproxy_restrict }}"
  template:
    src: templates/nginx-le.j2
    dest: /etc/nginx/sites-enabled/le-{{ item.value.rproxy_fe }}
  when: item.value.rproxy_certbot is defined and item.value.rproxy_certbot

И часть цикла:

- include_tasks: add_domain.yml
  loop: "{{ query('dict', net_containers|default({}))  }}"
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...