为什么Ansible显示"ERROR!在任务中未检测到任何操作"错误?

tec*_*raf 44 ansible

Ansible显示错误:

错误!任务中未检测到任何操作.这通常表示拼写错误的模块名称或模块路径不正确.

怎么了?


确切的记录是:

ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path.

The error appears to have been in 'playbook.yml': line 10, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

---
- name: My task name
  ^ here
Run Code Online (Sandbox Code Playgroud)

tec*_*raf 77

原因#1

您正在使用较旧版本的Ansible,它没有您尝试运行的模块.

怎么检查?

  1. 打开模块模块文档列表,找到模块的文档页面.

  2. 阅读页面顶部的标题 - 它通常显示引入模块的Ansible版本.例如:

    2.2版中的新功能.

  3. 确保您运行的是指定版本的Ansible或更高版本.跑:

    ansible-playbook --version
    
    Run Code Online (Sandbox Code Playgroud)

    并检查输出.它应该显示如下:

    ansible-playbook 2.4.1.0


原因#2

你试着写一个角色并把一本剧本放进去my_role/tasks/main.yml.

tasks/main.yml文件应仅包含任务列表.如果你指定:

---
- name: Configure servers
  hosts: my_hosts
  tasks:
    - name: My first task
      my_module:
        parameter1: value1
Run Code Online (Sandbox Code Playgroud)

Ansible尝试查找名为hosts的动作模块和名为的动作模块tasks.它没有,所以它抛出一个错误.

解决方案:仅指定tasks/main.yml文件中的任务列表:

---
- name: My first task
  my_module:
    parameter1: value1
Run Code Online (Sandbox Code Playgroud)

理由#3

操作模块名称拼写错误.

这很明显,但被忽视了.如果使用不正确的模块名称,例如users代替user,Ansible会报告"没有在任务检测的动作".

Ansible被设计为一个高度可扩展的系统.它没有可以运行的有限模块集,也无法"提前"检查每个操作模块的拼写.

实际上你可以编写然后指定自己的模块命名,qLQn1BHxzirz而Ansible必须尊重它.由于它是一种解释型语言,它只在尝试执行任务时"发现"错误.


原因#4

您正在尝试执行未随Ansible分发的模块.

操作模块名称是正确的,但它不是与Ansible一起分发的标准模块.

如果您使用的是由第三方提供的模块 - 软件/硬件供应商或公开共享的其他模块,则必须先下载该模块并将其放在适当的目录中.

您可以将其放在modules剧本的子目录中或公共路径中.

Ansible看起来ANSIBLE_LIBRARY--module-path命令行参数.

要检查哪些路径有效,请运行:

ansible-playbook --version
Run Code Online (Sandbox Code Playgroud)

并检查以下值:

配置模块搜索路径=

Ansible版本2.4及更高版本应提供路径列表.


原因#5

你真的在任务中没有任何动作.

任务必须定义一些操作模块.以下示例无效:

- name: My task
  become: true
Run Code Online (Sandbox Code Playgroud)

  • 我想知道为什么有一个任务没有行动对于 ansible 来说是一个问题。我可能想创建一个当变量具有特定值时失败的任务。 (2认同)

nel*_*aro 7

我真的无法改进@techraf 的回答/sf/answers/3301144031/。我想添加原因 #6 我的特例

原因#6

错误地使用roles:导入/包含角色作为子任务。

这是行不通的,您不能以这种方式将角色包含为剧中的子任务。

---
- hosts: somehosts
  tasks:

  - name: include somerole
    roles:
      - somerole
Run Code Online (Sandbox Code Playgroud)

使用 include_role

根据文档

您现在可以使用 import_role 或 include_role 将角色与任何其他任务内联使用:

- hosts: webservers
  tasks:
  - debug:
      msg: "before we run our role"
  - import_role:
      name: example
  - include_role:
      name: example
  - debug:
      msg: "after we ran our role"
Run Code Online (Sandbox Code Playgroud)

将角色放置在与主机内联的正确位置

包括顶部的角色

---
- hosts: somehosts
  roles:
    - somerole
  tasks:   
    - name: some static task
      import_role:
        name: somerole
      hosts: some host
    - include_role:
        name: example
Run Code Online (Sandbox Code Playgroud)

您需要了解导入/包含静态/动态之间的区别


zay*_*uan 7

当我将debug任务引用为ansible.builtin.debug

导致 CI​​ 中语法错误(但在本地有效):

- name: "Echo the jenkins job template"
  ansible.builtin.debug:
    var: template_xml
    verbosity: 1
Run Code Online (Sandbox Code Playgroud)

在本地和 CI 中工作:

- name: "Echo the jenkins job template"
  debug:
    var: template_xml
    verbosity: 1
Run Code Online (Sandbox Code Playgroud)

我相信 - 但尚未证实 - 本地与 CI 的差异在于 ansible 版本。

  • 当地:2.10
  • 置信度:2.7