SQL Server AlwaysOn High Availability Maintenance Plans
The best way is to use Ola Hallengren's maintenance plans, because the ability to determine AGs in the maintenance is already "baked in". However, if for some reason you don't want to or can't use his maintenance plans, then you'll need to insert the deterministic function, 
If sys.fn_hadr_is_primary_replica ( @dbname ) &lt;&gt; 1
BEGIN
-- If this is not the primary replica, exit (probably without error).
END
-- If this is the primary replica, continue to do the backup., 
to figure out if the database resides in the primary node or not. The example comes from: https://docs.microsoft.com/en-us/sql/relational-databases/system-functions/sys-fn-hadr-is-primary-replica-transact-sql