Creating the Ansible Play
Steps to Use Ansible Vault for Credentials
Create an Ansible Vault file to store your credentials:Create a YAML file (e.g.,vault.yml) to store your credentials and encrypt it with Ansible Vault.yamlansible_user:Brianansible_password:usarmy7584ansible_port:5985ansible_connection:winrmdomain_user:globomantics\brian.dorr.admindomain_password:iloveJESUS12#$Encrypt the file using Ansible Vault:shansible-vault encrypt vault.ymlModify your inventory file to reference the encrypted variables:Yourhosts.inifile should look like this:ini[windows]10.1.25.10 10.1.25.17 10.1.25.38 10.1.70.15Create a playbook to include the vaulted variables and use them:Update yourrename_and_join_domain.ymlplaybook to include the vaulted variables:yaml-name:Renamecomputersandjoindomainhosts:windowsgather_facts:novars_files:-vault.ymltasks:-name:Checkcurrentcomputernamewin_shell:"(Get-WmiObject -Class Win32_ComputerSystem).Name"register:current_computer_namevars:ansible_user:"{{ ansible_user }}"ansible_password:"{{ ansible_password }}"ansible_port:"{{ ansible_port }}"ansible_connection:"{{ ansible_connection }}"-name:Renamecomputerifnecessarywin_shell:| $currentComputerName = '{{ current_computer_name.stdout }}'; $expectedComputerName = '{{ expected_computer_name }}'; if ($currentComputerName -ne $expectedComputerName) { Rename-Computer -NewName $expectedComputerName -Force -Restart; } else { Write-Output 'Computer name is already correct'; }args:executable:powershellwhen:current_computer_name.stdout!=expected_computer_nameregister:rename_resultignore_errors:yes-name:Waitforsystemtocomebackafterrenamewin_wait_for_connection:delay:30when:rename_resultischanged-name:Joindomainifnotalreadyjoinedwin_shell:| $domain = 'globomantics.com'; $password = ConvertTo-SecureString '{{ domain_password }}' -AsPlainText -Force; $credential = New-Object System.Management.Automation.PSCredential('{{ domain_user }}', $password); if (-not (Get-WmiObject -Class Win32_ComputerSystem).PartOfDomain) { Add-Computer -DomainName $domain -Credential $credential -OUPath 'OU=Windows 10 VMs,OU=Globomantics Computers,DC=globomantics,DC=com' -Force -Restart -PassThru | Out-Null; Write-Output 'Attempted to join domain'; } else { Write-Output 'Already part of the domain'; }args:executable:powershellregister:join_resultignore_errors:yes-name:Waitforsystemtocomebackafterdomainjoinwin_wait_for_connection:delay:30when:join_resultischanged-name:Verifydomainmembershipwin_shell:| if ((Get-WmiObject -Class Win32_ComputerSystem).PartOfDomain) { Write-Output 'Successfully joined the domain'; } else { Write-Output 'Failed to join the domain'; }args:executable:powershellRun the Ansible Playbook with the Vault Password:When running the playbook, provide the vault password using the--ask-vault-passoption:shansible-playbook -i hosts.ini rename_and_join_domain.yml --ask-vault-pass
Explanation:
Vault File:vault.ymlcontains sensitive information and is encrypted with Ansible Vault.
Inventory File:hosts.inilists the Windows hosts without sensitive information.
Playbook:The playbook references the vaulted variables usingvars_files.Variables likeansible_user,ansible_password, etc., are loaded from the vault and used in tasks.
This setup ensures that your sensitive credentials are stored securely and only accessible when running the playbook with the correct vault password.