how to encrypt password in ansible :
all:
children:
host_servers:
vars:
ansible_user: root
ansible_ssh_pass: “123”
hosts:
server1:
ansible_host: 192.168.56.140
server2:
ansible_host: 10.30.232.4
server3:
ansible_host: 10.30.232.5
Assistant
Encrypting passwords in Ansible is crucial for maintaining secure practices, as leaving passwords in plaintext within your Ansible inventories can be a significant security risk. Ansible provides a feature called Ansible Vault to encrypt sensitive data, such as passwords, so that they aren't exposed in plain text.
To encrypt the ansible_ssh_pass variable using Ansible Vault, follow these steps:
Step 1: Create a Vault-encrypted Variable File
First, create a new file to store your encrypted password. For example, you can name this file vault_password.yml.
Use the following command to create the encrypted file with Ansible Vault:
code
ansible-vault create vault_password.yml
When prompted, enter a new Vault password that will be used to encrypt and decrypt this file.
Step 2: Enter the Encrypted Variable
Inside vault_password.yml, define the ansible_ssh_pass variable with the password you want to encrypt:
yaml
ansible_ssh_pass: 123
Save and exit the editor (for example, in vim, you'd press :wq).
Step 3: Modify the Inventory File to Use the Encrypted Variable
Remove the plaintext password from your inventory file. Instead of defining ansible_ssh_pass in the inventory, include the newly created encrypted file using vars_files. The updated inventory should look like this (with the plaintext password removed):
Step 4: Running Ansible Commands with Vault
When running an Ansible playbook, you need to provide the password for any Vault-encrypted files the playbook uses. You can do that in two ways:
Ask for the password at runtime:
code
ansible-playbook --ask-vault-pass playbook.yml
You'll be prompted to enter the Vault password you set up when encrypting the vault_password.yml file.
Use a password file (not recommended for sensitive environments since this file contains your Vault password in plaintext):
Ensure that the Vault password you use to encrypt/decrypt the data is stored and handled securely, and that the vault_password.yml file, as well as any playbook including this data, is managed with appropriate permissions and is not accessible to unauthorized users.