[Ansible] – Understanding playbooks

Ansible playbooks are YAML files which are implemented to run ansible tasks. Following a basic ansible playbook file structure.

--- 
# Here we can comment
- name: Ansible file example 
  hosts: myhost.example.com 
  tasks:
  - name: Install httpd by yum command
    yum:
      name: httpd
      state: latest
  - name: Check the connection
    ping: 
...

Following the command to check all options to work with playbooks

$ man ansible-playbook

Following few useful ansible playbooks useful commands.

–syntax-check: Will check the syntax of the file (E.g ansible-playbook –syntax-check myfile.yml)

–check: Will check the execution of the playbook. (E.g ansible-playbook –check myfile.yml)

Following the command to execute the playbook

$ ansible-playbook myfile.yml

How to configure playbooks to run different tasks in multi hosts?

---
# Example of multi tasks e multi hosts
- name: Run task in server A
  hosts: serverA.example.com
  tasks:
  - name: Check if exists user
    user: # ansible module
      name: john
      uid: 5400
      state: present
  
  - name: Check if httpd is enabled
    service:
      name: httpd
      enabled: true

  - name: Check if NTP server is enabled
    service:
      name: chronyd
      enabled: true

  - name: Check if postfix is enabled
    service:
      name: postfix
      enabled: true

-  name: Run task in server B
   hosts: serverB.example.com
   tasks: # tasks to run in the server B
   - name: Copy file
     copy:
       dest: /etc/myfile
       content: "Add here content of my file\n"
...

Following some references for the Ansible documentation.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s