Project setup
Creating the directory structure for a basic Ansible configuration
Executed:
aadmin@uvm0:~$ mkdir -p code/it-wiz/devops/ansible/projects/host_vars && cd code/it-wiz/devops/ansible/projects
aadmin@uvm0:~/code/it-wiz/devops/ansible/projects$ touch ansible.cfg hosts && cd host_vars
aadmin@uvm0:~/code/it-wiz/devops/ansible/projects/host_vars$ touch uvm26.yml uvm27.yml
Result
The following directory structure got created:
- uvm26.yml
- uvm27.yml
- ansible.cfg
- hosts
Populating the configuration files
ansible.cfg
Brought ansible.cfg to the following form:
[defaults]
inventory = hosts
host_key_checking = false
interpreter_python=/usr/bin/python3.12
[colors]
error = bright red
hosts
Brought hosts to the following form:
[ubuntu]
uvm26
uvm27
[ubuntu:vars]
ansible_become=true
host_vars/uvm26.yml
Brought host_vars/uvm26.yml to the following form:
ansible_user: aadmin
ansible_become_pass: apass26
host_vars/uvm26.yml
Brought host_vars/uvm27.yml to the following form:
ansible_user: aadmin
ansible_become_pass: apass27
Encrypting the files in the host_vars directory
Executed:
aadmin@uvm0:~/code/it-wiz/devops/ansible/projects/host_vars$ ansible-vault encrypt uvm26.yml --ask-vault-pass
aadmin@uvm0:~/code/it-wiz/devops/ansible/projects/host_vars$ ansible-vault encrypt uvm27.yml --ask-vault-pass
Results
The uvm26.yml and uvm26.yml files got encrypted.
Note
This example used the same Ansible Vault password for the encryption of both files.
Testing the new configuration
Executed:
aadmin@uvm0:~/code/it-wiz/devops/ansible/projects$ ansible all --one-line --module-name ping --ask-vault-pass
Vault pasword: <typed my vault password>
uvm27 | SUCCESS => {"changed": false,"ping": "pong"}
uvm26 | SUCCESS => {"changed": false,"ping": "pong"}
Result
The new configuration worked and Ansible was ready to use.
Explanations
This was an ad-hoc Ansible command, in which ping was the short name of the ansible.builtin.ping module.