Prompt user during execution
Creates Dynamic Playbooks
Playbook Handlers:
Templates:
All concepts in a single file:
One more sample:
index.html.j2
- hosts: web1 vars_prompt: - name: "sitename" prompt: "What is new site name?" tasks: - debug: msg="The name is {{ sitename }}"
Creates Dynamic Playbooks
Playbook Handlers:
- Tasks with asynchronous execution.
- Only runs tasks when notified
- Tasks only notify when state=changed
- Does not run until all playbook tasks have executed
- Most common for restarting services to load changes (if changes are made)
- tasks: web1 - copy: src=files/httpd.conf dest=/etc/httpd/conf/
notify:
- Apache Restart
handlers:
- name: Apache Restart
service: name=httpd state=restarted
- Copies config file to host
- If state=change on "COPY", tell "Apache Restart"
- Run "Service" module.
Conditional Clause:
use the clause "when" to choose if task should run.
Choose when to execute tasks.
- Use YUM if OS is RedHat
- Use APT if OS is Debian
To check OS from adhoc : ansible host1 -m setup -a "filter=*family*"
- tasks: - yum: name=httpd state=present
when: ansible_os_family == "RedHat"
- apt: name=apache2 state=present
when: ansible_os_family == "Debian"
Conditional Clause Based on Output:
Track whether previous task ran.
Searches JSON Result for status.
Status Options:
- success
- failed
- skipped
- tasks: - command: ls /path/doesnt/exist
register: result
ignore_errors: yes
- debug: msg="Failure!"
when: result|failed
---
- hosts: gui
become: yes
tasks:
- name: install apache2
apt: name=apache2 state=latest
ignore_errors: yes
register: results
- name: install httpd
yum: name=httpd state=latest
when: results|failed
Templates:
Uses Jinja2 Engine: Insert variables into static files
Creates and copies dynamic files: Deploy custom configurations
- tasks: - template:
src=templates/httpd.j2
dest=/et/httpd/conf/httpd.conf
owner=httpd
- Takes a file with pre-defind variable names.
- Inserts variable values in file
- Copies file to destination
jinja2 looks like below:
All concepts in a single file:
One more sample:
---
- hosts: gui
become: yes
vars:
file_version: 1.0
tasks:
- name: install index
template:
src: index.html.j2
dest: /var/www/html/index.html
mode: 0777
index.html.j2
<html>
<center>
<h1>This computer's hostname is {{ ansible_hostname }}</h1>
<h3>It is running {{ ansible_os_family }} family of operating system</h3>
<small>This file is version {{ file_version }}</small>
{# this will not end up in the final output file on the remote server #}
</center>
</html>
No comments:
Post a Comment