0
votes

I'm a beginner with Ansible, and I need to run some basic tasks on a remote server.

The procedure is as follows:

  • I log as some user (osadmin)
  • I run su - to become root
  • I then do the tasks I need to.

So, I wrote my playbook as follows:

---
- hosts: qualif
  vars:
    - ansible_user: osadmin
    - ansible_password: H1g2.D6#
  tasks:
    - name: Copy stuff from here to over there
      copy:
       src: /home/osadmin/file.txt
       dest: /home/osadmin/file-changed.txt
       owner: osadmin
       group: osadmin
       mode: 0777

Also, I have the following in vars/main.yml:

ansible_user: osadmin
ansible_password: password1
ansible_become_password: password2
[ some other values ]

However, when running my tasks, Ansible / the hosts returns me the following:

"Incorrect sudo password"

I then changed my tasks so that instead of becoming sudo and copy the file in some place my osadmin doesn't have access, I just copy the file on /home/osadmin. So, theorically, no need to become sudo for just a simple copy. The problem now is that not only it keeps saying "wrong sudo password", but if I remove it, Ansible asks for it.

I then decided to run the command and added -vvv at the end, and it showed me the following:

ESTABLISH SSH CONNECTION FOR USER: osadmin

SSH: EXEC sshpass -d10 ssh -C -o ControlMaster=auto -o ControlPersist=60s -o User=osadmin -o ConnectTimeout=10 -o ControlPath=/home/osadmin/.ansible/cp/b9489e2193 -tt HOST-ADDRESS '/bin/sh -c '"'"'sudo -H -S -n -u

root /bin/sh -c '"'"'"'"'"'"'"'"'echo BECOME-SUCCESS-ewujwywrqhcqfdrkaglvrouhmuiefwlj; /usr/bin/python /home/osadmin/.ansible/tmp/ansible-tmp-1550076004.1888492-11284794413477/AnsiballZ_setup.py'"'"'"'"'"'"'"'"' && sleep 0'"'"''

(1, b'sudo: a password is required\r\n', b'Shared connection to HOST-ADDRESS closed.\r\n')

As you can see, it somehow uses root, while I never told him to.

Does anyone know why Ansible keeps trying to be sudo, and how can I disable this?

Thank you in advance

1
Have you tried become: false? - Matt Schuchard
Check group_vars and host_vars in inventory and playbook directory - Konstantin Suvorov

1 Answers

1
votes

There is a difference between 'su' and 'sudo'. If you have 'su' access, that means, that you can log as root (may be not, but it looks like). Use ansible_ssh_user=root, ansible_password=password2.

If this doesn't work, try to configure sudo on a server. You should be able to run sudo whoami and to get answer root. After that your code should run.

One more thing: you are using 'copy' module incorrectly. It uses src as path on local machine (where ansible is run), and dst as path on remote machine.