Testing the Custom Configuration
Now that we have our custom configuration in place, let's create a simple playbook to test it. This will help us understand how our configuration changes affect Ansible's behavior.
Create a new file named test_config.yml
in the /home/labex/project
directory:
nano /home/labex/project/test_config.yml
Update the content as follows:
---
- name: Test Custom Configuration
hosts: all
tasks:
- name: Display remote user
debug:
msg: "Connected as user: {{ ansible_user }}"
- name: Display privilege escalation info
debug:
msg: "Privilege escalation is {{ 'enabled' if ansible_become | default(false) else 'disabled' }}"
- name: Show Ansible configuration
debug:
msg: "Inventory file: {{ lookup('config', 'DEFAULT_HOST_LIST') }}"
- name: Check if become is enabled in ansible.cfg
command: grep "become = True" /home/labex/project/ansible.cfg
register: become_check
changed_when: false
failed_when: false
- name: Display become setting from ansible.cfg
debug:
msg: "Become is {{ 'enabled' if become_check.rc == 0 else 'disabled' }} in ansible.cfg"
This updated playbook makes the following changes:
- We've added a default value for
ansible_become
to prevent the undefined variable error.
- We've added two new tasks that check the
ansible.cfg
file directly for the become
setting, providing a more accurate representation of your configuration.
Now, let's run the updated playbook:
ansible-playbook /home/labex/project/test_config.yml
This should run without errors and provide you with information about your Ansible configuration.
Additionally, let's address the deprecation warning by updating our ansible.cfg
file:
nano /home/labex/project/ansible.cfg
Add the following line under the [defaults]
section:
interpreter_python = /usr/bin/python3
Your ansible.cfg
file should now look something like this:
[defaults]
inventory = /home/labex/project/inventory
remote_user = labex
host_key_checking = False
stdout_callback = yaml
interpreter_python = /usr/bin/python3
[privilege_escalation]
become = True
become_method = sudo
become_user = root
become_ask_pass = False
Save and exit the editor.
Now, when you run the playbook again:
ansible-playbook /home/labex/project/test_config.yml
You should see the output without the deprecation warning, and it should correctly display your configuration settings.