|
| 1 | +# Ansible Commands, Playbooks, and Cheatsheet 🚀 |
| 2 | + |
| 3 | +## Ansible Commands |
| 4 | + |
| 5 | +### Installation |
| 6 | + |
| 7 | +# Install Ansible on Debian/Ubuntu |
| 8 | +```sh |
| 9 | +sudo apt update |
| 10 | +sudo apt install ansible |
| 11 | +``` |
| 12 | + |
| 13 | +# Install Ansible on CentOS/RHEL |
| 14 | +```sh |
| 15 | +sudo yum install ansible |
| 16 | +``` |
| 17 | + |
| 18 | +### Basic Commands |
| 19 | + |
| 20 | +**Check Ansible version** |
| 21 | +```sh |
| 22 | +ansible --version |
| 23 | +``` |
| 24 | +**Ping test to check connectivity** |
| 25 | +```sh |
| 26 | +ansible all -m ping |
| 27 | +``` |
| 28 | + |
| 29 | +**Ad-hoc command example (installing a package)** |
| 30 | +```sh |
| 31 | +ansible webserver -m yum -a "name=httpd state=latest" |
| 32 | +``` |
| 33 | + |
| 34 | +### Ansible Playbooks |
| 35 | +**Playbook Structure** |
| 36 | +```sh |
| 37 | +--- |
| 38 | +- name: Configure Web Servers |
| 39 | + hosts: webservers |
| 40 | + become: yes |
| 41 | + |
| 42 | + tasks: |
| 43 | + - name: Install Apache |
| 44 | + yum: |
| 45 | + name: httpd |
| 46 | + state: present |
| 47 | + |
| 48 | + - name: Start Apache Service |
| 49 | + service: |
| 50 | + name: httpd |
| 51 | + state: started |
| 52 | +``` |
| 53 | + |
| 54 | +**Example Playbook** |
| 55 | +```sh |
| 56 | +--- |
| 57 | +- name: Setup Monitoring |
| 58 | + hosts: monitoring |
| 59 | + become: yes |
| 60 | + |
| 61 | + tasks: |
| 62 | + - name: Install SNMP Service |
| 63 | + yum: |
| 64 | + name: net-snmp |
| 65 | + state: present |
| 66 | + |
| 67 | + - name: Configure SNMP Community |
| 68 | + template: |
| 69 | + src: snmpd.conf.j2 |
| 70 | + dest: /etc/snmp/snmpd.conf |
| 71 | + notify: |
| 72 | + - restart snmpd |
| 73 | + |
| 74 | + handlers: |
| 75 | + - name: restart snmpd |
| 76 | + service: |
| 77 | + name: snmpd |
| 78 | + state: restarted |
| 79 | +``` |
| 80 | + |
| 81 | +### Ansible Cheatsheet |
| 82 | +**Common Modules*** |
| 83 | +>yum: Manages packages on RPM-based systems. |
| 84 | +>apt: Manages packages on Debian-based systems. |
| 85 | +>file: Manages files and directories. |
| 86 | +>copy: Copies files to remote locations. |
| 87 | +>service: Manages services on the system. |
| 88 | +>template: Manages file templates. |
| 89 | +
|
| 90 | +**Inventory File (hosts.ini)** |
| 91 | +```ini |
| 92 | +[webservers] |
| 93 | +webserver1 ansible_host=192.168.1.101 |
| 94 | +webserver2 ansible_host=192.168.1.102 |
| 95 | + |
| 96 | +[monitoring] |
| 97 | +monitor1 ansible_host=192.168.1.201 |
| 98 | +``` |
| 99 | + |
| 100 | +**Variables and Templates** |
| 101 | +```sh |
| 102 | +# Variable example |
| 103 | +--- |
| 104 | +web_servers: |
| 105 | + - hostname: webserver1 |
| 106 | + ip: 192.168.1.101 |
| 107 | + |
| 108 | +# Template example |
| 109 | +--- |
| 110 | +- name: Configure Apache |
| 111 | + template: |
| 112 | + src: httpd.conf.j2 |
| 113 | + dest: /etc/httpd/conf/httpd.conf |
| 114 | +``` |
| 115 | + |
| 116 | +**Troubleshooting and Advanced Tips** |
| 117 | +>Use -vvv for verbose output to debug. |
| 118 | +>Ensure correct YAML syntax in playbooks (ansible-lint for linting). |
| 119 | +>Utilize roles for modular and reusable playbook organization. |
| 120 | +>Leverage Ansible Galaxy for community-contributed roles. |
| 121 | +
|
| 122 | +**Additional Resources** |
| 123 | +For comprehensive Ansible documentation and tutorials, visit [Ansible Documentation](https://docs.ansible.com/). |
| 124 | + |
| 125 | +This Markdown file provides an in-depth reference for Ansible commands, playbook structure, common modules, inventory setup, variables, templates, troubleshooting tips, and advanced techniques. Customize examples and details to suit your specific Ansible deployment and management needs. |
0 commit comments