How to Create an Ansible Playbook

An Ansible playbook is a list of modules and roles (with variables as needed) to execute on remote machines in your inventory.yml in sequential order. Below we cover the basic format to create an Ansible playbook with an example.

Is your Ansible agent getting too resource-intensive for your computer? Migrate it to our Cloud VPS Hosting today and continue your mission.

Create a Playbook

  1. Log into SSH
  2. Navigate to your Ansible root directory
  3. Create a new .yml file: touch playbook-name.yml
  4. Edit your playbook: nano playbook-name.yml

Sample Playbook

After creating a playbook file, add the sample playbook below and make changes as needed.

Remove comments after # as preferred.

---
   name: playbook-name # shows while running, optional but recommended for debugging
   hosts: group-1:group-2 # hosts in inventory.yml, “:” means group-1 “and” group-2
   become: no # Whether Ansible login as a regular user and “become” root - yes or no
   vars: # Edit variables in roles’ or /vars files
      wp_domain: ‘example.com’ # Example variable
   tasks: # Roles, modules, and any variables
   - name: Install package with yum
     yum: #  Module or role parameters to change
       name: Insert Index Page
       template:
         src: index.html
         dest: /usr/share/nginx/html/index.html
     - name: Start NGiNX # Example module with parameters to change
       service:
         name: nginx
         state: started

Learn more about IT automation in our Ansible Education Channel.

Leave a Reply