ansible-playbook 出现错误“您需要 root 才能执行此命令”

San*_*thi 5 ansible

我正在剧本下面运行。它将使用 ec2-user 登录到服务器,但将安装 mysql-java-connector,我的 test1 用户。

---
- hosts: cluster
  become: yes
  remote_user: ec2-user
  tasks:
  - name: Create test1 User
      user:
        name: test1
        password: '$6$jQX0JQzf8GB$NI/Pv1rMLyxWYaFCGNsbrun3sfn5bXSzg89Ip.ga2yf3n7hhrjiPsEo5IChIA7X8xVxnuZzm2sWA7IRM6qZOR0'
        state: present
        shell: /bin/bash       # Defaults to /bin/bash
        system: no             # Defaults to no
        createhome: yes        # Defaults to yes
        home: /home/test1
  - name: Add users to sudoers
      lineinfile:
        dest : /etc/sudoers
        state: present
        line: 'test1  ALL=(ALL)  NOPASSWD: ALL'
  - name: Install mysql java connector
      become_user: test1
      become_method: sudo
      yum: name=mysql-connector-java state=present
Run Code Online (Sandbox Code Playgroud)

出现以下错误:

fatal: [xxx.xxx.xxx.211]: FAILED! => {"changed": false, "msg": "You need to be root to perform this command.\n", "rc": 1, "results": [""]}
Run Code Online (Sandbox Code Playgroud)

Ale*_*nov -2

替换become_user: test1become_user: root(或删除此行,因为默认情况become_userroot)。

请阅读了解权限升级以获取更多信息。