Ansible Cheat Sheet
[vc_row][vc_column][vc_column_text]
Set & Check Hosts Connection | |
Command | What does it do? |
sudo nano /etc/ansible/hosts | Set up hosts by editing the hosts’ file in the Ansible directory |
ansible –m ping <hosts> | Ansible’s ping module allows you to check whether Ansible is connecting to hosts |
ansible -m ping server name | To check on servers individually |
ansible -m ping servergroupname | To check a particular server group |
Example Inventory File | |
Default location for host file
$ /etc/ansible/hosts #example host file [webservers] Â Â Â Â Â Â Â Â Â Â Â Â Â #a group called webservers [clouds] [moscow] [dev1:children]Â Â Â Â Â Â Â Â Â Â Â Â Â #dev1 is a group containing |
Parallelism & Shell Commands | |
Command | What does it do? |
ansible europe -a “/sbin/reboot” -f 20 | To use SSH with a password instead of keys, you can use –ask-pass (-K) |
ansible europe -a “/usr/bin/foo” -u username | To run /usr/bin/ansible from a user account, not the root |
ansible europe -a “/usr/bin/foo” -u username –become [–ask-become-pass] | To run commands through privilege escalation and not through user account |
ansible europe -a “/usr/bin/foo” -u username –become –become-user otheruser [–ask-become-pass] | If you are using password less method then use –ask-become-pass (-K) |
File Transfer | |
Command | What does it do? |
ansible europe -m copy -a “src=/etc/hosts dest=/tmp/hosts” | Transfer a file directly to many servers |
ansible webservers -m file -a “dest=/srv/foo/b.txt mode=600 owner=example group=example” | To change the ownership and permissions on files |
ansible webservers -m file -a “dest=/path/to/c mode=755 owner=example group=example state=directory” | To create directories |
ansible webservers -m file -a “dest=/path/to/c state=absent” | To delete directories (recursively) and delete files |
Manage Packages | |
Command | What does it do? |
ansible webservers -m apt -a “name=acme state=present” | To ensure that a package is installed, but doesn’t get updated |
ansible webservers -m apt -a “name=acme-1.5 state=present” | To ensure that a package is installed to a specific version |
ansible webservers -m apt -a “name=acme state=latest” | To ensure that a package at the latest version |
ansible webservers -m apt -a “name=acme state=absent” | To ensure that a package is not installed |
Manage Services | |
Command | What does it do? |
ansible webservers -m service -a “name=httpd state=started” | To ensure a service is started on all web servers |
ansible webservers -m service -a “name=httpd state=restarted” | To restart a service on all web servers |
ansible webservers -m service -a “name=httpd state=stopped | To ensure a service is stopped |
Sample Playbook | |
#Every YAML file starts with —
— – hosts: webservers  tasks:  handlers: #Running a playbook |
[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column][thim-button title=”Download as PDF” url=”https://webmagicinformatica.com/wp-content/uploads/2022/06/Ansible-Cheat-Sheet.pdf” new_window=”” custom_style=”custom_style” text_align=”text-center” border_width=”1″ hover_border_color=”#ffb606″ button_size=”medium” rounding=”tiny-rounded”][/vc_column][/vc_row]