【发布时间】:2020-01-25 16:07:16
【问题描述】:
我正在尝试使用 python 脚本执行以下剧本。
playbook = dict(
name = "Enable Site",
hosts = [host],
gather_facts = 'no',
tasks = [
dict(action=dict(
module='find', args=dict(paths="/etc/apache2/sites-enabled")), register='files_found'),
dict(action=dict(
module='shell', args="cd /etc/apache2/sites-enabled && a2dissite *"), register='shell_out', when='files_found.matched > 0'),
dict(action=dict(module='shell', args="a2ensite " + site_name), register='shell_out'),
dict(action=dict(module='service', args="name='apache2' state='reloaded'"), register='shell_out'),
]
)
这个 playbook 主要检查是否启用了任何 apache 站点,如果是,则通过从 /etc/apache2/sites-enabled 中删除所有文件来禁用它们。
第二个任务应该在/etc/apache2/sites-enabled 目录为空时执行。但是“何时”条件总是被评估为真。即使我写when="False"。也试过when="eval(False)"
【问题讨论】:
-
我不知道 Ansible,但你的比较不是倒过来的吗?看起来您希望它在找到匹配的文件 时运行。
-
@Chris 是的,这就是我想要的
-
你到底想如何运行这个剧本?
-
@larsks 使用以下示例代码docs.ansible.com/ansible/latest/dev_guide/developing_api.html
标签: python ansible ansible-runner