假设我想从 ansible 运行以下任务:
- name: dns-cloudflare | Request certificate
ansible.builtin.shell:
cmd: certbot certonly \
--dns-cloudflare \
--dns-cloudflare-credentials {{ certbot_cloudflare_credentials_file }} \
--non-interactive \
--agree-tos \
--expand \
--email {{ certbot_email }} \
--domain {{ __domains }}
args:
executable: /bin/bash
creates: "{{ certbot_directory }}/{{ item.name }}/{{ item.name }}/cert.pem"
我用这样的块构建它:
certbot_cert:
- name: test1.example.com
cn:
- test2-cn.example.com
post-hook: systemctl restart apache2
- name: test3.example.com
我想根据是否将 --post-hook 争论添加到我的 certbot 命令中item.post-hook
。而且这个钩子的内容不是静态的,无论我在那里输入什么,都应该落在我的 certbot 命令中,例如--post-hook 'systemctl restart apache2'
但是当我没有定义这个变量时,不应该有一个空的后钩子,它根本不应该出现在 certbot 命令中。
我怎样才能实现这个目标?
我尝试了变量和过滤器,并阅读了文档,但没有找到正确的方法来处理 is。不应该为每个可能的可选参数多次重复此任务。
您编写 的值的方式
cmd
不会保留换行符 - 这就是您想要的,因为这意味着 (a) 您不需要\
像在示例中所做的那样转义 ( ) 行结尾,并且(b) 您可以方便地使用 jinja 条件来抑制某些行。像这样:
对于 中的第一项
certbot_cert
,将运行:对于第二行,它将运行: