Learning Ansible 2.7(Third Edition)
上QQ阅读APP看书,第一时间看更新

Publishing a website

Since our website will be a simple, single-page website, we can easily create it and publish it using a single Ansible task. To make this page a little bit more interesting, we will create it from a template that will be populated by Ansible with a little data about the machine. The script to publish it will be called deploy_website.yaml, and will have the following content:

--- 
- hosts: all
remote_user: ansible
tasks:
- name: Ensure the website is present and updated
template:
src: index.html.j2
dest: /var/www/html/index.html
owner: root
group: root
mode: 0644
become: True

Let's start with a simple template that we will call index.html.j2:

<html> 
<body>
<h1>Hello World!</h1>
</body>
</html>

Now, we can test our website deployment by running the following:

$ ansible-playbook -i test01.fale.io, deploy_website.yaml  

We should receive the following output:

PLAY [all] ***********************************************

TASK [Gathering Facts] ***********************************
ok: [test01.fale.io]

TASK [Ensure the website is present and updated] *********
changed: [test01.fale.io]

PLAY RECAP ***********************************************
test01.fale.io : ok=2 changed=1 unreachable=0 failed=0

If you now go to your IP/FQDN test machine in your browser, you'll find the Hello World! page.