Ansible directory structure script

Ansible is a great tool for helping sysadmins doing their day-to-day tasks in a more automated manner. If you manage alot of systems, then you sure know how cumbersome it can be if you have to do something repetitive manually on them.

Ansible is a free automation software, that helps you develop your own recipes (called roles in ansible terminology), which can be used to automate almost everything.

This software is simple automation engine, that has built-in "rules" in terms of directories and files. I am posting a bash script, that deploys a preferred ansible working environment, instead of creating all the initial files and directories yourself. It is used for new projects only.

#!/bin/bash
# This script is used for quick deplyoment of your ansible directory structure according to ansible docs. Execute it directly in your directory where your ansible project should be. It is advisable for this directory to be empty beforehand! In order to start:
# 1) Edit your production and staging inventories
# 2) Execute ansible-playbook -i (staging|production) playbooks/all.yml

touch production                # inventory file for production servers
touch staging                   # inventory file for staging environment

mkdir group_vars/
    touch group_vars/group1                 # here we assign variables to particular groups
    touch group_vars/group2                 # ""
mkdir host_vars/
    touch host_vars/hostname1              # if systems need specific variables, put them here
    touch host_vars/hostname2              # ""

mkdir library/                  # if any custom modules, put them here (optional)
    mkdir library/module_utils/             # if any custom module_utils to support modules, put them here (optional)
    mkdir library/filter_plugins/           # if any custom filter plugins, put them here (optional)

mkdir playbooks/
    touch playbooks/all.yml            # playbook for webserver tier

mkdir roles/
    mkdir roles/common/               # this hierarchy represents a "role"
        mkdir roles/common/tasks/            #
            touch roles/common/tasks/main.yml      #  <-- tasks file can include smaller files if warranted
        mkdir roles/common/handlers/         #
            touch roles/common/handlers/main.yml      #  <-- handlers file
        mkdir roles/common/templates/        #  <-- files for use with the template resource
        mkdir roles/common/files/            #  <-- files for use with the copy resource
        mkdir roles/common/vars/             #
            touch roles/common/vars/main.yml      #  <-- variables associated with this role
        mkdir roles/common/defaults/         #
            touch roles/common/defaults/main.yml      #  <-- default lower priority variables for this role
        mkdir roles/common/meta/             #
            touch roles/common/meta/main.yml      #  <-- role dependencies
        mkdir roles/common/library/          # roles can also include custom modules
        mkdir roles/common/module_utils/     # roles can also include custom module_utils
        mkdir roles/common/lookup_plugins/   # or other types of plugins, like lookup in this case

cat > production << EOF
# file: production

[all]
1.2.3.4

[all:vars]
os=centos
dist=7
timezone='Europe/Sofia'
EOF

cat > staging << EOF
# file: staging

[all]
1.2.3.4

[all:vars]
os=centos
dist=7
timezone='Europe/Sofia'
EOF

cat > ansible.cfg << EOF
[defaults]
roles_path = roles
nocows = 1
EOF

cat > playbooks/all.yml << EOF
- hosts: all
  roles:
    - common
EOF

cat > roles/common/tasks/main.yml << EOF
- name: Update all packages on the OS via yum
  yum:
    name: "*"
    state: latest
  tags: [common, install]

- name: Install suplementary packages via yum
  yum:
    name:
      - epel-release
      - vim
      - nano
      - ntp
    state: present
  tags: [common, install]

- name: Set timezone to "{{ timezone }}"
  timezone:
    name: "{{ timezone }}"
  tags: [common, install, ntp]

- name: Synchronize via NTP
  command: ntpdate pool.ntp.org

- name: Enable and start NTPD
  service:
    name: ntpd
    state: started
    enabled: yes
  tags: [common, install, ntp]
EOF

cat > README.md << EOF
Ansible directory structure layout. You can read more in the official documentation at: https://docs.ansible.com/ansible/latest/user_guide/playbooks_best_practices.html :

production                # inventory file for production servers
staging                   # inventory file for staging environment

group_vars/
   group1                 # here we assign variables to particular groups
   group2                 # ""
host_vars/
   hostname1              # if systems need specific variables, put them here
   hostname2              # ""

library/                  # if any custom modules, put them here (optional)
module_utils/             # if any custom module_utils to support modules, put them here (optional)
filter_plugins/           # if any custom filter plugins, put them here (optional)

playbooks/        # main playbook dir
   all.yml                # playbook for all servers tier
   dbservers.yml          # playbook for dbserver tier

roles/
    common/               # this hierarchy represents a "role"
        tasks/            #
            main.yml      #  <-- tasks file can include smaller files if warranted
        handlers/         #
            main.yml      #  <-- handlers file
        templates/        #  <-- files for use with the template resource
            ntp.conf.j2   #  <------- templates end in .j2
        files/            #
            bar.txt       #  <-- files for use with the copy resource
            foo.sh        #  <-- script files for use with the script resource
        vars/             #
            main.yml      #  <-- variables associated with this role
        defaults/         #
            main.yml      #  <-- default lower priority variables for this role
        meta/             #
            main.yml      #  <-- role dependencies
        library/          # roles can also include custom modules
        module_utils/     # roles can also include custom module_utils
        lookup_plugins/   # or other types of plugins, like lookup in this case

    webtier/              # same kind of structure as "common" was above, done for the webtier role
    monitoring/           # ""
    fooapp/               # ""
EOF

cat << EOF
In order to start:
1) Edit your production and staging inventories
2) Execute ansible-playbook -i (staging|production) playbooks/all.yml
EOF

Comments