Use the attribute when to define that some task can just be executed when some conditional be attended. Following an example.
- name: Run tasks when userid exist
include: tasks.yml
vars:
services:
- firwalld
- httpd
when: userid is defined
- name: Copy file when the host is a webservice
copy:
src: files/index.html
dest: /var/www/html/index.html
when: inventory_hostname in groups["webservice"]
Check here the operators and see more about it in the Ansible docs.