特定键的可查询

问题描述 投票:0回答:1

我必须检查目标虚拟机的/ etc / hosts文件。如果该文件中有任何以10。。*开头的ip。它应报告是并显示ip,如果没有ip,则应在该目标主机名下报告No。所有这些信息都应该在天青管道中构建工件。.请向我建议可能性

azure ansible
1个回答
0
投票
使用file查找实际上是IMO的一个很好的开始。我在下面遵循的想法是:

    在换行符上分割文件的内容以获得行列表。
  • 在这些行上循环,然后将匹配的ip添加到列表中。使用正则表达式搜索ip。
  • 如果结果列表不为空,则显示结果列表的内容。
  • 示例剧本:

    - hosts: localhost gather_facts: false tasks: - name: Read /etc/hosts file in a list line by line set_fact: my_host_entries: "{{ lookup('file', '/etc/hosts').split('\n') }}" - name: Add matching ips to a list vars: ip_regex: "^10(\\.\\d+){3}" set_fact: matching_ips: "{{ matching_ips | default([]) + [item | regex_search(ip_regex)] }}" when: item is match(ip_regex) loop: "{{ my_host_entries }}" - name: Show list of matching ips debug: var: matching_ips when: matching_ips | default([]) | length > 0

    您可以适应您的确切需求。    
  • © www.soinside.com 2019 - 2024. All rights reserved.