<commit_before>---
- block:
  - name: Load wordpress_sites.yml vars into <env>_sites vars
    include_vars:
      file: group_vars/{{ item }}/wordpress_sites.yml
      name: "{{ item }}_sites"
    with_items: "{{ envs_with_wp_sites }}"
    when: envs_with_wp_sites | count > 1

  - name: Fail if there are duplicate site keys within host's wordpress_sites
    fail:
      msg: >
        If you put multiple environments on `{{ inventory_hostname }}`, `wordpress_sites`
        must use different site keys per environment. Adjust the following site keys that
        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 }}"

  when:
    - env_groups | count > 1
    - validate_site_keys | default(true) | bool

- name: Validate wordpress_sites
  fail:
    msg: "{{ lookup('template', 'wordpress_sites.j2') }}"
  when: wordpress_sites.keys() | difference(vault_wordpress_sites.keys()) | count
  tags: [wordpress]

- name: Validate format of site_hosts
  fail:
    msg: "{{ lookup('template', 'site_hosts.j2') }}"
  with_dict: "{{ wordpress_sites }}"
  when: item.value.site_hosts | rejectattr('canonical', 'defined') | list | count
  tags: [letsencrypt, wordpress]

- name: Verify dict format for apt package component variables
  fail:
    msg: "{{ lookup('template', 'package_vars_wrong_format_msg.j2') }}"
  when: package_vars_wrong_format | count
  vars:
    package_vars:
      apt_packages_default: "{{ apt_packages_default }}"
      apt_packages_custom: "{{ apt_packages_custom }}"
      memcached_packages_default: "{{ memcached_packages_default }}"
      memcached_packages_custom: "{{ memcached_packages_custom }}"
      php_extensions_default: "{{ php_extensions_default }}"
      php_extensions_custom: "{{ php_extensions_custom }}"
      sshd_packages_default: "{{ sshd_packages_default }}"
      sshd_packages_custom: "{{ sshd_packages_custom }}"
    package_vars_wrong_format: "[{% for k,v in package_vars.items() | list if v | type_debug != 'dict' %}'{{ k }}',{% endfor %}]"
  tags: [sshd, memcached, php]

- name: Verify dict format for apt package combined variables
  fail:
    msg: "{{ lookup('template', 'package_vars_wrong_format_msg.j2') }}"
  when: package_vars_wrong_format | count
  vars:
    package_vars:
      apt_packages: "{{ apt_packages }}"
      memcached_packages: "{{ memcached_packages }}"
      php_extensions: "{{ php_extensions }}"
      sshd_packages: "{{ sshd_packages }}"
    package_vars_wrong_format: "[{% for k,v in package_vars.items() | list if v | type_debug != 'dict' %}'{{ k }}',{% endfor %}]"
  tags: [sshd, memcached, php]

- name: Validate Ubuntu version
  debug:
    msg: |
      Trellis is built for Ubuntu 18.04 Bionic as of https://github.com/roots/trellis/pull/992

      Your Ubuntu version is {{ ansible_distribution_version }} {{ ansible_distribution_release }}

      We recommend you re-create your server to get the best experience.

      Note: both of these methods will delete all your existing data. It's up to you to backup what's needed and restore it.

      Development via Vagrant: `vagrant destroy && vagrant up`

      Staging/Production: Create a new server with Ubuntu 18.04 and provision
  when: ansible_distribution_release != 'bionic'

- name: Check whether passlib is needed
  fail:
    msg: |
      Ansible on OS X requires python passlib module to create user password hashes

      sudo easy_install pip
      pip install passlib
  when: env != 'development' and darwin_without_passlib | default(false)
  run_once: true

- name: Retrieve local SSH client's settings per host
  set_fact:
    ssh_client_ciphers: "{{ lookup('pipe', 'ssh -ttG ' + ansible_host + ' | grep ciphers') }}"
    ssh_client_kex: "{{ lookup('pipe', 'ssh -ttG ' + ansible_host + ' | grep kexalgorithms') }}"
    ssh_client_macs: "{{ lookup('pipe', 'ssh -ttG ' + ansible_host + ' | grep macs') }}"
    ssh_client_host_key_algorithms: "{{ lookup('pipe', 'ssh -ttG ' + ansible_host + ' | grep hostkeyalgorithms') }}"
  when: openssh_6_8_plus and validate_ssh | default(true)
  tags: [sshd]

- name: Validate compatible settings between SSH client and server
  assert:
    that:
      - overlapping_ciphers | count
      - overlapping_kex | count
      - overlapping_macs | count
      - overlapping_host_keys | count
    msg: "{{ lookup('template', 'validate_ssh_msg.j2') }}"
  when: openssh_6_8_plus and validate_ssh | default(true)
  tags: [sshd]

- name: Update apt packages
  apt:
    update_cache: yes

- name: Checking essentials
  apt:
    name: "{{ item.key }}"
    state: "{{ item.value }}"
    cache_valid_time: "{{ apt_cache_valid_time }}"
  with_dict: "{{ apt_packages }}"

- name: Validate timezone variable
  stat:
    path: /usr/share/zoneinfo/{{ ntp_timezone }}
  register: timezone_path
  changed_when: false

- name: Explain timezone error
  fail:
    msg: "{{ ntp_timezone }} is not a valid timezone. For a list of valid timezones, check https://php.net/manual/en/timezones.php"
  when: not timezone_path.stat.exists

- name: Add myhostname to nsswitch.conf to ensure resolvable hostname
  lineinfile:
    backrefs: yes
    backup: yes
    dest: /etc/nsswitch.conf
    line: \1 myhostname
    regexp: ^(hosts\:((?!myhostname).)*)$
    state: present

- name: Generate SSH key for vagrant user
  user:
    name: vagrant
    generate_ssh_key: yes
  when: env == 'development'

- name: Retrieve SSH client IP
  block:
    - ipify_facts:
      delegate_to: localhost
      become: no
      when: env != 'development' and ssh_client_ip_lookup | default(true)
      tags: [fail2ban, ferm]
  rescue:
    - fail:
        msg: "External IP resolution failed. Check that your DNS servers are working. Try to disable DNSCrypt if you are using it."<commit_msg>Lint: Fix `[502] All tasks should be named`<commit_after>---
- block:
  - name: Load wordpress_sites.yml vars into <env>_sites vars
    include_vars:
      file: group_vars/{{ item }}/wordpress_sites.yml
      name: "{{ item }}_sites"
    with_items: "{{ envs_with_wp_sites }}"
    when: envs_with_wp_sites | count > 1

  - name: Fail if there are duplicate site keys within host's wordpress_sites
    fail:
      msg: >
        If you put multiple environments on `{{ inventory_hostname }}`, `wordpress_sites`
        must use different site keys per environment. Adjust the following site keys that
        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 }}"

  when:
    - env_groups | count > 1
    - validate_site_keys | default(true) | bool

- name: Validate wordpress_sites
  fail:
    msg: "{{ lookup('template', 'wordpress_sites.j2') }}"
  when: wordpress_sites.keys() | difference(vault_wordpress_sites.keys()) | count
  tags: [wordpress]

- name: Validate format of site_hosts
  fail:
    msg: "{{ lookup('template', 'site_hosts.j2') }}"
  with_dict: "{{ wordpress_sites }}"
  when: item.value.site_hosts | rejectattr('canonical', 'defined') | list | count
  tags: [letsencrypt, wordpress]

- name: Verify dict format for apt package component variables
  fail:
    msg: "{{ lookup('template', 'package_vars_wrong_format_msg.j2') }}"
  when: package_vars_wrong_format | count
  vars:
    package_vars:
      apt_packages_default: "{{ apt_packages_default }}"
      apt_packages_custom: "{{ apt_packages_custom }}"
      memcached_packages_default: "{{ memcached_packages_default }}"
      memcached_packages_custom: "{{ memcached_packages_custom }}"
      php_extensions_default: "{{ php_extensions_default }}"
      php_extensions_custom: "{{ php_extensions_custom }}"
      sshd_packages_default: "{{ sshd_packages_default }}"
      sshd_packages_custom: "{{ sshd_packages_custom }}"
    package_vars_wrong_format: "[{% for k,v in package_vars.items() | list if v | type_debug != 'dict' %}'{{ k }}',{% endfor %}]"
  tags: [sshd, memcached, php]

- name: Verify dict format for apt package combined variables
  fail:
    msg: "{{ lookup('template', 'package_vars_wrong_format_msg.j2') }}"
  when: package_vars_wrong_format | count
  vars:
    package_vars:
      apt_packages: "{{ apt_packages }}"
      memcached_packages: "{{ memcached_packages }}"
      php_extensions: "{{ php_extensions }}"
      sshd_packages: "{{ sshd_packages }}"
    package_vars_wrong_format: "[{% for k,v in package_vars.items() | list if v | type_debug != 'dict' %}'{{ k }}',{% endfor %}]"
  tags: [sshd, memcached, php]

- name: Validate Ubuntu version
  debug:
    msg: |
      Trellis is built for Ubuntu 18.04 Bionic as of https://github.com/roots/trellis/pull/992

      Your Ubuntu version is {{ ansible_distribution_version }} {{ ansible_distribution_release }}

      We recommend you re-create your server to get the best experience.

      Note: both of these methods will delete all your existing data. It's up to you to backup what's needed and restore it.

      Development via Vagrant: `vagrant destroy && vagrant up`

      Staging/Production: Create a new server with Ubuntu 18.04 and provision
  when: ansible_distribution_release != 'bionic'

- name: Check whether passlib is needed
  fail:
    msg: |
      Ansible on OS X requires python passlib module to create user password hashes

      sudo easy_install pip
      pip install passlib
  when: env != 'development' and darwin_without_passlib | default(false)
  run_once: true

- name: Retrieve local SSH client's settings per host
  set_fact:
    ssh_client_ciphers: "{{ lookup('pipe', 'ssh -ttG ' + ansible_host + ' | grep ciphers') }}"
    ssh_client_kex: "{{ lookup('pipe', 'ssh -ttG ' + ansible_host + ' | grep kexalgorithms') }}"
    ssh_client_macs: "{{ lookup('pipe', 'ssh -ttG ' + ansible_host + ' | grep macs') }}"
    ssh_client_host_key_algorithms: "{{ lookup('pipe', 'ssh -ttG ' + ansible_host + ' | grep hostkeyalgorithms') }}"
  when: openssh_6_8_plus and validate_ssh | default(true)
  tags: [sshd]

- name: Validate compatible settings between SSH client and server
  assert:
    that:
      - overlapping_ciphers | count
      - overlapping_kex | count
      - overlapping_macs | count
      - overlapping_host_keys | count
    msg: "{{ lookup('template', 'validate_ssh_msg.j2') }}"
  when: openssh_6_8_plus and validate_ssh | default(true)
  tags: [sshd]

- name: Update apt packages
  apt:
    update_cache: yes

- name: Checking essentials
  apt:
    name: "{{ item.key }}"
    state: "{{ item.value }}"
    cache_valid_time: "{{ apt_cache_valid_time }}"
  with_dict: "{{ apt_packages }}"

- name: Validate timezone variable
  stat:
    path: /usr/share/zoneinfo/{{ ntp_timezone }}
  register: timezone_path
  changed_when: false

- name: Explain timezone error
  fail:
    msg: "{{ ntp_timezone }} is not a valid timezone. For a list of valid timezones, check https://php.net/manual/en/timezones.php"
  when: not timezone_path.stat.exists

- name: Add myhostname to nsswitch.conf to ensure resolvable hostname
  lineinfile:
    backrefs: yes
    backup: yes
    dest: /etc/nsswitch.conf
    line: \1 myhostname
    regexp: ^(hosts\:((?!myhostname).)*)$
    state: present

- name: Generate SSH key for vagrant user
  user:
    name: vagrant
    generate_ssh_key: yes
  when: env == 'development'

- block:
    - name: Retrieve SSH client IP
      ipify_facts:
      delegate_to: localhost
      become: no
      when: env != 'development' and ssh_client_ip_lookup | default(true)
      tags: [fail2ban, ferm]
  rescue:
    - name: Fail when unable to retrieve SSH client IP
      fail:
        msg: "External IP resolution failed. Check that your DNS servers are working. Try to disable DNSCrypt if you are using it."
{'fraction_non_alphanumeric': 0.11725944073667315, 'fraction_numerical': 0.004477485849455098, 'mean_word_length': 3.579497098646035, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4524738', 'n_tokens_mistral': 4011, 'n_tokens_neox': 3762, 'n_words': 1061}