3

I am using Ansible-2.0 on linux machine and I wish to do change DNS on remote machine using ansible as below:

---
# tasks file for test
- name: Change dns
   become: yes
   become_user: admin
   replace:
     dest: /etc/resolv.conf
     regexp: '192.168.1.24'
     replace: '8.8.8.8'

Output:

$ ansible-playbook -i "mn," test.yml 
TASK [test : Change dns]     *******************************************************
fatal: [mn]: FAILED! => {"changed": false, "failed": true, "msg": "The destination directory (/etc) is not writable by the current user."}

PLAY RECAP     *********************************************************************
mn                         : ok=1    changed=0    unreachable=0    failed=1   

On remote machine, admin is as sudo user and here I don't want to change sudo setting on remote machine, Is there any method to pass password using task(script), not using command line.

4

you should configure admin as a remote_user, not become_user. The become_user option sets to which user you would su to execute certain task. In other words, your playbook should look like:

---
- hosts: somehosts
  remote_user: admin
  roles:
    ....

#tasks file
---
# tasks file for test
- name: Change dns
  become: yes
   replace:
     dest: /etc/resolv.conf
     regexp: '192.168.1.24'
     replace: '8.8.8.8'

That way the admin user will be used to establish your ssh session, but for the Change dns task will be used sudo.

Also, if admin requires password for sudo, you will have to run your playbook like this:

ansible-playbook -i mn test.yml -K

The -K switch will prompt you for the sudo password for admin.

  • I've remove the become_user and added remote_user but getting this error fatal: [mn]: FAILED! => {"changed": false, "failed": true, "module_stderr": "", "module_stdout": "sudo: a password is required\r\n", "msg": "MODULE FAILURE", "parsed": false} – Nullpointer Feb 28 '17 at 7:28
  • I've edited my answer, please try again. – 13dimitar Feb 28 '17 at 7:31
  • using -b, not asking remote user password and getting same error, Can we pass the plain password on script – Nullpointer Feb 28 '17 at 8:33
  • No, we should not pass the plain password on script, it is an awful practice and it should not be done. How do you connect to the remote machine - is it with a password, or with ssh key? – 13dimitar Feb 28 '17 at 8:34
  • accessing remote machine using ssh-key – Nullpointer Feb 28 '17 at 8:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.