cat ~/.ssh/id_ed25519.pub | ssh -i ssh_key.pem azureuser@localhost 'mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys'
Ansible facts
-
facts are pieces of information ansible automatically discover
-
host, OS version, Ip address, cpu, memory disk,hostnames and so on …vm level
-
to collection facts by using setup module
# to get all facts info
ansible all -i hosts -m ansible.builtin.setup
ansible all -i hosts -m ansible.builtin.setup -a "filter=ansible_distribution"
Most Common Ansible Facts
A quick reference of frequently used keys inside the ansible_facts dictionary.
Operating System & Distribution
ansible_facts['distribution']— Name of the OS distribution (e.g.,Ubuntu,RedHat,AlmaLinux)ansible_facts['distribution_version']— Full version number of the OS (e.g.,24.04,9.3)ansible_facts['distribution_major_version']— Major version number of the OS (e.g.,24,9)ansible_facts['distribution_release']— Code name of the OS release (e.g.,noble,jammy)ansible_facts['os_family']— Broad OS family grouping (e.g.,Debian,RedHat,Suse)ansible_facts['kernel']— Active Linux kernel version (e.g.,6.8.0-45-generic)ansible_facts['pkg_mgr']— Default system package manager (e.g.,apt,dnf,yum)ansible_facts['service_mgr']— Default system init/service manager (e.g.,systemd)
System Identity & Environment
ansible_facts['hostname']— Short hostname of the target machine (e.g.,web01)ansible_facts['fqdn']— Fully Qualified Domain Name (e.g.,://example.com)ansible_facts['domain']— DNS domain name of the host (e.g.,example.com)ansible_facts['architecture']— CPU architecture of the operating system (e.g.,x86_64,aarch64)ansible_facts['machine_id']— Unique systemd machine identifieransible_facts['virtualization_type']— Hypervisor or container technology (e.g.,kvm,docker,vmware)ansible_facts['virtualization_role']— Role of the node in virtualization (guestorhost)ansible_facts['env']— Dictionary of remote environment variables (e.g.,ansible_facts['env']['PATH'])
Network & IP Addresses
ansible_facts['default_ipv4']['address']— Primary IPv4 address used for default routingansible_facts['default_ipv4']['interface']— Network interface name for the default route (e.g.,eth0,ens3)ansible_facts['default_ipv4']['gateway']— Default IPv4 gateway addressansible_facts['default_ipv4']['macaddress']— MAC address of the primary interfaceansible_facts['all_ipv4_addresses']— List of all active IPv4 addresses on the hostansible_facts['all_ipv6_addresses']— List of all active IPv6 addresses on the hostansible_facts['interfaces']— List of all available network interface names (e.g.,['lo', 'eth0'])
Hardware, Memory & Storage
ansible_facts['processor_vcpus']— Total number of logical virtual CPUsansible_facts['processor_cores']— Number of physical cores per processoransible_facts['processor']— List containing CPU model names and detailsansible_facts['memtotal_mb']— Total physical RAM in megabytesansible_facts['memfree_mb']— Currently unused RAM in megabytesansible_facts['swaptotal_mb']— Total swap space in megabytesansible_facts['mounts']— List of dictionaries describing mounted filesystemsansible_facts['devices']— Dictionary detailing block storage devices and partitions
Date & Time
ansible_facts['date_time']['date']— Current date inYYYY-MM-DDformatansible_facts['date_time']['time']— Current time inHH:MM:SSformatansible_facts['date_time']['iso8601']— UTC timestamp in ISO 8601 formatansible_facts['date_time']['tz']— System timezone abbreviation (e.g.,UTC,IST)
Quick CLI Commands
- Gather all facts for a host:
ansible <hostname> -m ansible.builtin.setup - Filter specific facts:
ansible <hostname> -m ansible.builtin.setup -a "filter=ansible_distribution*"
Task
- make tomcat file suppourt both os
- create new files under task
Prompt: Create a Custom Ansible Docker Image
Act as a Senior DevOps Engineer and Docker expert.
I want to create a reusable custom Ansible Docker image that works like a command-line tool.
Requirements
-
Create a Dockerfile to install:
- Python
- Ansible
- OpenSSH client
- sshpass
- Git
-
Create an entrypoint script that accepts and executes any command passed through
docker run. -
Build the image with the name:
custom-ansible:latest -
I should be able to pass any Ansible command directly after the Docker image name.
Expected Usage
Check Ansible version:
docker run --rm custom-ansible:latest ansible --version
Run Ansible ping:
docker run --rm \
-v "$(pwd):/ansible" \
-v "$HOME/.ssh:/root/.ssh:ro" \
custom-ansible:latest \
ansible all -i inventory.ini -m ping
Run Ansible playbook:
docker run --rm \
-v "$(pwd):/ansible" \
-v "$HOME/.ssh:/root/.ssh:ro" \
custom-ansible:latest \
ansible-playbook -i inventory.ini playbook.yml
Interactive shell:
docker run -it --rm \
-v "$(pwd):/ansible" \
--entrypoint /bin/bash \
custom-ansible:latest
Deliverables
- Complete project folder structure.
- Dockerfile.
- Entrypoint shell script.
- Build and run commands.
- Support Linux, Ubuntu, and Windows PowerShell usage.
- Explain volume mounting and SSH key permissions.
- Include troubleshooting steps.
- Follow Docker best practices and keep the image lightweight.
Important: Do not hardcode Ansible commands in the Dockerfile. The container must execute whatever command the user passes after the image name using exec "$@".
Provide complete working files with beginner-friendly explanations.
