If you have hardcoded your host credentials in your inventory file, then it is strongly advised to encrypt that file with proper passcode.

In this example, we will see how to do that.

1. Check the inventory file:


$ cat preprod_hosts
linux_host1 ansible_host=192.289.86.60 ansible_connection=ssh ansible_user=root ansible_ssh_pass=dbatest@123
linux_host2 ansible_host=192.289.86.61 ansible_connection=ssh ansible_user=root ansible_ssh_pass=dbatest@123
linux_host3 ansible_host=192.289.86.62 ansible_connection=ssh ansible_user=root ansible_ssh_pass=dbatest@123

2. Now encrypt the inventory file ( preprod_hosts)


$ ansible-vault encrypt preprod_hosts
New Vault password:                  --- >>> enter your password 
Confirm New Vault password:
Encryption successful

3. Check whether file has been encrypted or not.


$ cat preprod_hosts
$ANSIBLE_VAULT;1.1;AES256
63343930303032363031346230663937323731633463356662363430303735653734383131306233
6635623765633634343730326638623737396462383233340a616565376139316664613061313831
34373864336461363134313831653437393035653530333064383735353834386534373639373534
3838623933323032380a373536653935363535343762616535653638656636613739636437633638
66336433633532646437366161356537616536666630343362363132353331656464636266333136
61626264343463623963636239616638323733646366313561623765376663373664613135353639
61613234346633366231363432313365366165343261363137653531373230313261633233623263
30633062643861633433383937363034356336646362626632643332623538326630383335383931
32396566353837633535373861633761356239353034306238356634343761633437366263653430
61643565333838303464366535396665386563333166353236653735356633393138623834633937
33626564333733396537623033343636373035343537356466393935643462363064656162303038
33383631646664396630323463613966333062623831393135646138306631666234346335323039
35656465393632363537343136643562653230626366393938313631346633376432316533366638
36343933636266343633373839396261323664646538633463336134663632333134363962376532
63393536646636303235313132623539323531363532306462373538663433313331643135393537
65633266666563613538636130343238663136356664666633343835323163333733336166626362
62323834393461313735316231623663646132623938633264386236323039356134363462343834
65383131376366323366363763313562666166323235366233353061613933663038393537636133
62356538613132303461373063386638616463643262636564373062376639313366343931636261
34386137373936646535373562303139346433323134613039653739376362623530653038346337
3664

4. The same can be decrypted using below syntax:


$ ansible-vault decrypt preprod_hosts
Vault password:
Decryption successful

$cat preprod_hosts
linux_host1 ansible_host=192.289.86.60 ansible_connection=ssh ansible_user=root ansible_ssh_pass=dbatest@123
linux_host2 ansible_host=192.289.86.61 ansible_connection=ssh ansible_user=root ansible_ssh_pass=dbatest@123
linux_host3 ansible_host=192.289.86.62 ansible_connection=ssh ansible_user=root ansible_ssh_pass=dbatest@123