基于条件的Ansible惯用目的地/目的地
我正在写一个剧本,理想的情况是更新文件中的一个块,但是目标文件可以是基于某种条件的大约20个文件中的任何一个。这无疑是错误的,但我想要的是:基于条件的Ansible惯用目的地/目的地,ansible,ansible-playbook,ansible-2.x,Ansible,Ansible Playbook,Ansible 2.x,我正在写一个剧本,理想的情况是更新文件中的一个块,但是目标文件可以是基于某种条件的大约20个文件中的任何一个。这无疑是错误的,但我想要的是: - name: update files hosts: localhost user: myself tasks: - blockinfile: dest: /home/me/file1 when: {{ condition }} == True dest: /home/me/file2
- name: update files
hosts: localhost
user: myself
tasks:
- blockinfile:
dest: /home/me/file1
when: {{ condition }} == True
dest: /home/me/file2
when: {{ condition2 }} == True
...
block: |
data
data
...
有没有一种惯用的或正确的方法来解决这个问题?这听起来有点复杂,我有一种感觉,如果你从这个问题上退一步,你可能会想到一个更好的解决方案。但是,根据提供的信息,我会说将逻辑移到变量中,然后使用该变量作为目标 因此,您会有一些vars文件(例如角色默认值或环境组vars,甚至包括一个vars文件),如下所示:
rolename_blockinfile_dest: '{% if condition %}/home/me/file1{% elif condition2 %}/home/me/file2{% endif %}'
然后在您的任务中执行以下操作:
...
tasks:
- blockinfile:
dest: '{{ rolename_blockinfile_dest }}'
...
是的,这是一个聪明的方法。我刚刚为它创建了一个环境变量,并在构建过程中将其注入。谢谢你的提示!