community.general.nmap能否在Playbook中运行?求使用示例
问题解答
核心结论
community.general.nmap 是Inventory插件,不是能在Playbook任务里直接调用的模块,所以你直接在任务里写 community.general.nmap: 才会提示模块不存在。文档里说的“在Playbook中使用时指定:community.general.nmap”,指的是把它作为Inventory源来用,不是当任务模块调用。
正确使用方式
1. 作为Inventory插件使用(对应文档描述的场景)
你已经能正常用它作为常规Inventory插件,这里补充Playbook中指定该Inventory源的示例:
先创建一个Inventory配置文件(比如命名为nmap_hosts.yml):
plugin: community.general.nmap address: 192.168.0.0/24
然后在Playbook中指定这个Inventory源:
- name: 用nmap Inventory插件获取主机清单 hosts: all gather_facts: false tasks: - name: 打印扫描到的主机 debug: msg: "扫描到主机: {{ inventory_hostname }}"
运行Playbook时指定该Inventory文件即可:
ansible-playbook -i nmap_hosts.yml your_playbook.yml
2. 在Playbook任务中执行nmap扫描
如果需要在任务里主动执行nmap扫描,应该使用**community.general.nmap_scan模块**——这是专门用于任务场景的扫描模块,示例如下:
- name: 执行nmap扫描任务 hosts: localhost gather_facts: false tasks: - name: 扫描192.168.0.0/24网段 community.general.nmap_scan: address: 192.168.0.0/24 arguments: "-sP" # 可指定nmap扫描参数,比如-sP为Ping扫描 register: scan_result - name: 输出扫描结果 debug: var: scan_result
关键区别澄清
- Inventory插件:用于动态生成Ansible的主机清单,在Playbook执行前加载,提供要管理的主机列表。
- 任务模块:用于在Playbook任务中执行具体操作(比如扫描、安装软件等),是任务执行时调用的功能单元。
你的环境中community.general.nmap是Inventory插件,所以不能在任务里直接调用,若要执行扫描任务需使用community.general.nmap_scan模块。
内容的提问来源于stack exchange,提问作者notsodev
相关产品推荐
相关产品推荐

