Ansible Usage

Basic Usage


List all modules

ansible-doc -l

Show documentation for ping module

ansible-doc ping

Ad-hoc commands

Restart a service

ansible testServer -i hosts -u sysadmin -m service -a "name=httpd state=restarted" -b

Basic shell command

ansible testServer -i hosts -u sysadmin -a "df -h"

Execute a Playbook

Run playbook myservice.yml

ansible-playbook -i hosts myservice.yml

Run playbook with extra-vars

ansible-playbook -i hosts -e "HTTPD_PORT=8080" myservice.yml

Ansible Project Usage

A sample directory structure

This is a simplified view. There are more possible subfolders wich are read by ansible. They are mostly optional.

inventory                 # inventory file for your environment

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

site.yml                  # master playbook
webservers.yml            # playbook for webserver tier
dbservers.yml             # playbook for dbserver tier

    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
          #  <-- 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

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

Initiate a Role directory structure

This creates the basic directory structure for a new Role inside your playbook.

ansible-galaxy init myRole