如何在Ansible中实现用户指定Profile的精确匹配校验?
解决Ansible Profile精确匹配失败任务的问题
要实现仅当用户输入的Profile名称与列表中某个条目完全匹配时才触发任务失败,核心是先从命令输出中提取出精确的Profile名称列表,再进行精确匹配判断,避免原字符串包含匹配导致的误触发。
修改后的Playbook示例
--- - hosts: localhost gather_facts: no become: true tasks: - name: Getting the list of Profiles shell: some command to get the list of profiles register: output - name: Printing the list of profiles debug: msg: "{{ output.stdout_lines }}" - name: Extract exact profile names from output set_fact: existing_profiles: "{{ output.stdout_lines | select('match', '.*Profile=.*') | map('regex_replace', '.*Profile=(.*)$', '\\1') | list }}" - name: Failing the task when the specified profile already exists fail: msg: The Profile {{ userinput }} already exists when: userinput in
相关产品推荐
相关产品推荐

