Skip to content

Commit

Permalink
Merge pull request #1416 from roots/update-loops
Browse files Browse the repository at this point in the history
Migrage with_X looping to `loop`
  • Loading branch information
swalkinshaw authored Aug 6, 2022
2 parents 0401078 + d55b18d commit c87f502
Show file tree
Hide file tree
Showing 33 changed files with 212 additions and 115 deletions.
2 changes: 1 addition & 1 deletion roles/common/tasks/disable_challenge_sites.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
file:
path: "{{ nginx_path }}/sites-enabled/letsencrypt-{{ item }}.conf"
state: absent
with_items: "{{ wordpress_sites.keys() | list }}"
loop: "{{ wordpress_sites.keys() | list }}"
notify: reload nginx
12 changes: 8 additions & 4 deletions roles/common/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
include_vars:
file: group_vars/{{ item }}/wordpress_sites.yml
name: "{{ item }}_sites"
with_items: "{{ envs_with_wp_sites }}"
loop: "{{ envs_with_wp_sites }}"
when: envs_with_wp_sites | count > 1

- name: Fail if there are duplicate site keys within host's wordpress_sites
Expand All @@ -15,7 +15,7 @@
are duplicated between the `{{ item.env_pair | join('` and `') }}` groups:
{{ item.site_keys | to_nice_yaml | indent(2) }}
when: item.site_keys | count
with_items: "{{ site_keys_by_env_pair }}"
loop: "{{ site_keys_by_env_pair }}"

when:
- env_groups | count > 1
Expand All @@ -30,7 +30,9 @@
- name: Validate format of site_hosts
fail:
msg: "{{ lookup('template', 'site_hosts.j2') }}"
with_dict: "{{ wordpress_sites }}"
loop: "{{ wordpress_sites | dict2items }}"
loop_control:
label: "{{ item.key }}"
when: item.value.site_hosts | rejectattr('canonical', 'defined') | list | count
tags: [letsencrypt, wordpress]

Expand Down Expand Up @@ -144,7 +146,9 @@
name: "{{ item.key }}"
state: "{{ item.value }}"
cache_valid_time: "{{ apt_cache_valid_time }}"
with_dict: "{{ apt_packages }}"
loop: "{{ apt_packages | dict2items }}"
loop_control:
label: "{{ item.key }}"

- name: Validate timezone variable
stat:
Expand Down
13 changes: 8 additions & 5 deletions roles/deploy/hooks/finalize-after.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@
changed_when: site_transient_theme_roots.stdout != ''
when: project.update_wp_theme_paths | default(update_wp_theme_paths | default(true)) | bool

- debug:
var: wp_template_root.results

- name: Update WP theme paths
command: >
wp option set {{ item[0].option }}
wp option set {{ item[0].item }}
{{ item[1] | regex_replace('.*' + deploy_helper.releases_path + '/[^/]*(.*)', deploy_helper.new_release_path + '\1') }}
{% if project.multisite.enabled | default(false) %} --url={{ item[1].split(' ')[0] }}{% endif %}
args:
chdir: "{{ deploy_helper.current_path }}"
when: project.update_wp_theme_paths | default(update_wp_theme_paths | default(true)) | bool
with_subelements:
- "[{% for result in wp_template_root.results %}{'option': '{{ result.item }}', 'stdout_lines': {{ result.stdout_lines | default ([]) | select('search', deploy_helper.releases_path) | list }}},{% endfor %}]"
- stdout_lines
loop: "{{ wp_template_root.results | subelements('stdout_lines', skip_missing=true) }}"
loop_control:
label: "{{ item[0].item }}"
when: project.update_wp_theme_paths | default(update_wp_theme_paths | default(true)) | bool and item[1] is match(deploy_helper.releases_path)

- name: Warn about updating network database.
debug:
Expand Down
2 changes: 1 addition & 1 deletion roles/deploy/hooks/finalize-before.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@
when:
- wp_installed.rc == 0
- project.update_wp_theme_paths | default(update_wp_theme_paths | default(true)) | bool
with_items:
loop:
- template_root
- stylesheet_root
20 changes: 13 additions & 7 deletions roles/deploy/tasks/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
path: "{{ item }}"
delegate_to: localhost
register: deploy_build_before_paths
with_items: "{{ deploy_build_before | default([]) }}"
loop: "{{ deploy_build_before | default([]) }}"

- include_tasks: "{{ include_path.item }}"
with_items: "{{ deploy_build_before_paths.results }}"
loop: "{{ deploy_build_before_paths.results }}"
loop_control:
loop_var: include_path
label: "{{ include_path.item }}"
when: include_path.stat.exists
tags: deploy-build-before

Expand All @@ -18,33 +19,38 @@
src: "{{ item.src }}"
dest: "{{ deploy_helper.new_release_path }}/{{ item.dest }}"
mode: "{{ item.mode | default('0644') }}"
with_items: "{{ project.project_templates | default(project_templates) }}"
loop: "{{ project.project_templates | default(project_templates) }}"
loop_control:
label: "{{ item.name }}"

- name: Check if project folders exist
stat:
path: "{{ deploy_helper.current_path }}/{{ item }}"
register: project_folder_paths
with_items: "{{ project.project_copy_folders | default(project_copy_folders) }}"
loop: "{{ project.project_copy_folders | default(project_copy_folders) }}"

- name: Copy project folders
copy:
src: "{{ deploy_helper.current_path }}/{{ item.item }}/"
dest: "{{ deploy_helper.new_release_path }}/{{ item.item }}"
remote_src: true
mode: 'preserve'
with_items: "{{ project_folder_paths.results }}"
loop: "{{ project_folder_paths.results }}"
loop_control:
label: "{{ item.item }}"
when: item.stat.exists

- name: Check if deploy_build_after scripts exist
stat:
path: "{{ item }}"
delegate_to: localhost
register: deploy_build_after_paths
with_items: "{{ deploy_build_after | default([]) }}"
loop: "{{ deploy_build_after | default([]) }}"

- include_tasks: "{{ include_path.item }}"
with_items: "{{ deploy_build_after_paths.results }}"
loop: "{{ deploy_build_after_paths.results }}"
loop_control:
loop_var: include_path
label: "{{ include_path.item }}"
when: include_path.stat.exists
tags: deploy-build-after
10 changes: 6 additions & 4 deletions roles/deploy/tasks/finalize.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
path: "{{ item }}"
delegate_to: localhost
register: deploy_finalize_before_paths
with_items: "{{ deploy_finalize_before | default([]) }}"
loop: "{{ deploy_finalize_before | default([]) }}"

- include_tasks: "{{ include_path.item }}"
with_items: "{{ deploy_finalize_before_paths.results }}"
loop: "{{ deploy_finalize_before_paths.results }}"
loop_control:
loop_var: include_path
label: "{{ include_path.item }}"
when: include_path.stat.exists
tags: deploy-finalize-before

Expand All @@ -26,12 +27,13 @@
path: "{{ item }}"
delegate_to: localhost
register: deploy_finalize_after_paths
with_items: "{{ deploy_finalize_after | default([]) }}"
loop: "{{ deploy_finalize_after | default([]) }}"

- include_tasks: "{{ include_path.item }}"
with_items: "{{ deploy_finalize_after_paths.results }}"
loop: "{{ deploy_finalize_after_paths.results }}"
loop_control:
loop_var: include_path
label: "{{ include_path.item }}"
when: include_path.stat.exists
tags: deploy-finalize-after

Expand Down
8 changes: 4 additions & 4 deletions roles/deploy/tasks/initialize.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
path: "{{ item }}"
delegate_to: localhost
register: deploy_initialize_before_paths
with_items: "{{ deploy_initialize_before | default([]) }}"
loop: "{{ deploy_initialize_before | default([]) }}"

- include_tasks: "{{ include_path.item }}"
with_items: "{{ deploy_initialize_before_paths.results }}"
loop: "{{ deploy_initialize_before_paths.results }}"
loop_control:
loop_var: include_path
when: include_path.stat.exists
Expand All @@ -24,10 +24,10 @@
path: "{{ item }}"
delegate_to: localhost
register: deploy_initialize_after_paths
with_items: "{{ deploy_initialize_after | default([]) }}"
loop: "{{ deploy_initialize_after | default([]) }}"

- include_tasks: "{{ include_path.item }}"
with_items: "{{ deploy_initialize_after_paths.results }}"
loop: "{{ deploy_initialize_after_paths.results }}"
loop_control:
loop_var: include_path
when: include_path.stat.exists
Expand Down
8 changes: 4 additions & 4 deletions roles/deploy/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
path: "{{ item }}"
delegate_to: localhost
register: deploy_before_paths
with_items: "{{ deploy_before | default([]) }}"
loop: "{{ deploy_before | default([]) }}"

- include_tasks: "{{ include_path.item }}"
with_items: "{{ deploy_before_paths.results }}"
loop: "{{ deploy_before_paths.results }}"
loop_control:
loop_var: include_path
when: include_path.stat.exists
Expand All @@ -25,10 +25,10 @@
path: "{{ item }}"
delegate_to: localhost
register: deploy_after_paths
with_items: "{{ deploy_after | default([]) }}"
loop: "{{ deploy_after | default([]) }}"

- include_tasks: "{{ include_path.item }}"
with_items: "{{ deploy_after_paths.results }}"
loop: "{{ deploy_after_paths.results }}"
loop_control:
loop_var: include_path
when: include_path.stat.exists
Expand Down
8 changes: 4 additions & 4 deletions roles/deploy/tasks/prepare.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
path: "{{ item }}"
delegate_to: localhost
register: deploy_prepare_before_paths
with_items: "{{ deploy_prepare_before | default([]) }}"
loop: "{{ deploy_prepare_before | default([]) }}"

- include_tasks: "{{ include_path.item }}"
with_items: "{{ deploy_prepare_before_paths.results }}"
loop: "{{ deploy_prepare_before_paths.results }}"
loop_control:
loop_var: include_path
when: include_path.stat.exists
Expand Down Expand Up @@ -59,10 +59,10 @@
path: "{{ item }}"
delegate_to: localhost
register: deploy_prepare_after_paths
with_items: "{{ deploy_prepare_after | default([]) }}"
loop: "{{ deploy_prepare_after | default([]) }}"

- include_tasks: "{{ include_path.item }}"
with_items: "{{ deploy_prepare_after_paths.results }}"
loop: "{{ deploy_prepare_after_paths.results }}"
loop_control:
loop_var: include_path
when: include_path.stat.exists
Expand Down
34 changes: 24 additions & 10 deletions roles/deploy/tasks/share.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
path: "{{ item }}"
delegate_to: localhost
register: deploy_share_before_paths
with_items: "{{ deploy_share_before | default([]) }}"
loop: "{{ deploy_share_before | default([]) }}"

- include_tasks: "{{ include_path.item }}"
with_items: "{{ deploy_share_before_paths.results }}"
loop: "{{ deploy_share_before_paths.results }}"
loop_control:
loop_var: include_path
label: "{{ include_path.item }}"
when: include_path.stat.exists
tags: deploy-share-before

Expand All @@ -18,55 +19,68 @@
path: "{{ deploy_helper.shared_path }}/{{ item.src }}"
state: directory
mode: "{{ item.mode | default('0755') }}"
with_items: "{{ project.project_shared_children | default(project_shared_children) }}"
loop: "{{ project.project_shared_children | default(project_shared_children) }}"
loop_control:
label: "{{ item.path }}"
when: item.type | default('directory') | lower == 'directory'

- name: Ensure shared sources are present -- files' parent directories
file:
path: "{{ deploy_helper.shared_path }}/{{ item.src | dirname }}"
state: directory
mode: '0755'
with_items: "{{ project.project_shared_children | default(project_shared_children) }}"
loop: "{{ project.project_shared_children | default(project_shared_children) }}"
loop_control:
label: "{{ item.path }}"
when: item.type | default('directory') | lower == 'file'

- name: Ensure shared sources are present -- files
file:
path: "{{ deploy_helper.shared_path }}/{{ item.src }}"
state: touch
mode: "{{ item.mode | default('0644') }}"
with_items: "{{ project.project_shared_children | default(project_shared_children) }}"
loop: "{{ project.project_shared_children | default(project_shared_children) }}"
loop_control:
label: "{{ item.path }}"
when: item.type | default('directory') | lower == 'file'

- name: Ensure parent directories for shared paths are present
file:
path: "{{ deploy_helper.new_release_path }}/{{ item.path | dirname }}"
mode: '0755'
state: directory
with_items: "{{ project.project_shared_children | default(project_shared_children) }}"
loop: "{{ project.project_shared_children | default(project_shared_children) }}"
loop_control:
label: "{{ item.path }}"

- name: Ensure shared paths are absent
file:
path: "{{ deploy_helper.new_release_path }}/{{ item.path }}"
state: absent
with_items: "{{ project.project_shared_children | default(project_shared_children) }}"
loop: "{{ project.project_shared_children | default(project_shared_children) }}"
loop_control:
label: "{{ item.path }}"

- name: Create shared symlinks
file:
path: "{{ deploy_helper.new_release_path }}/{{ item.path }}"
src: "{{ deploy_helper.shared_path }}/{{ item.src }}"
state: link
with_items: "{{ project.project_shared_children | default(project_shared_children) }}"
loop: "{{ project.project_shared_children | default(project_shared_children) }}"
loop_control:
label: "{{ item.path }}"

- name: Check if deploy_share_after scripts exist
stat:
path: "{{ item }}"
delegate_to: localhost
register: deploy_share_after_paths
with_items: "{{ deploy_share_after | default([]) }}"
loop: "{{ deploy_share_after | default([]) }}"

- include_tasks: "{{ include_path.item }}"
with_items: "{{ deploy_share_after_paths.results }}"
loop: "{{ deploy_share_after_paths.results }}"
loop_control:
loop_var: include_path
label: "{{ include_path.item }}"
when: include_path.stat.exists
tags: deploy-share-after
12 changes: 7 additions & 5 deletions roles/deploy/tasks/update.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
path: "{{ item }}"
delegate_to: localhost
register: deploy_update_before_paths
with_items: "{{ deploy_update_before | default([]) }}"
loop: "{{ deploy_update_before | default([]) }}"

- include_tasks: "{{ include_path.item }}"
with_items: "{{ deploy_update_before_paths.results }}"
loop: "{{ deploy_update_before_paths.results }}"
loop_control:
loop_var: include_path
when: include_path.stat.exists
Expand All @@ -19,7 +19,9 @@
key: "{{ item.key | default(omit) }}"
path: "{{ item.path | default(omit) }}"
state: "{{ item.state | default('present') }}"
with_items: "{{ known_hosts | default([]) }}"
loop: "{{ known_hosts | default([]) }}"
loop_control:
label: "{{ item.name }}"

- name: Clone project files
git:
Expand Down Expand Up @@ -56,10 +58,10 @@
path: "{{ item }}"
delegate_to: localhost
register: deploy_update_after_paths
with_items: "{{ deploy_update_after | default([]) }}"
loop: "{{ deploy_update_after | default([]) }}"

- include_tasks: "{{ include_path.item }}"
with_items: "{{ deploy_update_after_paths.results }}"
loop: "{{ deploy_update_after_paths.results }}"
loop_control:
loop_var: include_path
when: include_path.stat.exists
Expand Down
4 changes: 2 additions & 2 deletions roles/fail2ban/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
src: "{{ item }}.j2"
dest: /etc/fail2ban/{{ item }}
mode: '0644'
with_items:
loop:
- jail.local
- fail2ban.local
notify:
Expand Down Expand Up @@ -47,7 +47,7 @@
src: "{{ item }}"
dest: "/etc/fail2ban/filter.d/{{ item | basename | regex_replace('.j2$', '') }}"
mode: '0644'
with_items: "{{ fail2ban_filter_templates.files | map(attribute='path') | list | sort(True) }}"
loop: "{{ fail2ban_filter_templates.files | map(attribute='path') | list | sort(True) }}"
notify: restart fail2ban

- name: ensure fail2ban starts on a fresh reboot
Expand Down
Loading

0 comments on commit c87f502

Please sign in to comment.