Here is the ansible playbook with multiple hosts in it. You can see we are working with web and application servers in the same playbook and executing two different plays (set of tasks) respectively.
--- # Play1 - WebServer related tasks - name: Play Web - Create apache directories and username in web servers hosts: webservers become: yes become_user: root tasks: - name: create username apacheadm user: name: apacheadm group: users,admin shell: /bin/bash home: /home/weblogic - name: install httpd yum: name: httpd state: installed # Play2 - Application Server related tasks - name: Play app - Create tomcat directories and username in app servers hosts: appservers become: yes become_user: root tasks: - name: Create a username for tomcat user: name: tomcatadm group: users shell: /bin/bash home: /home/tomcat - name: create a directory for apache tomcat file: path: /opt/oracle owner: tomcatadm group: users state: present mode: 0755
In the preceding playbook, you could notice that two plays are there and both of them targeted to different host groups.
![](https://i0.wp.com/myfuturehub.com/wp-content/uploads/2022/08/MFH_DevOps-2.png?resize=112%2C158&ssl=1)