| 1 |
--- |
| 2 |
- name: Converge |
| 3 |
hosts: all |
| 4 |
gather_facts: false |
| 5 |
vars: |
| 6 |
ca_cert_path: "{{ lookup('env', 'ca_cert_path') }}" |
| 7 |
https_cert_name: "{{ lookup('env', 'https_cert_name') }}" |
| 8 |
import_cert_path: "{{ lookup('env', 'path_for_import_cert') }}" |
| 9 |
export_cert_path: "{{ lookup('env', 'path_for_export_cert') }}" |
| 10 |
cert_export_path: "{{ lookup('env', 'path_for_import_cert') }}" |
| 11 |
|
| 12 |
tasks: |
| 13 |
- name: Set fact for mode |
| 14 |
ansible.builtin.set_fact: |
| 15 |
per_mode: "0755" |
| 16 |
|
| 17 |
- name: Install OpenSSL |
| 18 |
ansible.builtin.package: |
| 19 |
name: openssl |
| 20 |
state: present |
| 21 |
check_mode: false |
| 22 |
|
| 23 |
- name: Create Directory |
| 24 |
ansible.builtin.file: |
| 25 |
path: "{{ cert_export_path }}" |
| 26 |
state: directory |
| 27 |
mode: "{{ per_mode }}" |
| 28 |
register: idrac_certificate_created_directory |
| 29 |
check_mode: false |
| 30 |
|
| 31 |
- name: Generate CA private key |
| 32 |
ansible.builtin.command: > |
| 33 |
openssl genpkey -algorithm RSA \ |
| 34 |
-out "{{ export_cert_path }}"ca_key.pem \ |
| 35 |
-aes-256-cbc -pass pass:pass_123 |
| 36 |
args: |
| 37 |
creates: /tmp/idrac_certificate/ca_key.pem |
| 38 |
check_mode: false |
| 39 |
|
| 40 |
- name: Generate self-signed CA certificate |
| 41 |
ansible.builtin.command: > |
| 42 |
openssl req -x509 -new -key "{{ export_cert_path }}"ca_key.pem -sha256 \ |
| 43 |
-days 3650 -out "{{ export_cert_path }}"ca_cert.pem -passin \ |
| 44 |
pass:pass_123 \ |
| 45 |
-subj "/C=IN/ST=Karnataka/L=Bengaluru/O=Development \ |
| 46 |
/OU=Dev/CN=sample.domain.com" |
| 47 |
args: |
| 48 |
creates: /tmp/idrac_certificate/ca_cert.pem |
| 49 |
check_mode: false |
| 50 |
|
| 51 |
- name: Generate HTTPS CSR signing request |
| 52 |
ansible.builtin.import_role: |
| 53 |
name: idrac_certificate |
| 54 |
vars: |
| 55 |
hostname: "{{ lookup('env', 'IDRAC_IP') }}" |
| 56 |
username: "{{ lookup('env', 'IDRAC_USER') }}" |
| 57 |
password: "{{ lookup('env', 'IDRAC_PASSWORD') }}" |
| 58 |
validate_certs: false |
| 59 |
command: "generate_csr" |
| 60 |
certificate_type: "HTTPS" |
| 61 |
certificate_path: "{{ cert_export_path }}" |
| 62 |
cert_params: |
| 63 |
common_name: "sample.domain.com" |
| 64 |
organization_unit: "OrgUnit" |
| 65 |
locality_name: "Bangalore" |
| 66 |
state_name: "Karnataka" |
| 67 |
country_code: "IN" |
| 68 |
email_address: "[email protected]" |
| 69 |
organization_name: "OrgName" |
| 70 |
subject_alt_name: |
| 71 |
- "hostname1.chassis.com" |
| 72 |
|
| 73 |
- name: Fetch the single file name from a directory |
| 74 |
ansible.builtin.find: |
| 75 |
paths: "{{ cert_export_path }}" |
| 76 |
recurse: false |
| 77 |
file_type: file |
| 78 |
patterns: "*.txt" |
| 79 |
register: found_files |
| 80 |
|
| 81 |
- name: Set the file name as a variable |
| 82 |
ansible.builtin.set_fact: |
| 83 |
file_name: "{{ found_files.files[-1].path }}" |
| 84 |
|
| 85 |
- name: Setting up HTTPS CSR certificate path for exported file |
| 86 |
ansible.builtin.stat: |
| 87 |
path: "{{ idrac_certificate_out.certificate_path }}" |
| 88 |
register: csr_cert_file |
| 89 |
no_log: true |
| 90 |
|
| 91 |
- name: Verifying HTTPS generate CSR certificate |
| 92 |
ansible.builtin.assert: |
| 93 |
that: |
| 94 |
- csr_cert_file.stat.exists |
| 95 |
- not idrac_certificate_out.changed |
| 96 |
- not idrac_certificate_out.failed |
| 97 |
- idrac_certificate_out.msg == "Successfully performed |
| 98 |
the 'generate_csr' certificate operation." |
| 99 |
|
| 100 |
- name: Delete ca_cert.pem if it exists |
| 101 |
ansible.builtin.file: |
| 102 |
path: /tmp/idrac_certificate/https_cert.pem |
| 103 |
state: absent |
| 104 |
check_mode: false |
| 105 |
|
| 106 |
- name: Convert CSR from txt to pem format |
| 107 |
ansible.builtin.command: |
| 108 |
cmd: cp "{{ file_name }}" "{{ export_cert_path }}"https_cert.pem |
| 109 |
args: |
| 110 |
creates: /tmp/idrac_certificate/https_cert.pem |
| 111 |
check_mode: false |
| 112 |
|
| 113 |
- name: Delete ca_cert.pem if it exists. |
| 114 |
ansible.builtin.file: |
| 115 |
path: /tmp/idrac_certificate/idrac_cert.pem |
| 116 |
state: absent |
| 117 |
check_mode: false |
| 118 |
|
| 119 |
- name: Sign CSR with OpenSSL |
| 120 |
ansible.builtin.command: > |
| 121 |
openssl x509 -req -in "{{ export_cert_path }}"https_cert.pem \ |
| 122 |
-CA /tmp/idrac_certificate/ca_cert.pem \ |
| 123 |
-CAkey /tmp/idrac_certificate/ca_key.pem -CAcreateserial \ |
| 124 |
-out /tmp/idrac_certificate/idrac_cert.pem -days 365 -sha256 \ |
| 125 |
-passin pass:pass_123 |
| 126 |
args: |
| 127 |
creates: /tmp/idrac_certificate/idrac_cert.pem |
| 128 |
check_mode: false |
| 129 |
|
| 130 |
- name: Import HTTPS certificate |
| 131 |
ansible.builtin.import_role: |
| 132 |
name: dellemc.openmanage.idrac_certificate |
| 133 |
vars: |
| 134 |
hostname: "{{ lookup('env', 'IDRAC_IP') }}" |
| 135 |
username: "{{ lookup('env', 'IDRAC_USER') }}" |
| 136 |
password: "{{ lookup('env', 'IDRAC_PASSWORD') }}" |
| 137 |
validate_certs: false |
| 138 |
command: "import" |
| 139 |
reset: true |
| 140 |
certificate_type: "HTTPS" |
| 141 |
certificate_path: "{{ import_cert_path }}/idrac_cert.pem" |
| 142 |
|
| 143 |
- name: Waiting for idrac readiness |
| 144 |
ansible.builtin.wait_for: |
| 145 |
timeout: 30 |
| 146 |
when: |
| 147 |
- not ansible_check_mode |
| 148 |
- idrac_certificate_out is defined |
| 149 |
- idrac_certificate_out.changed |
| 150 |
|
| 151 |
- name: Asserting operation with check mode. |
| 152 |
ansible.builtin.assert: |
| 153 |
that: idrac_certificate_out.msg == "Changes found to be applied." |
| 154 |
when: ansible_check_mode |
| 155 |
|
| 156 |
- name: Asserting operation with Normal/Idempotence mode. |
| 157 |
ansible.builtin.assert: |
| 158 |
that: idrac_certificate_out.msg == "Successfully performed the |
| 159 |
'import' certificate operation.iDRAC |
| 160 |
has been reset successfully." |
| 161 |
when: not ansible_check_mode and idrac_certificate_out.changed |
| 162 |
|
| 163 |
- name: Export a custom certificate |
| 164 |
ansible.builtin.import_role: |
| 165 |
name: dellemc.openmanage.idrac_certificate |
| 166 |
vars: |
| 167 |
hostname: "{{ lookup('env', 'IDRAC_IP') }}" |
| 168 |
username: "{{ lookup('env', 'IDRAC_USER') }}" |
| 169 |
password: "{{ lookup('env', 'IDRAC_PASSWORD') }}" |
| 170 |
validate_certs: false |
| 171 |
command: "export" |
| 172 |
certificate_type: "HTTPS" |
| 173 |
certificate_path: "{{ export_cert_path }}" |
| 174 |
when: not ansible_check_mode |
| 175 |
|
| 176 |
- name: Setting up HTTPS certificate path for exported file |
| 177 |
when: idrac_certificate_out is defined |
| 178 |
and idrac_certificate_out.certificate_path is defined |
| 179 |
ansible.builtin.stat: |
| 180 |
path: "{{ idrac_certificate_out.certificate_path }}" |
| 181 |
register: https_cert_file |
| 182 |
no_log: true |
| 183 |
|
| 184 |
- name: Asserting operation with Normal/Idempotence mode |
| 185 |
ansible.builtin.assert: |
| 186 |
that: |
| 187 |
- https_cert_file.stat.exists |
| 188 |
- not idrac_certificate_out.changed |
| 189 |
- not idrac_certificate_out.failed |
| 190 |
- idrac_certificate_out.msg == "Successfully performed the |
| 191 |
'export' certificate operation." |
| 192 |
when: not ansible_check_mode and not idrac_certificate_out.changed |
| 193 |
|
| 194 |
- name: Deleting the directory |
| 195 |
ansible.builtin.include_tasks: |
| 196 |
file: ../__delete_directory.yml |