Last active
September 26, 2020 15:55
-
-
Save harshalbhakta/af13cea8bebc91814ea4e27f887ee73c to your computer and use it in GitHub Desktop.
Ansible Cheatsheet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- hosts: all | |
become: true | |
vars: | |
package: vim # Deifines a global variables that can be used across the playbook | |
packages: [ 'vim', 'git', 'curl' ] # Stor array in a variable | |
doc_root: /var/www/example | |
tasks: | |
# Installing a single package | |
- name: Install a single Package | |
apt: name={{ package }} state=latest # apt is Ansible's builtin module. | |
# Installing multiple packages using a loop | |
- name: Install Packages | |
apt: name={{ item }} state=latest | |
with_items: # Loop with multiple items. Reference each with {{ item }} | |
- vim | |
- git | |
- curl | |
# Conditionally execute a task using when | |
- name: Shutdown Debian Based Systems | |
command: /sbin/shutdown -t now | |
when: ansible_os_family == "Debian" | |
# Conditionally use result of one task in other task using register | |
- name: Check if PHP is installed | |
register: php_installed # Store resutlt of task in a variable | |
command: php -v | |
ignore_errors: true # Continue executing other tasks even if this task fails | |
- name: This task is only executed if PHP is installed | |
debug: var=php_install # debug: var is used to print content of variable | |
when: php_installed|success # Use variable from previous task to conditionally execute this task | |
- name: This task is only executed if PHP is NOT installed | |
debug: msg='PHP is NOT installed' # debug: msg us used to print a string | |
when: php_installed|failed | |
# Example task showing how to use templates | |
- name: Change default Apache virtual host | |
template: # Template module is used to create a file using a template | |
src: ansible-template-example.tpl # doc_root variable is available in this file | |
# Triggering handler via notify | |
- name: Change default Apache virtual host | |
template: | |
src: vhost.tpl | |
dest: /etc/apache2/sites-available/000-default.conf | |
notify: restart apache | |
dest: /etc/apache2/sites-available/000-default.conf | |
# Handlers can only to be triggered via notify in tasks. Handlers can also be placed in separate files | |
# Handlers trigger state change in services (restart or a stop) | |
handlers: | |
- name: restart apache | |
service: name=apache2 state=restarted | |
- name: other handler | |
service: name=other state=restarted |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- doc_root is a replacable variable --> | |
<VirtualHost *:80> | |
ServerAdmin webmaster@localhost | |
DocumentRoot {{ doc_root }} | |
<Directory {{ doc_root }}> | |
AllowOverride All | |
Require all granted | |
</Directory> | |
</VirtualHost> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment