如何在python上的ansible脚本中设置变量值?

2024-05-23 21:00:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这样一本易懂的剧本:

---
-hosts: amqp
 vars:
  id: "123"
 roles:
  -cc

现在,我希望在执行playbook时运行设置ID变量的python脚本

我知道我可以通过以下任务启动脚本:

tasks:
 -name: script starter
  script: idgenerator.py
  args:
    executable: python3

虽然python脚本的相关部分如下所示:

identifier = generate_id()
sys.stdout.write(identifier)

所以我的问题是如何用pythonfile的“return”值设置变量id

谢谢

编辑:多亏了@Zeitounator和他的示例代码,这个问题现在已经解决了,这就是解决方案或可能的方法

pre_tasks:
 -name: script starter
  script: idgenerator.py
  args:
    executable: python3
  register: script_output
 -name: factsetter
  set_fact:
   id: "{{ script_output.stdout }}"

Tags: namepy脚本idoutputstdoutscriptargs