Skip to content

Latest commit

 

History

History
126 lines (79 loc) · 2.47 KB

ansible_command_line.md

File metadata and controls

126 lines (79 loc) · 2.47 KB

Ansible Command Line

A Thread of 10 key Ansible commands and concepts all DevOps and Linux Administrators should focus on.

  1. Check Ansible Version

Command: ansible --version

Explanation: Verify your Ansible installation and check version details.

  1. Ping All Hosts

Command: ansible all -m ping

Explanation: Ping all hosts in your inventory to ensure they are reachable.

  1. Run Command on Remote Hosts

Command: ansible all -a "uptime"

Explanation: Use ad-hoc commands to execute tasks like checking uptime on all remote hosts.

  1. Target Specific Hosts

Command: ansible webservers -m ping

Explanation: Run commands for a specific group of hosts (e.g., webservers).

  1. Execute Shell Commands

Command: ansible all -m shell -a "ls -l /var/www/"

Explanation: Run shell commands on remote hosts using the shell module.

  1. Copy Files to Remote Hosts

Command: ansible all -m copy -a "src=/local/file dest=/remote/path"

Explanation: Transfer files from the control node to remote machines.

  1. Gather Facts from Remote Hosts

Command: ansible all -m setup

Explanation: Collect detailed system facts (OS info, IP address) from remote hosts.

  1. Run Playbook

Command: ansible-playbook site.yml

Explanation: Execute tasks defined in the site.yml playbook file.

  1. Check Playbook Syntax

Command: ansible-playbook site.yml --syntax-check

Explanation: Ensure your playbook has no syntax errors before running it.

  1. Simulate Playbook Changes

Command: ansible-playbook site.yml --check

Explanation: Run in "check" mode to preview changes without making them.

  1. Limit Playbook to Specific Hosts

Command: ansible-playbook site.yml --limit webservers

Explanation: Run your playbook on a specific group of hosts only.

  1. Use Privilege Escalation (sudo)

Command: ansible all -b -m yum -a "name=nginx state=present"

Explanation: Use the -b (become) flag to run tasks with elevated privileges.

  1. Run Specific Tasks via Tags

Command: ansible-playbook site.yml --tags "install"

Explanation: Use tags to execute specific parts of the playbook, like installations.

  1. Use Custom Inventory File

Command: ansible-playbook -i /path/to/inventory site.yml

Explanation: Specify a custom inventory file instead of using the default one.

  1. Check Host Connectivity Before Running

Command: ansible-playbook -i inventory.yml site.yml --list-hosts

Explanation: List the hosts that the playbook will be applied to without executing it.

Bibliography: