Getting Error "You need to be root to perform this command" ansible-playbook

13,589

Same error, you should include become and become_user. In some cases add become method. More

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

become

set to yes to activate privilege escalation.

become_user

set to user with desired privileges — the user you become, NOT the user you login as. Does NOT imply become: yes, to allow it to be set at host level. Default value is root.

Share:
13,589

Related videos on Youtube

Sanjay Sethi
Author by

Sanjay Sethi

Updated on June 04, 2022

Comments

  • Sanjay Sethi
    Sanjay Sethi over 1 year

    I am running below playbook. which will login to the server using ec2-user but mysql-java-connector will be installed, my test1 user.

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

    Gets below error:

    fatal: [xxx.xxx.xxx.211]: FAILED! => {"changed": false, "msg": "You need to be root to perform this command.\n", "rc": 1, "results": [""]}
    
  • Sanjay Sethi
    Sanjay Sethi about 5 years
    i want to install with test1 user which is having sudo permission. If i use become_user: root mean i am installing it through root user. same way like most user install and run apache with apache user instead of root.
  • Alexey Vazhnov
    Alexey Vazhnov about 5 years
    If only test user have full sudo permission, then use remote_user: test instead of remote_user: ec2-user. Anyway, yum works from root user only. To run your service from non-root, you should reconfigure your service after installation. But there are no service start in your example, only installation. Installation with yum works only from root user.
  • Abdelghani Roussi
    Abdelghani Roussi over 3 years
    for more informations, please visit : Ansible docs : become