filmov
tv
Advanced Features in Ansible Playbooks

Показать описание
Solution
Begin by logging in to the Ansible control node using the credentials on the hands-on lab page:
Become the ansible user:
sudo -i -u ansible
Use ansible-vault to protect the confidential information
Use ansible-vault to encrypt /home/ansible/confidential to protect the confidential information stored within using the password "I love ansible".
Run ansible-vault encrypt /home/ansible/confidential and supply the password "I love ansible".
Create a playbook that deploys httpd on webservers
---
- hosts: webservers
become: yes
vars_files:
- /home/ansible/confidential
tasks:
- name: install httpd
yum:
name: httpd
state: latest
notify: httpd service
tags:
- base-install
handlers:
- name: Restart and enable httpd
service:
name: httpd
state: restarted
enabled: yes
listen: httpd service
Deploy the templates stored on the control node to the webservers group
- name: configure virtual host
template:
notify: httpd service
tags:
- vhost
- name: configure site auth
template:
src: /home/ansible/htpasswd.j2
dest: /etc/httpd/conf/htpasswd
notify: httpd service
tags:
- vhost
Asynchronously execute data-job on webservers
- name: run data job
async: 600
poll: 0
tags:
- data-job
Your complete file should look similar to the below:
---
- hosts: webservers
become: yes
vars_files:
- /home/ansible/confidential
tasks:
- name: install httpd
yum:
name: httpd
state: latest
notify: httpd service
tags:
- base-install
- name: configure virtual host
template:
notify: httpd service
tags:
- vhost
- name: configure site auth
template:
src: /home/ansible/htpasswd.j2
dest: /etc/httpd/conf/htpasswd
notify: httpd service
tags:
- vhost
- name: run data job
async: 600
poll: 0
tags:
- data-job
handlers:
- name: Restart and enable httpd
service:
name: httpd
state: restarted
enabled: yes
listen: httpd service
Execute playbook to verify your playbook works correctly
Begin by logging in to the Ansible control node using the credentials on the hands-on lab page:
Become the ansible user:
sudo -i -u ansible
Use ansible-vault to protect the confidential information
Use ansible-vault to encrypt /home/ansible/confidential to protect the confidential information stored within using the password "I love ansible".
Run ansible-vault encrypt /home/ansible/confidential and supply the password "I love ansible".
Create a playbook that deploys httpd on webservers
---
- hosts: webservers
become: yes
vars_files:
- /home/ansible/confidential
tasks:
- name: install httpd
yum:
name: httpd
state: latest
notify: httpd service
tags:
- base-install
handlers:
- name: Restart and enable httpd
service:
name: httpd
state: restarted
enabled: yes
listen: httpd service
Deploy the templates stored on the control node to the webservers group
- name: configure virtual host
template:
notify: httpd service
tags:
- vhost
- name: configure site auth
template:
src: /home/ansible/htpasswd.j2
dest: /etc/httpd/conf/htpasswd
notify: httpd service
tags:
- vhost
Asynchronously execute data-job on webservers
- name: run data job
async: 600
poll: 0
tags:
- data-job
Your complete file should look similar to the below:
---
- hosts: webservers
become: yes
vars_files:
- /home/ansible/confidential
tasks:
- name: install httpd
yum:
name: httpd
state: latest
notify: httpd service
tags:
- base-install
- name: configure virtual host
template:
notify: httpd service
tags:
- vhost
- name: configure site auth
template:
src: /home/ansible/htpasswd.j2
dest: /etc/httpd/conf/htpasswd
notify: httpd service
tags:
- vhost
- name: run data job
async: 600
poll: 0
tags:
- data-job
handlers:
- name: Restart and enable httpd
service:
name: httpd
state: restarted
enabled: yes
listen: httpd service
Execute playbook to verify your playbook works correctly