1

I want to move my workspace3 folder to /usr/share directory using below Ansible scripts. But I am unable to move it.

 - name: Move workspace3 directory to /usr/share/ Folder
   command: mv /tmp/workspace3/ /usr/share

I also tried with shell module

   shell: mv /tmp/workspace3 /usr/share

I believe it is a permission issue, how can I define permission through ansible ?

Tanay Suthar
  • 453
  • 3
  • 8
  • 19

2 Answers2

6

/usr/share is a directory owned by the root user. In order to create folders (or move folders) into this directory, you must use priviledged escalation. This is very simple with ansible, simply use the following in your playbook;

become: yes

Make sure that when you run your playbook you use the -K flag which will ask you for the sudo password, assuming you do not have NOPASSWD configured for that user in your sudoers file. The default user that "become" uses is root.

Doc: http://docs.ansible.com/ansible/become.html

Avalon
  • 912
  • 6
  • 16
4

In version 2.0 use copy module with remote_src parameter.

- name: Move workspace3 directory to /usr/share/ Folder
  become: yes
  copy:
    remote_src: yes
    src: /tmp/workspace3
    dest: /usr/share/

If you want to move file you need to delete old file with file module

- name: Remove old files foo
  file:
    path: /tmp/workspace3
    state: absent

Hope that help you.

Arbab Nazar
  • 22,378
  • 10
  • 76
  • 82
  • Note that if you try to copy folder that has files in it it will not work (ansible 2.2.2.0), you need to use alternative from http://stackoverflow.com/questions/24162996/how-to-move-rename-a-file-using-an-ansible-task-on-a-remote-system#comment47150350_24457830 – Kyslik Apr 24 '17 at 10:28