You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行Ansible Playbook报错“No argument passed to command module”求助

Ansible Playbook执行错误排查与解决

环境文件信息

1. Inventory清单文件(nodes)

[servers]
server1
server2
server3

2. SSH配置($HOME/.ssh/config)

原文件存在语法错误,Host字段后多了冒号:

Host: server1  # 错误:Host后不能加冒号
  HostName: 1.1.1.1
  Port: 1111
  User: root
Host: server2
  HostName: 2.2.2.2
  Port: 2222
  User: root
Host: server3
  HostName: 3.3.3.3
  Port: 3333
  User: root

3. Playbook文件(playbook.yaml)

原文件存在缩进和变量语法问题:

- hosts: servers
  tasks:
    - name: show hostnames only
      shell:
        cmd: hostname

    - name: Copy csf files
    copy:  # 错误:缩进未与name对齐
        src: {{ item.src }}  # 错误:Jinja2变量未加双引号
        dest: {{ item.dest }}
        mode: preserve
    with_items:
        - { src: /etc/csf/csf.conf, dest: /etc/csf/csf.conf }
        - { src: /etc/csf/csf.allow, dest: /etc/csf/csf.allow }

执行错误信息

执行ansible playbook.yaml后出现:

[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
[WARNING]: Could not match supplied host pattern, ignoring: playbook.yaml
### some help commands of ansible ###
Some actions do not make sense in Ad-Hoc (include, meta, etc)
ERROR! No argument passed to command module

问题分析与修复

1. 核心执行命令错误

ansible是用于Ad-Hoc临时命令的工具,执行Playbook必须用ansible-playbook命令。原命令把Playbook文件名当成了主机组名,导致Ansible无法识别目标主机。

2. Inventory文件未指定

Ansible默认不会自动加载名为nodes的清单文件,需要通过-i参数明确指定,或者将清单重命名为hosts放在当前目录,也可以在ansible.cfg中配置默认路径。

3. SSH配置语法修正

去掉Host字段后的冒号,正确格式:

Host server1
  HostName 1.1.1.1
  Port 1111
  User root
Host server2
  HostName 2.2.2.2
  Port 2222
  User root
Host server3
  HostName 3.3.3.3
  Port 3333
  User root

4. Playbook语法修正

  • 修正copy模块的缩进,与同任务的name对齐;
  • Jinja2变量用双引号包裹,避免YAML解析歧义;
    修正后的Playbook:
- hosts: servers
  tasks:
    - name: show hostnames only
      shell:
        cmd: hostname

    - name: Copy csf files
      copy:
          src: "{{ item.src }}"
          dest: "{{ item.dest }}"
          mode: preserve
      with_items:
          - { src: /etc/csf/csf.conf, dest: /etc/csf/csf.conf }
          - { src: /etc/csf/csf.allow, dest: /etc/csf/csf.allow }

正确执行命令

ansible-playbook -i nodes playbook.yaml

内容的提问来源于stack exchange,提问作者Saeed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 01:30:25