Clone a private repository using the source control credential.
- name: Clone repository hosts: localhost tasks:
- git:
repo: "https://github.com/private/repo.git"
dest: "~/repo"
version: "main"
key_file: "~/.ssh/id_rsa"
Explanation:
Using git module with secure credentials fetches repositories for automated deployments or configurations.
Configure the inventory so that web1 has both IPv4 and IPv6 addresses and use them conditionally in a playbook.
- name: Use IPv4 or IPv6 hosts: web1
tasks:
- debug:
msg: "{{ ansible_ipv6_address if ansible_ipv6_address else ansible_host }}"
Explanation:
Defining multiple addresses and using conditional logic allows flexibility for dual-stack environments.
Set up notifications for job failures in Automation Controller.
2. Choose Type: Slack.
3. Configure Slack details and associate it with a job template.
4. Set the trigger to On Failure.
Explanation:
Notifications improve responsiveness by alerting users of job failures in real time.
Verify that an EE supports a specific Ansible version.
Explanation:
Checking the Ansible version inside the EE ensures compatibility with playbooks and modules requiring specific versions.
Run a playbook in an EE using Ansible Automation Controller.
2. Go to Execution Environments and create a new EE entry using the image registry.example.com/my_execution_env:1.0.
3. Associate the EE with a Job Template and run the playbook.
Explanation:
Configuring the EE in the Automation Controller ensures playbooks run consistently across environments.
Create an inventory to manage multiple environments like staging and production.
staging:
hosts:
web1:
ansible_host: 192.168.1.11 production:
hosts:
web1:
ansible_host: 192.168.1.20
Explanation:
Environment-specific inventories simplify deployments and maintenance across diverse infrastructure setups.
Configure a host db1 to use a specific username dbadmin and SSH key ~/.ssh/db_key. Ensure this is applied via host_vars.
echo "ansible_user: dbadmin" > host_vars/db1.yml
echo "ansible_ssh_private_key_file: ~/.ssh/db_key" >> host_vars/db1.yml
Explanation:
Using host_vars allows granular control of variables, such as user credentials and SSH keys, for individual hosts.
Use the Vault file to configure credentials for inventory hosts.
all:
hosts:
web1:
ansible_user: "{{ ansible_user }}"
ansible_password: "{{ ansible_password }}"
Run with:
ansible-playbook site.yml --vault-password-file vault_pass.txt
Explanation:
Integrating Vault variables into inventory files ensures secure use of sensitive credentials during task execution.
Use ansible_facts to display the OS distribution of web1.
tasks:
- debug:
var: ansible_distribution
Explanation:
Ansible facts provide detailed system information, enabling tailored configurations and playbook logic.
You are given access to a Git repository with the URL https://github.com/example/repo.git. Clone the repository to your local machine in the directory /home/user/projects.
git clone https://github.com/example/repo.git
Explanation:
Cloning a repository retrieves all files, branches, and history to your local system. The git clone command initiates this process by downloading the specified repository.
Create a playbook to retrieve the last element of a list using a filter.
vars:
items: [1, 2, 3, 4, 5] tasks:
- name: Display last element debug:
var: "{{ items | last }}"
Explanation:
The last filter extracts the last item from a list, simplifying operations on sequential data structures.
Split a list into chunks of a specified size.
items: [1, 2, 3, 4, 5, 6] tasks:
- name: Create chunks
debug:
var: "{{ items | batch(2) }}"
Explanation:
The batch filter splits a list into smaller groups of specified size, useful for parallel processing.
Create an inventory in Automation Controller using a dynamic inventory script.
o Source: Custom Script
o Script: Upload the inventory script (e.g., idm_inventory.py).
2. Sync the inventory.
Explanation:
Dynamic inventory scripts allow fetching host information from external systems like databases or cloud platforms.
Create an inventory that uses AWS EC2 tags to group hosts.
regions:
- us-west-2 keyed_groups:
- key: tags.Name prefix: tag_
Explanation:
Using AWS EC2 tags to group hosts allows dynamic organization of cloud resources in inventories.
Modify the playbook to override ansible_port for web1 in the inventory.
Explanation:
Overriding ansible_port at the host level ensures compatibility with non-standard SSH configurations.