In Jenkins you use labels to define where a project can be built. Once you create a new item you will see (in the job configuration) a checkbox named "Restrict where this project can be run". Here can either use the slave node name (for example slave4.mycompany) or create a label which is a container for multiple slaves. Once properly filled out, this job will always build on the given slave node.
If you need more functionality than that, try the NodeLabel Parameter Plugin (nodelabelparameter). You can install it from Manage Jenkins > Manage Plugins > Available.
To identify which node you are on during the build, you can for instance manually set persistent environment variables on both build slaves, as example $SLAVENAME returning "buildslave1", or use the linux "hostname" command in your scripts to identify the current context, and based on that output decide how to proceed.