Ansible Mastery: 100 Essential Commands and Techniques for Efficient Automation
Content:
ansible --version
: Check Ansible version.ansible all -m ping
: Test connectivity to all hosts.ansible all -a "whoami"
: Execute the whoami command on all hosts.ansible-playbook playbook.yml
: Run a playbook.ansible-inventory --list
: List all hosts in your inventory.ansible-playbook playbook.yml --syntax-check
: Check syntax of a playbook.ansible-playbook playbook.yml --limit "hostname"
: Target a specific host in a playbook run.ansible-playbook playbook.yml --check
: Dry run a playbook.ansible all -m setup
: Gather facts from all hosts.ansible all -b -a "apt update"
: Update package lists on all Debian/Ubuntu hosts.ansible all -m copy
: Copy files to all hosts.ansible all -m file
: Delete files from all hosts.ansible all -m user
: Ensure a user exists on all hosts.ansible all -m service
: Ensure a service is started on all hosts.ansible-vault create secret.yml
: Create a new encrypted file.ansible-vault edit secret.yml
: Edit an encrypted file.ansible-vault decrypt secret.yml
: Decrypt a file.ansible-vault encrypt secret.yml
: Encrypt a file.ansible-doc -l
: List all available Ansible modules.ansible-doc copy
: View documentation for the copy module.ansible all -m shell
: Execute shell commands on all hosts.ansible-playbook --step playbook.yml
: Execute a playbook in step-by-step mode.ansible all -m apt
: Install nginx on all Debian/Ubuntu hosts.ansible all -m yum
: Install httpd on all RHEL/CentOS hosts.ansible all -m group
: Ensure a group exists on all hosts.ansible-galaxy install username.rolename
: Install a specific role from Ansible Galaxy.ansible-console
: Start an interactive Ansible console.ansible-config list
: List all configuration settings.ansible-config view
: View the current configuration file.ansible all -m ping --forks=50
: Execute the ping module with 50 parallel forks.ansible-playbook playbook.yml -e "var=value"
: Run playbook with extra variables.ansible all -m setup -a "filter=ansible_os_family"
: Gather OS family facts from all hosts.ansible-inventory --graph
: Display a graph of your inventory.ansible-playbook playbook.yml --tags "tagname"
: Run only tasks with a specific tag in a playbook.ansible-playbook playbook.yml --skip-tags "tagname"
: Skip tasks with a specific tag in a playbook.ansible all -m debug -a "var=hostvars"
: Debug by printing host variables.ansible-vault rekey secret.yml
: Change the encryption password for a vault file.ansible all -m setup --tree /tmp/facts
: Save gathered facts into files under /tmp/facts.ansible-galaxy role init myrole
: Initialize a new role structure.ansible-galaxy collection install community.general
: Install a collection from Ansible Galaxy.ansible-playbook playbook.yml --vault-id @prompt
: Run a playbook with vault, prompting for the password.ansible localhost -m debug -a "msg='Hello World'"
: Print "Hello World" message.ansible all -m stat -a "path=/path/to/file"
: Get file statistics for all hosts.ansible all -m setup -a "gather_subset=!all"
: Gather limited set of facts from all hosts.ansible all -b -m package -a "name=git state=present"
: Install git on all hosts using the package module.ansible-playbook playbook.yml --force-handlers
: Force handlers to run even if a task fails.ansible all -m ping --become
: Test connectivity using become (sudo).ansible all -m setup -a "filter=ansible_distribution"
: Gather distribution facts from all hosts.ansible-playbook playbook.yml --list-tasks
: List all tasks in a playbook.ansible-playbook playbook.yml --list-hosts
: List all hosts targeted by a playbook.ansible all -m win_command -a "ipconfig /all"
: Execute ipconfig /all on Windows hosts.ansible all -m win_ping
: Test connectivity to Windows hosts.ansible all -m setup -a "filter=ansible_processor*"
: Gather processor facts.ansible-playbook playbook.yml --tags "configure"
: Run tasks tagged with "configure".ansible localhost -m setup -a "filter=ansible_processor_cores"
: Get processor core info from localhost.ansible all -b -m yum -a "name=nginx state=latest"
: Ensure latest NGINX on RHEL/CentOS.ansible all -b -m apt -a "pkg=apache2 state=absent"
: Remove Apache2 on Debian/Ubuntu.ansible all -m ping -e 'ansible_python_interpreter=/usr/bin/python3'
: Use Python3 for ping.ansible all -b -m service -a "name=ssh state=restarted"
: Restart SSH service on all nodes.ansible all -m debug -a "msg='{{ ansible_facts['os_family'] }}'"
: Show OS family via debug.ansible all -b -m file -a "dest=/example/file state=file mode=0664"
: Ensure file exists with permissions.ansible all -b -m shell -a "useradd -m john"
: Add user 'john' with home directory.ansible-playbook playbook.yml --limit "webservers:!phoenix"
: Exclude 'phoenix' from 'webservers'.ansible all -m ping --become --become-user root
: Ping as root.ansible all -m setup -a "filter=ansible_memory_mb"
: Gather memory facts.ansible-playbook playbook.yml --no-cows
: No cowsay.ansible-console -i inventory.yml
: Interactive console with inventory.ansible all -m get_url -a "url=https://example.com/file dest=/tmp/file"
: Download file.ansible all -b -m shell -a "iptables -L"
: List iptables rules.ansible-galaxy collection update ansible.builtin
: Update ansible.builtin collection.ansible all -b -m modprobe -a "name=dm_mirror state=present"
: Load dm_mirror module.ansible-playbook playbook.yml --connection=local
: Use local connection.ansible all -b -m shell -a "echo 'Hello World' > /tmp/hello.txt"
: Echo text to file.ansible all -b -m setup -a "filter=ansible_processor_cores"
: Processor cores info.ansible all -b -m selinux -a "state=permissive"
: Set SELinux permissive.ansible all -b -m timezone -a "name=UTC"
: Set timezone to UTC.ansible all -b -m git -a "repo=https://github.com/example/repo.git dest=/opt/repo"
: Clone git repo.ansible all -b -m shell -a "/usr/bin/systemctl restart nginx"
: Restart nginx with systemctl.ansible all -m ping --forks=20
: Ping with 20 forks.ansible all -b -m file -a "path=/tmp/testdir state=absent"
: Remove directory.ansible all -m setup -a "gather_subset=all"
: Gather all facts.ansible all -m debug -a "var=hostvars[inventory_hostname]"
: Debug host vars.ansible-galaxy role uninstall username.rolename
: Uninstall a role.ansible all -b -m npm -a "name=express global=yes state=present"
: Install npm package globally.ansible all -b -m apt_repository -a "repo='ppa:ansible/ansible' state=present"
: Add apt repository.ansible all -b -m sysctl -a "name=net.ipv4.ip_forward value=1 state=present"
: Enable IP forwarding.ansible-playbook playbook.yml --inventory-file=/path/to/inventory
: Specify inventory file.ansible all -m win_command -a "ipconfig /all"
: Run ipconfig /all on Windows hosts.ansible-playbook playbook.yml --tags "deploy"
: Run tasks tagged 'deploy'.ansible all -m shell -a "lsb_release -a"
: Run lsb_release -a on all hosts.ansible all -m setup -a "filter=ansible_lvm"
: Gather LVM facts.ansible all -b -m package -a "name=elasticsearch state=present"
: Install Elasticsearch.ansible-playbook playbook.yml --ask-pass
: Prompt for SSH password.ansible all -b -m reboot
: Reboot all hosts.ansible all -m setup -a "gather_subset=!all,hardware"
: Exclude hardware facts.ansible all -b -m file -a "dest=/example/file state=file mode=0664"
: Ensure file exists with permissions.ansible all -b -m shell -a "useradd -m john"
: Add user 'john' with home directory.ansible-playbook playbook.yml --limit "webservers:!phoenix"
: Exclude 'phoenix' from 'webservers'.ansible all -m ping --become --become-user root
: Ping as root.ansible all -m setup -a "filter=ansible_memory_mb"
: Gather memory facts.
No comments:
Post a Comment