不安全变量的可靠最佳实践



有比这更好的解决方案吗?

- hosts: localhost
gather_facts: true
tasks:
- name: strip unsafe characters from ansible_virtualization_role value
shell: |
echo {{ ansible_virtualization_role }} | sed 's/[^a-z]//g'
register: buf 
- name: Set my_virtualization_role
set_fact:
my_virtualization_role={{ buf.stdout }}

ansible lint reports"-no changed when#Commands should not change things if nothing needed doing";因此,在不使用shell模块的情况下清除不安全变量是很好的,但我尝试的每个纯Ansible解决方案都会给出不安全变量错误

Charles

您应该能够使用ansible过滤器来实现您想要做的事情-特别是,看看regex_replace过滤器-请参阅https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#searching-带有正则表达式的字符串

在您的示例中,您可以编写

- hosts: localhost
gather_facts: true
tasks:
- name: Set my_virtualization_role
set_fact:
my_virtualization_role={{ ansible_virtualization_role | regex_replace('[^a-z]', '') }}

最新更新