Ansible playbooks are used to accomplish complex and repetitive tasks. Playbooks are in YAML format. A playbook is composed of plays. A play executes one or more tasks with each task calling an Ansible module.
YAML format can be tricky and lead to many errors, so we are going to make some changes in vim to eradicate such errors.
[devops@anscontrol ansible]$ cat /home/devops/.vimrc
set tabstop=2 shiftwidth=2 expandtab autoindent cursorcolumn cursorline Playbook is executed in the below format. It also accepts various arguments.
[devops@anscontrol ansible]$ ansible-playbook example.ymlEach Ansible playbook consists of plays. Each play has three sections:
- Target - The target hosts.
- Variable - Defined variables while running plays.
- Task - All the modules to run in order.
Below is an example Ansible playbook.
---
- name: This playbook will create some users (comment of entire playbook)
hosts: devhosts
vars:
version: latest
tasks:
- name: create a user named user1 - comment of this module only
user:
name: user1
comment: first user
uid: 3010
shell: /bin/bash
- name: install httpd
yum:
name: httpd
state: "{{ version }}"
- name: allow http in firewall
firewalld:
service: http
state: enabled
In the above playbook, we have defined the target which are the hosts under the group 'devhosts'.
The variable 'version' has the value 'latest' which is called in the second task module named 'yum'.
We have defined three tasks:
1. Create 'user1'
Create user named 'user1' with UID '3010' and shell '/bin/bash'.
2. Install package 'httpd'
Install the latest package 'httpd' in the hosts under the group 'devhosts'.
3. Allow 'http' service in the 'firewalld' module.
Change the state of 'http' service to enabled/allowed in firewall.
We can also verify the syntax of a playbook as given below.
[devops@anscontrol ansible]$ ansible-playbook --syntax-check example.ymlNo errors should be given in the output from above command.
Now we run the above playbook:
[devops@anscontrol ansible]$ ansible-playbook example.yml
PLAY [This playbook will create some users (comment of entire playbook)] ******************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************************************************
ok: [192.168.116.5]
ok: [192.168.116.4]
TASK [create a user named user1 - comment of this module only] ****************************************************************************************************
changed: [192.168.116.5]
changed: [192.168.116.4]
TASK [install httpd] **********************************************************************************************************************************************
changed: [192.168.116.5]
changed: [192.168.116.4]
TASK [allow http service] *****************************************************************************************************************************************
changed: [192.168.116.5]
changed: [192.168.116.4]
PLAY RECAP ********************************************************************************************************************************************************
192.168.116.4 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.116.5 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
We have successfully run our first playbook.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.