# 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: Fetch idrac_username index
  community.general.idrac_redfish_info:
    category: Manager
    command: GetManagerAttributes
    baseuri: "{{ inventory_hostname }}"
    username: "{{ idrac_username }}"
    password: "{{ idrac_password }}"
  register: idrac_manager_info

- name: Set idrac_attributes
  set_fact:
    idrac_attributes: "{{ idrac_manager_info.redfish_facts.entries | selectattr('Id', 'defined') | selectattr('Id', 'equalto', 'iDRACAttributes') }}"

- name: Enable 2FA
  community.general.idrac_redfish_config:
    category: Manager
    command: SetManagerAttributes
    resource_id: iDRAC.Embedded.1
    manager_attributes:
      Users.2.EmailAddress: "{{ use_email_address_2fa }}"
      Users.2.Simple2FA: "Enabled"
      Users.2.UseEmail: "Enabled"
    baseuri: "{{ inventory_hostname }}"
    username: "{{ idrac_username }}"
    password: "{{ idrac_password }}"
  register: enable_2fa
  when: idrac_attributes[0].Attributes['Users.2.UserName'] == idrac_username

- name: 2FA skip status
  debug:
    msg: "{{ enable2fa_skip_msg }}"
  when: idrac_attributes[0].Attributes['Users.2.UserName'] != idrac_username

- name: 2FA status
  debug:
    msg: "{{ enable_2fa_msg }}"
  when: 
    - idrac_attributes[0].Attributes['Users.2.UserName'] == idrac_username
    - not enable_2fa.failed