-
-
Save ober/ef8cdc047b3d52159c3f to your computer and use it in GitHub Desktop.
ansible - example of custom module to gather info on installed packages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# only works on RedHat family | |
import rpm | |
def _check_installed(module): | |
ts = rpm.TransactionSet() | |
mi = ts.dbMatch() | |
results = [] | |
for h in mi: | |
results.append(h['name']) | |
return { 'ansible_facts': { 'packages_installed': results } } | |
def main(): | |
module = AnsibleModule( | |
argument_spec = dict( | |
), | |
supports_check_mode = True, | |
) | |
data = _check_installed(module) | |
module.exit_json(**data) | |
from ansible.module_utils.basic import * | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PLAY [foo] ******************************************************************** | |
GATHERING FACTS *************************************************************** | |
ok: [localhost] | |
TASK: [gather_installed ] ***************************************************** | |
ok: [localhost] | |
TASK: [debug var=hostvars[inventory_hostname]['packages_installed']] ********** | |
ok: [localhost] => { | |
"hostvars[inventory_hostname]['packages_installed']": [ | |
"perl-version", | |
"xz-lzma-compat", | |
"fontpackages-filesystem", | |
"perl-Digest-SHA", | |
"openconnect", | |
"libattr", | |
... | |
"python-pycurl", | |
"dhclient", | |
"libICE" | |
] | |
} | |
TASK: [debug msg='do something when httpd is installed'] ********************** | |
ok: [localhost] => { | |
"msg": "do something when httpd is installed" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- hosts: foo | |
tasks: | |
- gather_installed: | |
- debug: var=hostvars[inventory_hostname]['packages_installed'] | |
- debug: msg='do something when httpd is installed' | |
when: "'httpd' in packages_installed" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment