
上QQ阅读APP看书,第一时间看更新
Running the role
Now that the role for installing and configuring Apache is complete, we can add it to our playbook:
---
- hosts: boxes
gather_facts: true
become: yes
become_method: sudo
vars_files:
- group_vars/common.yml
roles:
- roles/common
- roles/apache
Following on from the playbook in the previous section, we can simply rerun the following command:
$ ansible-playbook -i production site.yml
This will work through the common role before executing the apache role. I have truncated the output for the common role in the playbook run here:
PLAY [boxes] ***************************************************************************************
TASK [Gathering Facts] *****************************************************************************
ok: [box]
TASK [roles/common : update all of the installed packages] *****************************************
ok: [box]
TASK [roles/common : install the common packages] **************************************************
ok: [box]
TASK [roles/common : copy the ntp.conf to /etc/ntp.conf] *******************************************
ok: [box]
TASK [roles/common : add group for our users] ******************************************************
ok: [box]
TASK [roles/common : add users to our group] *******************************************************
ok: [box]
TASK [roles/common : add keys to our users] ********************************************************
ok: [box]
TASK [roles/apache : install the apache packages] **************************************************
changed: [box] => (item=[u'httpd24u', u'httpd24u-filesystem', u'httpd24u-tools', u'httpd24u-mod_ssl', u'openssl', u'openssl-libs'])
TASK [roles/apache : Add user to apache group] *****************************************************
changed: [box] => (item={u'state': u'present', u'group': u'lamp', u'name': u'lamp', u'key': u'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDmuoFR01i/Yf3HATl9c3sufJvghTFgYzK/Zt29JiTqWlSQhmXhNNTh6iI6nXuPVhQGQaciWbqya6buncQ3vecISx6+EwsAmY3Mwpz1a/eMiXOgO/zn6Uf79dXcMN2JwpLFoON1f9PR0/DTpEkjwqb+eNLw9ThjH0J994+Pev+m8OrqgReFW36a/kviUYKsHxkXmkgxtPJgwKU90STNab4qyfKEGhi2w/NzECgseeQYs1H3klORaHQybhpXkoCIMmgy9gnzSH7oa2mJqKilVed27xoirkXzWPaAQlfiEE1iup+2xMqWY6Jl9qb8tJHRS+l8UcxTMNaWsQkTysLTgBAZ russmckendrick@me.com'})
TASK [roles/apache : create the document root for our website] *************************************
changed: [box]
TASK [roles/apache : set the permissions on the user folder] ***************************************
changed: [box]
TASK [roles/apache : copy the vhost.conf to /etc/httpd/conf.d/] ************************************
changed: [box]
TASK [roles/apache : set the selinux allowing httpd_t to be permissive] ****************************
changed: [box]
TASK [roles/apache : copy the test HTML page to the document root] *********************************
changed: [box]
RUNNING HANDLER [roles/apache : restart httpd] *****************************************************
changed: [box]
PLAY RECAP *****************************************************************************************
box : ok=15 changed=8 unreachable=0 failed=0
Opening http://192.168.50.4.nip.io/ in our browser should give us a page that looks like the following screenshot:

As you can see, the template has picked up all of the variables we defined; the source for the page looks like this:
<!--Ansible managed-->
<!doctype html>
<title>Success !!!</title>
<style>
body { text-align: center; padding: 150px; }
h1 { font-size: 50px; }
body { font: 20px Helvetica, sans-serif; color: #333; }
article { display: block; text-align: left; width: 650px;
margin: 0 auto; }
</style>
<article>
<h1>Success !!!</h1>
<p>
<p>This HTML page has been deployed using Ansible to
<b>192.168.50.4.nip.io</b>.<br>
The user is <b>lamp</b> who is in the <b>apache</b> group.<br>
The weboot is <b>/home/lamp/web</b>, the default index file is
<b>index.html</b>.<br></p>
</p>
</article>
If we were to rerun the playbook, we should see the following results:
PLAY RECAP *****************************************************************************************
box : ok=14 changed=0 unreachable=0 failed=0
As you can see, there are 14 tasks that are ok, and nothing has changed.