【问题标题】:ERROR! no action detected in task. Ansible错误!任务中未检测到任何操作。 Ansible
【发布时间】:2016-04-12 08:41:32
【问题描述】:

我最近开始使用 Ansible,我有一个 playbook 文件,内容如下:

...# Code to start an EC2 instance in the same playbook
...# Then trying to install Nginx on the same server:

  - hosts: webserver
    become: yes
    remote_user: ubuntu
    tasks:
      - name: Install Nginx
        apt: pkg=nginx state=installed update_cache=true
        notify:
          - start nginx

使用

运行时出现以下错误
ansible-playbook -i hosts ec2_launch.yml

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

The offending line appears to be:

  - hosts: webserver
    ^ here

我一定是遗漏了一些东西,不知道要更新什么。也许我无法在同一个剧本中启动实例并安装 Nginx?

提前致谢。

【问题讨论】:

  • 是的,你应该可以。错误可能在前面的部分中,即实际创建实例的部分
  • 好吧,是的,我意识到我已将这些任务置于 EC2 配置任务之下,因此我移动了它们,并且不再出现此错误。现在,问题是它仍然没有像PLAY [Install Nginx on this new instance] ************************************** skipping: no hosts matched 所说的那样安装 Nginx。虽然我有一个将新创建的 EC2 实例添加到主机文件的任务。
  • 我认为您应该添加 ec2 配置任务以使问题更清晰。此外,ec2 模块Ansible Docs 中有很多关于如何在新创建的实例上运行任务的示例。
  • 感谢您指向文档,我需要修改一些内容:- name: Add new instance to host group add_host: hostname={{ item.public_ip }} groupname=launched with_items: ec2.instances

标签: nginx ansible ansible-playbook


【解决方案1】:

更新此内容以显示此问题是如何解决的:

---
  - name: Provision an EC2 Instance
    hosts: local
    connection: local
    gather_facts: True
    tags: provisioning
    # Necessary Variables for creating/provisioning the EC2 Instance
    vars:
      instance_type: t2.micro
      security_group: Webserver  # Change the security group name here
      image: amiID # Change the AMI, from which you want to launch the server
      region: eu-central-1 # Change the Region
      keypair: keypair # Change the keypair name
      count: 1

    # Task that will be used to Launch/Create an EC2 Instance
    tasks:
      - name: Launch the new EC2 Instance
        local_action: ec2 
                      group={{ security_group }} 
                      instance_type={{ instance_type}} 
                      image={{ image }} 
                      wait=true 
                      region={{ region }} 
                      keypair={{ keypair }}
                      count={{count}}
        register: ec2

      - name: Add new instance to host group
        add_host: hostname={{ item.public_ip }} groupname=launched
        with_items: ec2.instances

      - name: Wait for SSH to come up
        local_action: wait_for 
                      host={{ item.public_ip }} 
                      port=22 
                      state=started
        with_items: ec2.instances

      - name: Add tag to Instance(s)
        local_action: ec2_tag resource={{ item.id }} region={{ region }} state=present
        with_items: ec2.instances
        args:
          tags:
            Name: webserver

  - name: Install Nginx on this new instance
    hosts: launched
    become: yes
    remote_user: ubuntu
    tasks:
      - name: Install Nginx
        apt: pkg=nginx state=installed update_cache=true
        notify:
          - start nginx

使用最新的 Ansible 更新,这会触发一些警告,并且需要更新剧本,但这是我用来解决上述问题的当前实现,基于 cmets。

【讨论】:

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