Ansible roles
- Lets create a role for tomcat playbook.
- Refer Here for the changes done to refactor the tomcat playbook into a role.
- In roles main.yml is the default file that gets called, from there we can call many other yaml files using include_tasks Refer Here
- Refer Here for sample
- Ideal usage of role
---
- hosts: webservers
roles:
- common
- role: foo_app_instance
vars:
dir: '/opt/a'
app_port: 5000
- role: foo_app_instance
vars:
dir: '/opt/b'
app_port: 5001
- The tomcat role works with version v10.0.27 (add this to variables). The other possible fix is to make the whole url as a variable.
Ansible Collections
- In Ansible the reusable assets are
- In Ansible we can create custom modules as well by writing python code.
- Ansible collections are collections of
- custom ansible modules
- roles
- Refer Here for the collections created.
Things to discuss
- facts (custom facts)
- inventory
- flush handlers
- parallelism
- gather_facts
- Ansible on windows.
- ansible tower.
Setting custom facts
---
- name: setting some fact
become: yes
hosts: all
tasks:
- name: set some facts
ansible.builtin.set_fact:
message: This is from ansible
team: devops
- name: get the facts
ansible.builtin.debug:
msg: "The facts are team = {{ team }} message = {{ message }}"
Flushing Handlers
- Ansible handlers are executed based on consolidataion of all the tasks in ansible.
- If you want to execute handlers after some step immedietly we use flush_handlers as mentioned below
---
- name: flush handlers demo
hosts: all
tasks:
- name: print something
ansible.builtin.file:
path: "/tmp/handle1"
state: touch
notify:
- handle1
- handle2
- name: Force all notified handlers to run at this point, not waiting for normal sync points
ansible.builtin.meta: flush_handlers
- name: print something again
ansible.builtin.file:
path: "/tmp/handle2"
state: touch
notify:
- handle1
- handle2
handlers:
- name: handle1
ansible.builtin.debug:
msg: "handle1"
- name: handle2
ansible.builtin.debug:
msg: "handle2"
Like this:
Like Loading...