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

时间:2018-10-19 10:21:54

标签: 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

得到以下错误:

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

3 个答案:

答案 0 :(得分:0)

become_user: test1替换为become_user: root(或删除此行,因为默认情况下become_userroot

有关详细信息,请阅读Understanding privilege escalation

答案 1 :(得分:0)

ansible_user=Your-User文件中添加ansible_become=true/etc/ansible/hosts以消除此错误:

  

您必须是root用户才能执行此命令

答案 2 :(得分:0)

相同的错误,您应该包含“成为”和“成为_用户”。在某些情况下,添加成为方法。 More

- hosts: somehost
  name: Install something 
  become: yes
  remote_user: yourname
  

成为

     

设置为yes可激活特权升级。

     

成为用户_p

     

设置为具有所需特权的用户-您成为的用户,而不是您登录时使用的用户。不表示成为:是,允许对其进行设置   在主机级别。默认值为root。