having an ansible playbook that will run on all hosts including Debian based & RedHat based servers
this will install mariadb on RHEL based servers only doing some configuration changes, start the service once the service started handlers are used to change the root password but the problem is handlers seems to be applied on all the hosts instead of RHEL based servers only, when statement is not working with handlers, check the following code:
---
- name: "Install & Configure MariaDB server on RHEL8"
hosts: all
vars:
required_distribution: "RedHat"
required_version: "8"
my_packages:
- mariadb-server
- python3-PyMySQL
tasks:
- name: "check & install MariaDB on RHEL 8 only"
yum:
name: "{{ item }}"
state: present
loop: "{{ my_packages }}"
when: "ansible_distribution == required_distribution and ansible_distribution_major_version == required_version"
- name: "start & enable MariaDb service"
service:
name: mariadb
state: started
enabled: true
when: "ansible_distribution == required_distribution and ansible_distribution_major_version == required_version"
notify:
- root_password
handlers:
- name: root_password
mysql_user:
name: root
password: password
when: "ansible_distribution == required_distribution and ansible_distribution_major_version == required_version"
getting this warning:
RUNNING HANDLER [root_password] ****************************************************************************************************************************************************
task path: /home/student/labs/lab6/lab6.yaml:29
[WARNING]: Module did not set no_log for update_********
changed: [server-b] => {"changed": true, "msg": "Password updated (new style)", "user": "root"}
changed: [server-a] => {"changed": true, "msg": "Password updated (new style)", "user": "root"}
changed: [server-d] => {"changed": true, "msg": "Password updated (new style)", "user": "root"}
changed: [server-c] => {"changed": true, "msg": "Password updated (new style)", "user": "root"}
META: ran handlers
META: ran handlers
what I am considering here is the handler is also applying on ubuntu-a server & giving me "[WARNING]: Module did not set no_log for update_**"
if this warning is not coming from ubuntu-a server then how to remove this and what’s the issue in my playbook, is this only applied on RHEL based servers on handlers as well?
I am also looking for some effective way of writing the playbook so that each time I don’t have to write when block to filter out my managed hosts it must be checked once (may be at play level) and will apply on all tasks
2
Answers
Given the inventory
Q: "Effective way of writing the playbook to filter out my managed hosts."
A: Use inventory plugin constructed. See
and run the playbook
This will create the cache
Test the inventory
You can see that the plugin constructed created groups centos_8 and ubuntu_20.
gives
Q: "The handler is also applying on Ubuntu."
A: In your code, there is no reason for this. For example, given the data
the tasks
give
Your condition works as expected
gives
You don’t have to quote the condition
You can format it
, or even better
All options give the same result.
Test the handler. There is no reason to put the condition into the handler when the condition is in the task that will trigger it
The task below will trigger the handler for required_distribution and required_version only
gives
I don’t think that message indicates the handler is running on Ubuntu, but it’s hard to say without seeing your inventory and other more info.
Anyways, if you want to only apply when "when" once (good impulse, you have two options:
Put all of your tasks in a "block" and apply the when to the block. You shouldn’t even need then when on the handler if all of the tasks are skipped using the same when.
If what you really want is to bail on the play altogether on non RedHat 8 hosts, you could use end_host like this, as the first task in your play.