【问题标题】:ERROR! 'sudo' is not a valid attribute for a Play错误! 'sudo' 不是 Play 的有效属性
【发布时间】:2020-02-11 06:03:56
【问题描述】:

我有一个 ansible 播放文件,它必须执行两个任务,首先在本地机器上获取磁盘使用情况,另一个任务是获取远程机器的磁盘使用情况并在远程机器上安装 apache2。

当我尝试运行文件时,我收到错误 “错误!'sudo' 不是 Play 的有效属性” 当我从 yml 文件中删除 sudo 和 apt 部分时,它运行良好。

我正在使用 ansible 2.9.4。以下是两个剧本文件:

文件运行没有任何错误,

--- 
- 
  connection: local
  hosts: localhost
  name: play1
  tasks: 
    - 
      command: "df -h"
      name: "Find the disk space available"
    - 
      command: "ls -lrt"
      name: "List all the files"
    - 
      name: "List All the Files"
      register: output
      shell: "ls -lrt"
    - 
      debug: var=output.stdout_lines
- 
  hosts: RemoteMachine1
  name: play2
  tasks: 
    - name: "Find the disk space"
      command: "df -h"
      register: result
    - debug: var=result.stdout_lines

文件运行出错:

--- 
- 
  connection: local
  hosts: localhost
  name: play1
  tasks: 
    - 
      command: "df -h"
      name: "Find the disk space available"
    - 
      command: "ls -lrt"
      name: "List all the files"
    - 
      name: "List All the Files"
      register: output
      shell: "ls -lrt"
    - 
      debug: var=output.stdout_lines
- 
  hosts: RemoteMachine1
  name: play2
  sudo: yes
  tasks: 
    - name: "Find the disk space"
      command: "df -h"
      register: result
    - name: "Install Apache in the remote machine" 
      apt: name=apache2 state=latest
    - debug: var=result.stdout_lines

完整的错误信息:

ERROR! 'sudo' is not a valid attribute for a Play

The error appears to be in '/home/Documents/ansible/play.yml': line 20, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

-
  hosts: RemoteMachine1
  ^ here

【问题讨论】:

    标签: ansible


    【解决方案1】:

    Ansible play 关键字 sudo 是(很久以前)deprecated with warnings in version 2.0 and removed in version 2.2

    请参阅actual supported play keywords。使用:

    become: true
    

    【讨论】:

      【解决方案2】:
      -  hosts: RemoteMachine1
         name: play2
         become: yes
         tasks: 
            - name: "Find the disk space"
              command: "df -h"
              register: result
            - name: "Install Apache in the remote machine" 
              apt: name=apache2 state=latest
            - debug: var=result.stdout_lines
      

      use become: 是的,它将以 root 用户身份运行您的任务。

      Become directives

      【讨论】:

        猜你喜欢
        • 2017-01-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-12
        相关资源
        最近更新 更多