<commit_before>---
# Gathers facts (bind address) from servers not currently targeted.
# 'delegate_facts' is currently rather buggy in Ansible so this might not
# always work. Hence 'consul_gather_server_facts' defaults to 'no'.
- name: Gather facts from other servers
  setup:
  delegate_to: "{{ item }}"
  delegate_facts: true
  with_items: "{{ consul_servers | difference(play_hosts) }}"
  ignore_errors: true
  when: consul_gather_server_facts | bool

- name: Expose advertise_address(_wan) datacenter and node_role as facts
  set_fact:
    consul_advertise_address_wan: "{{ consul_advertise_address_wan }}"
    consul_advertise_address: "{{ consul_advertise_address }}"
    consul_bind_address: "{{ consul_bind_address }}"
    consul_datacenter: "{{ consul_datacenter }}"
    consul_node_role: "{{ consul_node_role }}"

- name: Read bootstrapped state
  stat:
    path: "{{ consul_bootstrap_state }}"
  register: bootstrap_state
  ignore_errors: true
  tags: always

- name: Include user and group settings
  import_tasks: user_group.yml

- name: Include directory settings
  import_tasks: dirs.yml

- name: Check for existing Consul binary
  stat:
    path: "{{ consul_binary }}"
  register: consul_binary_installed

- name: Calculate whether to install consul binary
  set_fact:
    consul_install_binary: "{{ consul_install_upgrade | bool or not consul_binary_installed.stat.exists }}"

- name: Install OS packages and consul - locally
  include_tasks: install.yml
  when:
    - consul_install_binary | bool
    - not consul_install_remotely | bool

- name: Install OS packages and consul - remotely
  include_tasks: install_remote.yml
  when:
    - consul_install_binary | bool
    - consul_install_remotely | bool

# XXX: Individual gossip tasks are deprecated and need to be removed
# - include_tasks: ../tasks/encrypt_gossip.yml
- block:
    - block:
        - name: Check for gossip encryption key on previously boostrapped server
          slurp:
            src: "{{ consul_config_path }}/config.json"
          register: consul_config_b64
          ignore_errors: true

        - name: Deserialize existing configuration
          set_fact:
            consul_config: "{{ consul_config_b64.content | b64decode | from_json }}"
          when: consul_config_b64.content is defined

        - name: Save gossip encryption key from existing configuration
          set_fact:
            consul_raw_key: "{{ consul_config.encrypt }}"
          when: consul_config is defined

      no_log: true
      when:
        - consul_raw_key is not defined
        - bootstrap_state.stat.exists | bool
        - inventory_hostname in consul_servers

    # Key provided by extra vars or the above block
    - name: Write gossip encryption key locally for use with new servers
      copy:
        content: "{{ consul_raw_key }}"
        dest: '/tmp/consul_raw.key'
      become: false
      vars:
        ansible_become: false
      no_log: true
      register: consul_local_key
      delegate_to: localhost
      changed_when: false
      when: consul_raw_key is defined

    # Generate new key if none was found
    - block:
        - name: Generate gossip encryption key
          shell: "PATH={{ consul_bin_path }}:$PATH consul keygen"
          register: consul_keygen

        - name: Write key locally to share with other nodes
          copy:
            content: "{{ consul_keygen.stdout }}"
            dest: '/tmp/consul_raw.key'
          become: false
          vars:
            ansible_become: false
          delegate_to: localhost

      no_log: true
      run_once: true
      when:
        - not consul_local_key.changed
        - not bootstrap_state.stat.exists | bool

    - name: Read gossip encryption key for servers that require it
      set_fact:
        consul_raw_key: "{{ lookup('file', '/tmp/consul_raw.key') }}"
      no_log: true
      when:
        - consul_raw_key is not defined

    - name: Delete gossip encryption key file
      file:
        path: '/tmp/consul_raw.key'
        state: absent
      become: false
      vars:
        ansible_become: false
      run_once: true
      delegate_to: localhost
      changed_when: false
  no_log: true
  when:
    - consul_encrypt_enable

- name: Create ACL configuration
  include_tasks: acl.yml
  when: consul_acl_enable | bool

- name: Create Consul configuration
  import_tasks: config.yml

- name: Create TLS configuration
  include_tasks: tls.yml
  when: consul_tls_enable | bool

- name: Create syslog configuration
  import_tasks: syslog.yml

- name: Create BSD init script
  template:
    src: consul_bsdinit.j2
    dest: /etc/rc.d/consul
    owner: root
    group: wheel
    mode: 0755
  when: ansible_os_family == "FreeBSD"

- name: Create SYSV init script
  template:
    src: consul_sysvinit.j2
    dest: /etc/init.d/consul
    owner: root<commit_msg>🐛 Fix for #322 evaluating consul_encrypt_enable as a bare variable.<commit_after>---
# Gathers facts (bind address) from servers not currently targeted.
# 'delegate_facts' is currently rather buggy in Ansible so this might not
# always work. Hence 'consul_gather_server_facts' defaults to 'no'.
- name: Gather facts from other servers
  setup:
  delegate_to: "{{ item }}"
  delegate_facts: true
  with_items: "{{ consul_servers | difference(play_hosts) }}"
  ignore_errors: true
  when: consul_gather_server_facts | bool

- name: Expose advertise_address(_wan) datacenter and node_role as facts
  set_fact:
    consul_advertise_address_wan: "{{ consul_advertise_address_wan }}"
    consul_advertise_address: "{{ consul_advertise_address }}"
    consul_bind_address: "{{ consul_bind_address }}"
    consul_datacenter: "{{ consul_datacenter }}"
    consul_node_role: "{{ consul_node_role }}"

- name: Read bootstrapped state
  stat:
    path: "{{ consul_bootstrap_state }}"
  register: bootstrap_state
  ignore_errors: true
  tags: always

- name: Include user and group settings
  import_tasks: user_group.yml

- name: Include directory settings
  import_tasks: dirs.yml

- name: Check for existing Consul binary
  stat:
    path: "{{ consul_binary }}"
  register: consul_binary_installed

- name: Calculate whether to install consul binary
  set_fact:
    consul_install_binary: "{{ consul_install_upgrade | bool or not consul_binary_installed.stat.exists }}"

- name: Install OS packages and consul - locally
  include_tasks: install.yml
  when:
    - consul_install_binary | bool
    - not consul_install_remotely | bool

- name: Install OS packages and consul - remotely
  include_tasks: install_remote.yml
  when:
    - consul_install_binary | bool
    - consul_install_remotely | bool

# XXX: Individual gossip tasks are deprecated and need to be removed
# - include_tasks: ../tasks/encrypt_gossip.yml
- block:
    - block:
        - name: Check for gossip encryption key on previously boostrapped server
          slurp:
            src: "{{ consul_config_path }}/config.json"
          register: consul_config_b64
          ignore_errors: true

        - name: Deserialize existing configuration
          set_fact:
            consul_config: "{{ consul_config_b64.content | b64decode | from_json }}"
          when: consul_config_b64.content is defined

        - name: Save gossip encryption key from existing configuration
          set_fact:
            consul_raw_key: "{{ consul_config.encrypt }}"
          when: consul_config is defined

      no_log: true
      when:
        - consul_raw_key is not defined
        - bootstrap_state.stat.exists | bool
        - inventory_hostname in consul_servers

    # Key provided by extra vars or the above block
    - name: Write gossip encryption key locally for use with new servers
      copy:
        content: "{{ consul_raw_key }}"
        dest: '/tmp/consul_raw.key'
      become: false
      vars:
        ansible_become: false
      no_log: true
      register: consul_local_key
      delegate_to: localhost
      changed_when: false
      when: consul_raw_key is defined

    # Generate new key if none was found
    - block:
        - name: Generate gossip encryption key
          shell: "PATH={{ consul_bin_path }}:$PATH consul keygen"
          register: consul_keygen

        - name: Write key locally to share with other nodes
          copy:
            content: "{{ consul_keygen.stdout }}"
            dest: '/tmp/consul_raw.key'
          become: false
          vars:
            ansible_become: false
          delegate_to: localhost

      no_log: true
      run_once: true
      when:
        - not consul_local_key.changed
        - not bootstrap_state.stat.exists | bool

    - name: Read gossip encryption key for servers that require it
      set_fact:
        consul_raw_key: "{{ lookup('file', '/tmp/consul_raw.key') }}"
      no_log: true
      when:
        - consul_raw_key is not defined

    - name: Delete gossip encryption key file
      file:
        path: '/tmp/consul_raw.key'
        state: absent
      become: false
      vars:
        ansible_become: false
      run_once: true
      delegate_to: localhost
      changed_when: false
  no_log: true
  when:
    - consul_encrypt_enable | bool

- name: Create ACL configuration
  include_tasks: acl.yml
  when: consul_acl_enable | bool

- name: Create Consul configuration
  import_tasks: config.yml

- name: Create TLS configuration
  include_tasks: tls.yml
  when: consul_tls_enable | bool

- name: Create syslog configuration
  import_tasks: syslog.yml

- name: Create BSD init script
  template:
    src: consul_bsdinit.j2
    dest: /etc/rc.d/consul
    owner: root
    group: wheel
    mode: 0755
  when: ansible_os_family == "FreeBSD"

- name: Create SYSV init script
  template:
    src: consul_sysvinit.j2
    dest: /etc/init.d/consul
    owner: root
{'fraction_non_alphanumeric': 0.07712029535432263, 'fraction_numerical': 0.003179161111680853, 'mean_word_length': 2.781310585498255, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '293045', 'n_tokens_mistral': 3069, 'n_tokens_neox': 2966, 'n_words': 853}