Example 1: Create a playbook to run an echo "Hello World" command on dbservers.
Create a hello.yaml in /etc/ansible folder. ( you can give any primary name but the extension should be .yaml or .yml).
touch /etc/ansible/hello.yaml
vi hello.yamlIn the .yaml file,Type in:---
- name: play for running shell commands
hosts: dbservers
tasks:
- name: Executing command module
command: echo "Hello World"
Execute: ansible-playbook hello.yaml
BUT this does not show the output!!
So to remedy this, the hello.yaml file needs to add some more lines of codes:
register: output
- name:
debug:
msg: "{{ output.stdout }}"
now it will show as per picture below!
Example 2: Create a playbook to read a file (/tmp/status.txt) using the command module.
---
- name: play for running shell commands
hosts: dbservers
tasks:
- name: Executing cat command
command: cat /tmp/status.txt
register: output
- name: Printing output of cat command
debug:
msg: "{{ output.stdout }}"
Example 3: Create MULTIPLE plays in a playbook.
- playbook: multiple.yaml
- play1: run on dbservers and create a file /tmp/local.txt
- play2: run on webservers and create a file /tmp/webserver.txt
---
- name: running shell commands on dbservers
hosts: dbservers
tasks:
- name: creating local.txt on dbservers
command: touch /tmp/local.txt
- name: running shell commands on webservers
hosts: webservers
tasks:
- name: creating webservers.txt on webservers
command: touch /tmp/webservers.txt
done!
Example 4: Create a yaml file called (mul.yaml) and make a script such that it will create a file (testansible.txt) in /tmp and add content ("Hello World") in that .txt file and read it.
touch /etc/ansible/mul.yaml
vi /etc/ansible/mul.yaml
---
- name: running shell command on dbservers
hosts: dbservers
tasks:
- name: creating a file in /tmp/
command: touch /tmp/testansible.txt
- name: adding text in the .txt file (you can use copy module or shell module)
copy:
content: "Hello World"
dest: /tmp/testansible.txt
shell: echo "Hello World2" >> /tmp/testansible.txt
- name: executing cat command on content of .txt file
command: cat /tmp/testansible.txt
register: output
- name: printing out the content
debug:
msg: "{{ output.stdout }}"
ansible-playbook mul.yaml
Example 5: Create a variable and store "Hello World" then print it out using the same mul.yaml file as example 4 above.
vi mul.yaml
---
- name: running shell command on dbservers
hosts: dbservers
vars:
string: "Hello World!"
tasks:
- name: creating a file in /tmp/
command: touch /tmp/testansible.txt
- name: adding text in the .txt file
copy:
content: "{{ string }}"
dest: /tmp/testansible.txt
- name: executing cat command on content of .txt file
command: cat /tmp/testansible.txt
register: output
- name: printing out the content
debug:
msg: "{{ output.stdout }}"
Example 6: use vars_prompt
---
- name: running shell command on dbservers
hosts: dbservers
vars_prompt:
name: data
prompt: Enter the value
tasks:
- name: creating a file in /tmp/
command: touch /tmp/testansible.txt
- name: adding text in the .txt file
copy:
content: "{{ data }}"
dest: /tmp/testansible.txt
- name: executing cat command on content of .txt file
command: cat /tmp/testansible.txt
register: output
- name: printing out the content
debug:
msg: "{{ output.stdout }}"
Example 7: Dry Run with --check and check mode
Dry Run
When ansible-playbook is executed with --check it will not make any changes on remote systems. Instead, any module instrumented to support ‘check mode’ (which contains most of the primary core modules, but it is not required that all modules do this) will report what changes they would have made rather than making them. Other modules that do not support check mode will also take no action, but just will not report what changes they might have made.
Example 7.1: Dry Run with --check
Below script is copying /tmp/testing.txt file to webservers /tmp/test.txt. but if you use --check option with ansible-playbook execution it will only do the dry run not actually running the command to copy to webservers.
touch /tmp/testing.txt
touch dryrun.yamlvi dryrun.yaml
--- - name: play for dry run
hosts: webservers
tasks:
- name: Copying testing.txt file to webservers
copy:
src=/tmp/testing.txt
dest=/tmp/test.txt
ansible-playbook dryrun.yml --check
Example 7.2: Dry Run with check_mode
touch dryruncheckmode.yaml
vi dryruncheckmode.yaml
--- - name: play for dbservers multiple tasks
hosts: dbservers
vars_prompt:
name: data
prompt: Enter the value
tasks:
# Task1
- name: create a file
command: touch /tmp/myfile.txt
check_mode: no (this will not apply dry run)
# Task2
- name: Add content to the file
copy:
content: "{{ data }}"
dest: /tmp/myfile.txt
check_mode: yes (this will apply dry run)
# Task 3
- name: Read the content
command: cat /tmp/myfile.txt
register: output
# Task 4
- name: Print the content
debug:
var: output.stdout
ansible-playbook dryruncheckmode.yaml
Example 10: logging with log_path
Playbook:- dryrun.yaml
uncomment log_path in ansible.cfg file to store the log output in
/var/log/ansible.log--- - name: play for dry run
hosts: webservers
tasks:
- name: Copying testing.txt file to webservers
copy: src=/tmp/testing.txt
dest=/tmp/test.txt
ansible-playbook dryrun.yaml
After execution check /var/log/ansible.log file it should have the log of this file execution.
Example 11: no_log attribute
if no_log =True then no log information is recorded in the log file for that particular module.
Example 12: Error handling ( ignore_errors: True)
Playbook: error.yaml
In the below code, Task3 will be executed because Task2 is having the exception handling.
---
- hosts: webservers
tasks:
- name: Task1
command: date
- name: Task2
command: date1
ignore_errors: True
- name: Task3
command: ls
ansible-playbook error.yaml
Example 13: Magic variables (Ansible Facts)
Playbook: facts.yaml
Run the below command to find the facts of a host, it also returns the in-built ansible variables. In below command stores the output of these inbuilt variables in facts.log file
ansible dbservers -m ansible.builtin.setup > facts.log
to read the log:
vi facts.log
vi facts.yaml
---
- name: play for finding the facts using ansible magic variables
hosts: webservers
tasks:
- name: Print some facts
debug:
msg: "{{ ansible_facts['os_family'], ansible_facts['nodename'] }}"
ansible-playbook facts.yaml
It should print the OS Name and node name of web servers.
Example 14: create a conditional statement (when) if the server is using the os_family of RedHat.
vi conditional.yaml
---
- name: play for conditional statements
hosts: webservers
tasks:
- name: install httpd (apache server on CentOS)
yum:
name: httpd
state: present
when: ansible_facts['os_family']=='RedHat'
It should skip for the webservers cause they are debian-based systems.
Example 15: (!= operator)Create a playbook to install apache2 on OS which doesn't belong to RedHat family
vi conditionnotequals.yaml
---
- name: play for conditonal statements
hosts: webservers
tasks:
- name: install apache2
apt:
name: apache2
state: present
when: ansible_facts['os_family']!="RedHat"
ansible-playbook conditionnotequals.yaml
Example 16: (or logical operator)Create a playbook to install apache2 on OS which doesn't belong to RedHat family or belongs to Debian OS family
vi conditionor.yaml
---
- name: play for conditonal statements
hosts: webservers
tasks:
- name: install apache2
apt:
name: apache2
state: present
when: ansible_facts['os_family']!="RedHat" or ansible_facts['os_family']=="Debian"
ansible-playbook conditionor.yamlExample 17: (and logical operator)Create a playbook to install apache2 on OS which doesn't belong to Debian OS family and OS is Ubuntu
vi conditionand.yaml
---
- name: play for conditonal statements
hosts: webservers
tasks:
- name: install apache2
apt:
name: apache2
state: present
when: ansible_facts['os_family']=="Debian" and ansible_distribution=="Ubuntu"
ansible-playbook conditionand.yamlExample 18: Loops part 1 (iterate through item)vi loops1.yaml---
- name: playbook for loops example
hosts: dbservers
tasks:
- name: print values using loops
debug:
msg: "{{ item }}"
with_items / loop:
- 1
- 2
- 3
- 4
ansible-playbook loops1.yaml
Example 19: Loops part 2 (iterate through making directories)
Create some directories under /tmp using loop statement
vi loop2.yaml
---
- name: play for loops
hosts: dbservers
tasks:
- name: Create Multiple directories
command: mkdir "{{ item }}"
loop / with_items:
- /tmp/1
- /tmp/2
- /tmp/3
- /tmp/4
ansible-playbook loops2.yaml
Example 20: Loops part3 (remove directories created on example 19 using loops)
vi loop3.yaml
---
- name: play for loops
hosts: dbservers
vars:
dirs:
- /tmp/1
- /tmp/2
- /tmp/3
tasks:
- name: removing directories
command: rmdir "{{ item }}"
with_items / loop: "{{ dirs }}"
ansible-playbook loop3.yaml
Example 21: Multiple Conditions (utilizing in-built commands (succeeded, failed, skipped))
vi multcond.yaml
---
- name: multiple condition
hosts: webservers
vars:
testskip: test
tasks:
- name: Executing a shell script
command: sh /home/vagrant/test.sh
register: output
ignore_errors: True
when: testskip == "ABC"
- name: Execute when shell script is executed successfully
debug:
var: output.stdout
when: output is succeeded
- name: Execute when shell script execution failed
debug:
msg: "Script execuion is failed"
when: output is failed
- name: Execute when shell script is executed successfully
debug:
msg: "Script is skipped"
when: output is skipped
ansible-playbook multcond.yaml
Example 22: Ansible Tags
You can run a specific task using ansible tags.
vi tags.yaml
---
- name: using Tags
hosts: webservers
tasks:
- name: Start a service
service:
name: apache2
state: started
tags: startservice
- name: Stop apache service
service:
name: apache2
state: stopped
tags: stopservice
- name: Restart apahce service
service:
name: apache2
state: restarted
tags: restartservice
ansible-playbook tags.yaml --tags restartservice