gpt4 book ai didi

python - Ansible 条件基于文件的内容

转载 作者:太空狗 更新时间:2023-10-29 22:11:16 25 4
gpt4 key购买 nike

如果有人能指出这有什么问题,我们将不胜感激......

以下代码旨在在寄存器模块中设置一个测试,打印 /etc/timezone 的当前值。然后有一个任务将其与组/主机 {{ timezone }} 变量进行比较,并且仅在不同时才运行该任务(即不会不必要地调用处理程序)。

但无论如何它总是运行。

- name: check current timezone
shell: cat /etc/timezone
register: get_timezone

- name: set /etc/timezone
shell: echo "{{ timezone }}" > /etc/timezone
when: get_timezone.stdout.find('{{ timezone }}') == false
notify: update tzdata

....

在 group_vars/all.yml 中:

timezone: Europe/London

最佳答案

如果找不到子字符串(https://docs.python.org/2/library/string.html,请参阅 string.find),Python string.find 方法将返回 -1。因此,您可以像这样修改您的 yml:

- name: set /etc/timezone
shell: echo "{{ timezone }}" > /etc/timezone
when: get_timezone.stdout.find('{{ timezone }}') == -1
notify: update tzdata

或者只使用“不在”:

- name: set /etc/timezone
shell: echo "{{ timezone }}" > /etc/timezone
when: '"{{ timezone }}" not in get_timezone.stdout'
notify: update tzdata

关于python - Ansible 条件基于文件的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23182905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com