如果文件有模式,如何跳过Ansible中的模板副本



如果目标文件中没有字符串,则尝试仅复制Nginx配置文件。

我以为这会奏效:

- name: Copy nginx config file
template:
src: templates/nginx.conf
dest: /etc/nginx/sites-enabled/default
validate: grep -l 'managed by Certbot' %s

但是如果";由Certbot管理";不在文件中,并停止运行剧本。

如果目标文件已经具有该模式,我如何跳过模板副本?也许有更好的方法可以得到同样的结果?

灵感来自其他答案

您可以在检查模式下使用lineinfile模块检查文件中是否存在内容。然后,您可以将结果用作模板任务的条件。条件中的default是为了应对文件不存在并且found属性不在注册结果中的情况。

---
- name: Check for presence of "managed by Certbot" in file
lineinfile:
path: /etc/nginx/sites-enabled/default
regexp: ".*# managed by Certbot.*"
state: absent
check_mode: yes
changed_when: false
register: certbot_managed
- name: Copy nginx config file when not certbot managed
template:
src: templates/nginx.conf
dest: /etc/nginx/sites-enabled/default
when: certbot_managed.found | default(0) == 0

您可以使用failed_when条件,并将其建立在validate生成的失败消息(failed to validate(的基础上:

- name: Copy nginx config file
template:
src: templates/nginx.conf
dest: /etc/nginx/sites-enabled/default
validate: grep -l 'managed by Certbot' %s
failed_when: 
- copy_config_file.failed
- copy_config_file.msg != 'failed to validate'
register: copy_config_file

注意:在when*_when中,有一个条件列表就像执行list.0 and list.1 and ...


给定剧本:

- hosts: all
gather_facts: no
tasks:
- copy:
dest: templates/nginx.conf
content: "{{ content | default('some random content') }}"
- copy:
dest: /etc/nginx/sites-enabled/default
content: "blank"
- template:
src: templates/nginx.conf
dest: /etc/nginx/sites-enabled/default
validate: grep -l 'managed by Certbot' %s
failed_when: 
- copy_config_file.failed
- copy_config_file.msg != 'failed to validate'
register: copy_config_file
- shell: cat templates/nginx.conf
register: template_content
failed_when: false
- shell: cat /etc/nginx/sites-enabled/default
register: file_content
failed_when: false
- debug:
var: template_content.stdout

- debug:
var: file_content.stdout
  1. 通过以下方式运行时:
    ansible-playbook play.yml
    
    它提供:
    PLAY [all] *******************************************************************************************************
    TASK [copy] ******************************************************************************************************
    changed: [localhost]
    TASK [copy] ******************************************************************************************************
    changed: [localhost]
    TASK [template] **************************************************************************************************
    ok: [localhost]
    TASK [shell] *****************************************************************************************************
    changed: [localhost]
    TASK [shell] *****************************************************************************************************
    changed: [localhost]
    TASK [debug] *****************************************************************************************************
    ok: [localhost] => {
    "template_content.stdout": "some random content"
    }
    TASK [debug] *****************************************************************************************************
    ok: [localhost] => {
    "file_content.stdout": "blank"
    }
    PLAY RECAP *******************************************************************************************************
    localhost                  : ok=7    changed=4    unreachable=0    failed=0     skipped=0    rescued=0    ignored=0   
    
  2. 现在,当与一起跑步时
    ansible-playbook play.yml -e "content='managed by Certbotnsome other content'"
    
    通过一个额外的参数来修改模板的内容,它给出:
    PLAY [all] *******************************************************************************************************
    TASK [copy] ******************************************************************************************************
    ok: [localhost]
    TASK [copy] ******************************************************************************************************
    changed: [localhost]
    TASK [template] **************************************************************************************************
    changed: [localhost]
    TASK [shell] *****************************************************************************************************
    changed: [localhost]
    TASK [shell] *****************************************************************************************************
    changed: [localhost]
    TASK [debug] *****************************************************************************************************
    ok: [localhost] => {
    "template_content.stdout": "managed by Certbotnsome other content"
    }
    TASK [debug] *****************************************************************************************************
    ok: [localhost] => {
    "file_content.stdout": "managed by Certbotnsome other content"
    }
    PLAY RECAP *******************************************************************************************************
    localhost                  : ok=7    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    

最新更新