Chapter 7. Complex Playbooks
In the last chapter, we went over a fully functional Ansible playbook for deploying the Mezzanine CMS. That example exercised a number of common Ansible features, but it didn’t cover all of them. This chapter touches on those additional features, which makes it a bit of a grab bag.
Running a Task on the Control Machine
Sometimes you want to run a particular task on the control machine instead of
on the remote host. Ansible provides the local_action clause for tasks to
support this.
Imagine that the server we wanted to install Mezzanine onto had just booted, so that if we ran our playbook too soon, it would error out because the server hadn’t fully started up yet.
We could start off our playbook by invoking the wait_for module to wait until
the SSH server was ready to accept connections before we executed the rest
of the playbook. In this case, we want this module to execute on our laptop, not
on the remote host.
The first task of our playbook would have to start off like this:
-name:wait for ssh server to be runninglocal_action:wait_for port=22 host="{{inventory_hostname}}"search_regex=OpenSSH
Note how we’re referencing inventory_hostname in this task, which evaluates to the name of the remote
host, not localhost. That’s because the scope of these variables is still the
remote host, even though the task is executing locally.
Note
If your play involves multiple hosts, and you use local_action, the task will be executed multiple times, one for ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access