【问题标题】:Why does Ansible show "ERROR! no action detected in task" error?为什么 Ansible 显示“错误!在任务中未检测到任何操作”错误?
【发布时间】:2018-04-19 22:18:12
【问题描述】:

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

【问题讨论】:

    标签: ansible


    【解决方案1】:

    原因 #1

    您使用的是旧版本的 Ansible,它没有您尝试运行的模块。

    如何检查?

    1. 打开模块列表module documentation 并找到您的模块的文档页面。

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

      2.2 版中的新功能。

    3. 确保您正在运行指定版本的 Ansible 或更高版本。运行:

      ansible-playbook --version
      

      然后检查输出。它应该显示如下内容:

      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
    

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

    解决方案:在tasks/main.yml 文件中只指定一个任务列表:

    ---
    - name: My first task
      my_module:
        parameter1: value1
    

    原因 #3

    动作模块名称拼写错误。

    这很明显,但被忽视了。如果您使用不正确的模块名称,例如 users 而不是 user,Ansible 将报告“在任务中未检测到任何操作”。

    Ansible 被设计成一个高度可扩展的系统。它没有您可以运行的有限模块集,也无法“提前”检查每个动作模块的拼写。

    事实上,您可以编写并指定您自己的名为qLQn1BHxzirz 的模块,Ansible 必须尊重这一点。由于它是一种解释型语言,它仅在尝试执行任务时“发现”错误。


    原因 #4

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

    动作模块名称正确,但它不是随 Ansible 分发的标准模块。

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

    您可以将其放置在 playbook 的 modules 子目录或公共路径中。

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

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

    ansible-playbook --version
    

    并检查以下值:

    配置的模块搜索路径 =

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


    原因 #5

    您在任务中确实没有任何操作。

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

    - name: My task
      become: true
    

    【讨论】:

    • 我想知道为什么有一个没有行动的任务对 ansible 来说是个问题。我可能想创建一个在变量具有特定值时失败的任务。
    • 它失败了,因为 ansible 是垃圾,是其他垃圾中最好的部署,但从 SD 的角度来看,这种神奇的行为简直太疯狂了
    • 最复杂的答案!!
    【解决方案2】:

    @techraf 的回答 https://stackoverflow.com/a/47159200/619760 我无法真正改进。 我想添加原因 #6 我的特殊情况

    原因 #6

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

    这不起作用,您不能以这种方式将角色作为子任务包含在戏剧中。

    ---
    - hosts: somehosts
      tasks:
    
      - name: include somerole
        roles:
          - somerole
    

    使用 include_role

    根据documentation

    您现在可以使用 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"
    

    将角色放置在与主机一致的正确位置

    在顶部包含角色

    ---
    - hosts: somehosts
      roles:
        - somerole
      tasks:   
        - name: some static task
          import_role:
            name: somerole
          hosts: some host
        - include_role:
            name: example
    

    你需要了解import/include static/dynamic的区别

    【讨论】:

      【解决方案3】:

      对我来说,“systemd”模块出现了问题。原来我的ansible --version2.0.0.2 并且模块是在2.2 版本中首次引入的。将我的 ansible 更新到最新版本解决了这个问题。

      playbook.yaml

      - name: "Enable and start docker service and ensure it's not masked"
        systemd:
          name: docker
          state: started
          enabled: yes
          masked: no
      

      错误

      ERROR! no action detected in task
      etc..   
      etc..
      etc..
      
      - name: "Enable and start docker service and ensure it's not masked"
        ^ here
      

      【讨论】:

        【解决方案4】:

        错误说明:

        没有要执行的任务意味着它无法执行您的剧本中描述的操作

        根本原因:

        • 安装的 Ansible 版本不支持

        如何检查:

        • ansible --version

        解决方案:

        • 将 Ansible 升级到支持您尝试使用的功能的版本

        如何升级 Ansible:

        https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html#selecting-an-ansible-version-to-install

        Ubuntu 快速说明:

        sudo apt update
        sudo apt install software-properties-common
        sudo apt-add-repository --yes --update ppa:ansible/ansible
        sudo apt install ansible
        

        P.S:遵循这条路径并从版本 2.0.2 升级到 2.9 升级后,同样的剧本就像一个魅力

        【讨论】:

          【解决方案5】:

          当我将debug 任务引用为ansible.builtin.debug 时出现此错误

          在 CI 中导致语法错误(但在本地工作):

          - name: "Echo the jenkins job template"
            ansible.builtin.debug:
              var: template_xml
              verbosity: 1
          

          在本地和 CI 中工作:

          - name: "Echo the jenkins job template"
            debug:
              var: template_xml
              verbosity: 1
          

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

          • 本地:2.10
          • CI:2.7

          【讨论】:

            【解决方案6】:

            在我的情况下,这是修复:

            ansible-galaxy collection install ansible.posix
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2016-06-05
              • 2017-05-06
              • 2020-05-03
              • 1970-01-01
              • 2017-07-10
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多