pip install ansible
[web]
web1.example.com
web2.example.com
[db]
db1.example.com
[all:vars]
ansible_user=myuser
ansible all -m ping
ansible all -a "df -h"
ansible-playbook playbook.yml --syntax-check
ansible-playbook -i hosts playbook.yml
ansible-playbook -i hosts playbook.yml --tags "tag1,tag2"
---
- name: My first playbook
hosts: all
tasks:
- name: Ensure nginx is installed
ansible.builtin.package:
name: nginx
state: present
vars:
user_name: "john"
user_home: "/home/john"
handlers:
- name: restart nginx
ansible.builtin.service:
name: nginx
state: restarted
ansible-galaxy init my-role
my-role/
├── defaults/
├── files/
├── handlers/
├── meta/
├── tasks/
├── templates/
└── vars/
when: "'web' in group_names"
with_items:
- item1
- item2
- name: Debug variable
ansible.builtin.debug:
var: my_variable
This cheat sheet should provide a quick reference for someone new to Ansible. It covers installation, inventory files, basic commands, playbook structure, and some debugging tips.