-->

DEVOPSZONES

  • Recent blogs

    Ansible: Run your first playbook

     Ansible: Run your first playbook

    Ansible: Run your first playbook
    Ansible: Run your first playbook

    Recap:

    Ansible Installation:

    Ansible is an agentless automation tool that you install on a single host (referred to as the control node). From the control node, Ansible can manage an entire fleet of machines and other devices (referred to as managed nodes) remotely with SSH, Powershell remoting, and numerous other transports, all from a simple command-line interface with no databases or daemons required.

    Control node requirements

    For your control node (the machine that runs Ansible), you can use nearly any UNIX-like machine with Python 3.8 or newer installed. This includes Red Hat, Debian, Ubuntu, macOS, BSDs, and Windows under a Windows Subsystem for Linux (WSL) distribution.

    Install Ansible:

     #apt-get install ansible

    Check Ansible:

     # ansible --version

     YAML Syntax in Ansible Playbooks


    What is YAML?

    • YAML Ain’t Markup Language (YAML) is often called a data serialization language.
    • It was intended to be human-readable and organize data into a structured format.
    • Programming languages can understand the content of YAML files (which usually have a .yml or .yaml extension) and map them to built-in data types.
    • All YAML files (regardless of their association with Ansible or not) can optionally begin with --- and end with .... This is part of the YAML format and indicates the start and end of a document.
    • Lists in YAML are represented with a hyphen followed by a white space. Each play is an associative array, a dictionary, or a map in terms of key-value pairs.
    • Indentations are important. All members of a list should be at the same indentation level.
    • Each play can contain key-value pairs separated by ":" to denote hosts, variables, roles, tasks, and so on.

    YAML syntax with file formatting

    There're a few rules to follow while developing YAML files. YAML uses indentation (like Python), which builds the relationship of items with one another.

    Do not use TAB

    YAML files are based on indentation but one important thing is that in YAML file we should not use TAB instead it is recommended to use space character. So we will update our .vimrc file to only allow two spaces when someone hits a TAB button for yaml FileType. We will add this in the home folder of ansible user on the controller node where we will be working with our YAML files.

    root@cactiserver:~# cat .vimrc
    autocmd FileType yaml setlocal ai ts=2 sw=2 et

    Writing your ansible playbook

    • A playbook is based on YAML file syntax which can contain one or more than one play
    • A play is a combination of hosts and tasks.
    • A task is nothing but a call of action, which applies on group of hosts.
    sample playbook
    sample playbook

    • The very first line is the place where we put --- to mark the starting of the playbook
    • Next we can either use name to assign a name of the PLAY or you can use hosts. But the second line should start with [space][dash][space] followed by the Key which is name with a value "update web servers"
    • Then we mention the tasks and alternatively we can assign a name to the task. This will again act as another List with a Key Value pair.
    • Under the tasks the first thing we define is "module" which will actually perform the task
    • Under the module you have to provide the argument lists for the respective module. 
    • I have created two TASKS in update web servers and two TASKs in update db servers to give you a rough idea of the playbook structure.

    Running playbooks

    To run your playbook, use the ansible-playbook command.

    ansible-playbook web-server-playbook.yaml -f 10 --verbose
    

    Use the --verbose flag when running your playbook to see detailed output from successful modules as well as unsuccessful ones.

    Run sample Playbook
    Run sample Playbook

    Hope this helps. Sharing is careing.



    No comments