# Copyright 2021 Dell Inc. or its subsidiaries. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
---

- name: Check if {{ login_input_filename }} file is encrypted
  command: cat {{ login_input_filename }}
  changed_when: false
  no_log: true
  register: config_content
  run_once: true

- name: Decrpyt {{ login_input_filename }}
  command: >-
    ansible-vault decrypt {{ login_input_filename }}
    --vault-password-file {{ login_vault_filename }}
  when: "'$ANSIBLE_VAULT;' in config_content.stdout"
  changed_when: false
  run_once: true

- name: Include variable file {{ login_input_filename }}
  include_vars: "{{ login_input_filename }}"
  no_log: true
  run_once: true

- name: Encrypt {{ login_input_filename }}
  command: >-
    ansible-vault encrypt {{ login_input_filename }}
    --vault-password-file {{ login_vault_filename }}
  changed_when: false
  when: "'$ANSIBLE_VAULT;' in config_content.stdout"
  run_once: true

- name: Update {{ login_input_filename }} permission
  file:
    path: "{{ login_input_filename }}"
    mode: "{{ file_permission }}"