Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MQTT notification callback plugin #23769

Closed
wants to merge 1 commit into from

Conversation

mtreinish
Copy link
Contributor

This commit adds a new callback for emitting mqtt notifications for
tasks and playbooks.

SUMMARY

This commit adds a new callback module for emitting mqtt notifications for
tasks and playbooks.

ISSUE TYPE
  • New Module Pull Request
COMPONENT NAME
ANSIBLE VERSION
ansible 2.4.0 (detached HEAD c3cd60722f) last updated 2017/04/19 16:12:09 (GMT -400)
ADDITIONAL INFORMATION

@ansibot ansibot added affects_2.4 This issue/PR affects Ansible v2.4 c:plugins/callback needs_triage Needs a first human triage before being processed. new_plugin This PR includes a new plugin. labels Apr 19, 2017
@abadger abadger removed the needs_triage Needs a first human triage before being processed. label Apr 20, 2017
out_topic = self.base_topic + '/' + topic
publish.single(out_topic, msg, hostname=self.mqtt_hostname,
port=self.port, auth=self.auth, tls=self.tls,
client_id=self.client_id)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think adding a timestamp field would be nice.

CALLBACK_NAME = 'mqtt'
CALLBACK_NEEDS_WHITELIST = True

def _parse_config(self, config):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a thought, you could leverage get_config from constants.py to allow end users to use env variables or ansible.cfg as they prefer instead of relying on a different config file.

Example usage in ARA: https://github.com/openstack/ara/blob/master/ara/config.py

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 on get_config for clarity.

@dmsimard
Copy link
Contributor

dmsimard commented May 2, 2017

Pinged you on IRC about it but for posterity...
I noticed you don't have item methods in the callback (ex: v2_runner_item_on_ok).

When dealing with a task that has items (ex: with_items), the result._result var of the task is actually pretty empty -- it doesn't contain the results of the items, just a summary message like "All items completed".

The results are kept in the items' result variables. Maybe you want to pick those up, I don't know.
Example implementation: https://review.openstack.org/#/c/461612/3/ara/plugins/callbacks/log_ara.py

Note that I just filed a bug about the inconsistent callback execution with items and task completion: #24207

@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label May 2, 2017
@mtreinish
Copy link
Contributor Author

@dmsimard The latest revision fixes mots of your comments. I still have to investigate the config switch, I haven't had a chance to dig into that yet though.

@ansibot ansibot removed the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label May 28, 2017
@ansibot ansibot added stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels Jun 23, 2017
@ansibot ansibot removed the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Jul 6, 2017
@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Jul 18, 2017
Copy link
Contributor

@omgjlk omgjlk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some nits, but I think there's one actual problem to address.


Optionally these values can be set via a yaml file in /etc/mqtt_client.yaml,
or $HOME/.mqtt_client.yaml (where each env variable lowercase is a key in
the yaml files)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like some things could be in ENV, some could be in the user's dot file, and some could be in /etc/ and as long as they aren't overlapping, it'll gather all the parts from all the files. That's fine, but might want to mention it in the docs (could be as a follow up after the fact too). Additionally the ordering should be documented as well (ENV, user dir, /etc/ dir)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point, I say below that ENV takes first priority, but I should outline the config file priority too. I've got to respin this anyway, I'll add that bit on in the next rev

def _publish(self, topic, msg):
out_topic = self.base_topic + '/' + topic
msg['timestamp'] = datetime.datetime.utcnow().isoformat()
publish.single(out_topic, json.dumps(msg),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this ever raise an error? Should it be trapped and logged correctly, or is there infra around callbacks that takes care of this already?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that's true, I don't know if there is any infra around callback plugins to do that or not. (I'd have to dig through the call path to find out, most of the interface isn't documented). It probably doesn't hurt to collect the errors either way though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, looking at it a bit more closely I'm not sure how to setup logging for something like this from the plugin context, I couldn't find any good examples of that to base it off of. If you have any suggestions I'll add that on in a future patch.

'status': "FAILED",
'host': self.hostname,
'ansible_host': host,
'playbook_id': self.session,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Above playbook_id was set to self.uuid, but here it's being set to self.session which above is used for session. Maybe some copy/pasta got messed up?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, yep that's a good catch. I'll respin this now

@ansibot ansibot added the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. label Oct 7, 2017
@mtreinish
Copy link
Contributor Author

The latest revision addresses the review comments from @j2sol

@ansibot ansibot removed the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Oct 16, 2017
@ansibot
Copy link
Contributor

ansibot commented Oct 16, 2017

The test ansible-test sanity --test pylint [?] failed with the following error:

lib/ansible/plugins/callback/mqtt.py:224:15: duplicate-key Duplicate key 'ansible_host' in dictionary

click here for bot help

This commit adds a new callback for emitting mqtt notifications for
tasks and playbooks.
Copy link
Contributor

@omgjlk omgjlk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good know to me. If I get info on handling tracebacks in a callback I'll respond.

@omgjlk
Copy link
Contributor

omgjlk commented Oct 17, 2017

shipit

@ansibot ansibot removed the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. label Oct 17, 2017
@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Oct 25, 2017
@ansibot ansibot added support:community This issue/PR relates to code supported by the Ansible community. and removed support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels Sep 19, 2018
@ansibot ansibot added the community_review In order to be merged, this PR must follow the community review workflow. label Oct 25, 2018
@jpmens
Copy link
Contributor

jpmens commented Nov 29, 2018

shipit

MQTT_KEYFILE (optional): The path pointing to the PEM encoded client
private key
MQTT_CLIENT_ID (optional): MQTT client identifier, defaults to
hostname + pid
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This callback needs to be updated to utilize "config". See other current callback plugins for their DOCUMENTATION and set_options method.

@ssbarnea
Copy link
Member

@bcoca can you give us a hand with that?

@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed community_review In order to be merged, this PR must follow the community review workflow. labels Nov 29, 2018
@ansibot ansibot added community_review In order to be merged, this PR must follow the community review workflow. and removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Mar 4, 2020
@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed community_review In order to be merged, this PR must follow the community review workflow. labels Mar 28, 2020
@Akasurde
Copy link
Member

Please submit new callback plugin requests to https://github.com/ansible-collections/community.general. Thanks for your contribution.

@Akasurde Akasurde closed this Aug 19, 2020
@ansible ansible locked and limited conversation to collaborators Sep 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.4 This issue/PR affects Ansible v2.4 c:plugins/callback needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. new_plugin This PR includes a new plugin. plugins/callback stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. support:community This issue/PR relates to code supported by the Ansible community.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

10 participants