简体   繁体   English

Ansible - 测试是否调用 python 脚本确实通过 Ansible 运行

[英]Ansible - Testing if called python script is indeed running via Ansible

I'm looking for a way to test, in my python script, if said script is running from Ansible so I can also run it through shell (for running unit tests etc).我正在寻找一种测试方法,在我的 python 脚本中,如果所述脚本从 Ansible 运行,那么我也可以通过 shell 运行它(用于运行单元测试等)。 Calling AnsibleModule without calling from an ansible playbook will just endlessly wait for a response that will never come.在没有从 ansible 剧本调用的情况下调用 AnsibleModule 只会无休止地等待永远不会出现的响应。

I'm expecting that there isn't a simple test and that I have to restructure in some way, but I'm open to any options.我期待没有一个简单的测试,我必须以某种方式进行重组,但我对任何选择持开放态度。

def main():
# must test if running via ansible before next line
module = AnsibleModule(
    argument_spec=dict(
        server=dict(required=True, type='str'),
        [...]
    )
    [... do things ...]
)

if __name__ == "__main__":
    if running_via_ansible:
        main()
    else:
        run_tests()

I believe there are a couple of answers, with various levels of trickery involved我相信有几个答案,涉及不同程度的诡计

  1. since your module is written in python, ansible will use the AnsiballZ framework to run it, which means its sys.argv[0] will start with AnsiballZ_ ;由于您的模块是用 python 编写的,因此 ansible 将使用AnsiballZ 框架来运行它,这意味着它的sys.argv[0]将以AnsiballZ_ it will also likely be written to $HOME/.ansible/tmp on the target machine, so one could sniff for .ansible/tmp showing up in argv[0] also它也可能会被写入目标机器上的$HOME/.ansible/tmp ,因此也可以嗅探.ansible/tmp出现在argv[0]
  2. if the file contains the string WANT_JSON in it, then ansible will invoke it with the module's JSON payload as the first argument instead of feeding it JSON on sys.stdin (thus far the filename has been colocated with the AnsiballZ_ script, but I don't know that such a thing is guaranteed) if the file contains the string WANT_JSON in it, then ansible will invoke it with the module's JSON payload as the first argument instead of feeding it JSON on sys.stdin (thus far the filename has been colocated with the AnsiballZ_ script, but I don'不知道这样的事情是有保证的)
  3. Similar, although apparently far more python specific: if it contains a triple-quoted sentinel """<<INCLUDE_ANSIBLE_MODULE_JSON_ARGS>>""" (or the ''' flavor works, too) then that magic string is replaced by the serialized JSON that, again, would have been otherwise provided via stdin类似的,虽然显然更多的是 python 特定:如果它包含三引号标记"""<<INCLUDE_ANSIBLE_MODULE_JSON_ARGS>>""" (或'''风格也有效),那么该魔术字符串将被序列化的 JSON 替换,同样,本来是通过标准输入提供的

While this may not apply, or be helpful, I actually would expect that any local testing environment would have more "fingerprints" than trying to detect the opposite, and has the pleasing side-effect of "failing open" in that the module will assume it is running in production mode unless it can prove testing mode, which should make for less weird false positives.虽然这可能不适用或有帮助,但我实际上希望任何本地测试环境都比试图检测相反的环境具有更多的“指纹”,并且具有“失败打开”的令人愉快的副作用,因为模块将假设它在生产模式下运行,除非它可以证明测试模式,这应该会减少奇怪的误报。 Then again, I guess the reasonable default depends on how problematic it would be for the module to attempt to carry out its payload when not really in use再说一次,我猜合理的默认值取决于模块在不真正使用时尝试执行其有效负载会有多大问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM