Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ansible

I have an ansible play file that has to performs two tasks, first on the local machine get the disk usage and another task is to get the disk usage of a remote machine and install apache2 in the remote machine.

When I am trying to run the file I am getting the error "ERROR! 'sudo' is not a valid attribute for a Play" When I remove the sudo and apt section from the yml file, it is running fine.

I am using ansible 2.9.4. Below are two playbook files:

File running without any error,

--- 
- 
  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

File running with error:

--- 
- 
  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

Complete error message:

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
like image 460
Jyoti Prakash Mallick Avatar asked Feb 11 '20 06:02

Jyoti Prakash Mallick


2 Answers

Ansible play keyword sudo was (long ago) deprecated with warnings in version 2.0 and removed in version 2.2

See the actual supported play keywords. Use:

become: true
like image 148
Zeitounator Avatar answered Sep 30 '22 00:09

Zeitounator


-  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: yes it will run your tasks as the root user.

Become directives

like image 24
shreyash Avatar answered Sep 30 '22 00:09

shreyash