Question concerning multiple -hosts: entries in a playbook

There might be a better way to do this, but I’m unaware of it, so I’m asking the list. I have a playbook that has two parts to it:

  1. Pulls branch from git repo to the localhost (ie the Ansible server)
    1a. compiles the code (it’s a PHAR file, so nothing too onerous)
  2. Pushes the code to our external servers.

Part 1/1a has a hosts: entry of ‘localhost’ and part 2 has a hosts: entry for our external servers that are getting the updated code.

My question is, is there a way to abstract the second hosts: line so I can specify specific hosts/groups? IOW, if I set that hosts: line to ‘production’, is there a way to specify on the command line -i dbservers or other subset instead of manually editing that line every time?

Second question, if the above question is just wrong/bad/not sane, is there a better way to do this?

You can set the 2nd play like this:

- hosts: '{{target|default('devel')}}'

then call it:

ansible-playbook ... -e 'target=production'

Huh. I hadn't thought about using that method. I'll give it a shot. Thanks.