Overview of Ansible PLAYBOOK
An Ansible playbook is a single YAML file that contains multiple plays.
Each Play will be defined with a set of activities that are treated as tasks, and these tasks can be executed at the remote host that is the Ansible client.
The task can be a single action that can be one of:
- Execute a command
- Run a script
- install patch or package
- Reboot VM/box
- Restart services
Simple ansible play can be
- check the timestamp
- reboot server
- wait for connect back
- check the uptime
- check timestamp
Complex ansible play
- Take a backup of files on 20 DB VMs
- Deploy application on 100 App boxes
- 100 servers patch apply
- 100 VM reboot after patch
- Mail and slack notifications on patch process
Ansible ad-hoc command
When you plan to write a playbook first you need to test the ad-hoc commands as trial and error will gives more confidence to run in a play
# Ansible ad-hoc command syntax ansible [-i inventry_file] server[group1:group2] -m module [-a argument]Here is some, "Hello World" program from Ansible ad-hoc command and playbook
cd qa; ls # Using qa servers as targets ansible qa -m debug -a "Welcome To VybhavaTechnologies from remote" ansible -i localhost -m debug -a "Welcome To VybhavaTechnologies from localhost"We can also the ping to the specific host alias that is defined in the "/etc/ansible/hosts" file as shown below
[servers] host1 ansible_ssh_host=192.168.33.200 host2 ansible_ssh_host=192.168.33.210 host3 ansible_ssh_host=192.168.33.220
Why do we run ad-hoc commands in Ansible Controller?
- To use setup tasks to quickly bring a managed node to a desired state
- To perform a quick test to verify that a playbook has executed successfuly
- To run a discovery task to verify that a node meets certain criteria
Sample ad-hoc commands with Ansible Shell Module
Let's experiment with 'shell' module send a terminal command to the remote host and retrieve the results. For instance, to find out the disk-space and uptime usage on our host2 machine, we could use:
ansible -m shell -a 'df -h .;uptime' server
Ansible shell module execution example. |
You can run the ansible commands on the selective hosts here host2 and host3.
ansible -m shell -a 'uptime' host2:host3Converting the above ad-hoc command execution into a playbook as hello.yml file. You can use your favourite editor, vi hello.yml # File name: hello.yml
--- - name: Hello from ansible playbook hosts: localhost tasks: - name: Prints message debug: msg: "Welcome To VybhavaTechnologies from remote"Execute the playbook using ansible-playbook command with playbook yaml file as :
ansible-playbook hello.ymlMore Examples to practice different automation needs:
ansible -i prod_inv prod -m shell -a "uptime" ansible -i prod_inv prod:web:db -m shell -a "uptime" # multiple group ansible web -m shell -a "free -m" # get the RAM size of web groupExecution of the above commands are
How to converting the ad-hoc commands to playbook?
Here I've tried to get converted simple uptime, free commands to run in playbook
# File: fun-play.yaml --- - name: Monitor CPU and Mem hosts: all tasks: - name: Find CPU load shell: uptime register: up_time - debug: var: up_time.stdout_lines - name: Find RAM Size shell: free -m register: free_ram - debug: var: free_ram.stdout_linesExecution of the above play book as follows:
ansible-playbook fun-play.yamlYou can add df command to this one more play into the playbook and give try.
How a PLAYBOOK structure can be?
A playbook can have multiple play sections.
Multiple play sections in a Ansible Playbook |
Creating your first Playbook
Here is a simple play book with very simple tasks
It's execution output is as shown below:
--- - name: First playbook hosts: db tasks: - name: test command command: hostname register: output - debug: var=output - name: test a script script: remoterun.sh register: scriptout - debug: msg: "{{ scriptout.stdout }}" - name: another play hosts: db tasks: - name: Install webserver become: yes become_user: root yum: name: httpd state: present - name: Start webserver become: yes become_user: root service: name: httpd state: startedCreate a script File remoterun.sh
echo "Welcome from shell script"
How does Ansible built-in Debug works?
A debug module block can be added to any of the tasks, which will help us to understand the Ansible execution flow.
Sample example where debug uses ansible facts to retrieve the remote host IP addresses.
--- - name: Testing hosts: all gather_facts: yes tasks: - debug: var=hostvars[inventory_hostname]['ansible_env'].SSH_CONNECTION.split(' ')[2]
The execution output is image
_/\_
Hope you enjoyed this post, Keep learning, Keep smiling Keep sharing ... :)
No comments:
Post a Comment