Debian Codegraph @

ansible_collections/community/general/plugins/modules/say.py

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2013, Michael DeHaan <[email protected]>
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7 from __future__ import annotations
8
9 DOCUMENTATION = r"""
10 module: say
11 short_description: Makes a computer to speak
12 description:
13 - Makes a computer speak! Amuse your friends, annoy your coworkers!
14 notes:
15 - In 2.5, this module has been renamed from C(osx_say) to M(community.general.say).
16 - If you like this module, you may also be interested in the osx_say callback plugin.
17 - A list of available voices, with language, can be found by running C(say -v ?) on a OSX host and C(espeak --voices) on
18 a Linux host.
19 extends_documentation_fragment:
20 - community.general._attributes
21 attributes:
22 check_mode:
23 support: full
24 diff_mode:
25 support: none
26 options:
27 msg:
28 type: str
29 description:
30 - What to say.
31 required: true
32 voice:
33 type: str
34 description:
35 - What voice to use.
36 requirements: [say or espeak or espeak-ng]
37 author:
38 - "Ansible Core Team"
39 - "Michael DeHaan (@mpdehaan)"
40 """
41
42 EXAMPLES = r"""
43 - name: Makes a computer to speak
44 community.general.say:
45 msg: '{{ inventory_hostname }} is all done'
46 voice: Zarvox
47 delegate_to: localhost
48 """
49 import platform
50
51 from ansible.module_utils.basic import AnsibleModule
52
53
54 def say(module, executable, msg, voice):
55 cmd = [executable, msg]
56 if voice:
57 cmd.extend(("-v", voice))
58 module.run_command(cmd, check_rc=True)
59
60
61 def main():
62 module = AnsibleModule(
63 argument_spec=dict(
64 msg=dict(required=True),
65 voice=dict(),
66 ),
67 supports_check_mode=True,
68 )
69
70 msg = module.params["msg"]
71 voice = module.params["voice"]
72 possibles = ("say", "espeak", "espeak-ng")
73
74 if platform.system() != "Darwin":
75 # 'say' binary available, it might be GNUstep tool which doesn't support 'voice' parameter
76 voice = None
77
78 for possible in possibles:
79 executable = module.get_bin_path(possible)
80 if executable:
81 break
82 else:
83 module.fail_json(msg=f"Unable to find either {', '.join(possibles)}")
84
85 if module.check_mode:
86 module.exit_json(msg=msg, changed=False)
87
88 say(module, executable, msg, voice)
89
90 module.exit_json(msg=msg, changed=True)
91
92
93 if __name__ == "__main__":
94 main()