Ansible Playbook that runs multiple plays

Below is an example of a Ansible Playbook which runs multiple 
plays. Each play has its specific tasks. You can also define
specific hosts group as well.
- name: Play 1 - Install some dependencies
  hosts: web
  tasks:
    - name: update yum cache
      yum:
        update_cache: yes
      become: true

    - name: Install Apache web server
      yum:
        name: httpd
        state: present
      become: true

- name: Play 2 - Configure web servers
  hosts: web
  tasks:
    - name: Copy some website files
      copy:
        src: /path/to/website_files
        dest: /var/www/html
      become: true

    - name: Make sure Apache is running and enabled
      service:
        name: httpd
        state: started
        enabled: yes
      become: true

- name: Play 3 - Its time to set up database server
  hosts: database_server
  tasks:
    - name: Install MySQL server
      yum:
        name: mysql-server
        state: present
      become: true

    - name: Start MySQL service
      service:
        name: mysqld
        state: started
        enabled: yes
      become: true
In above playbook, we have below three plays:

1. Play 1 - Install apache server and its dependencies.
2. Play 2 - Configure web server with its details.
3. Play 3 - Install and configure mysql database.

Now it's time to run
playbook. You can use below command to run playbook.
# ansible-playbook multiplay.yaml

Leave a Reply

Your email address will not be published. Required fields are marked *