How to copy file in ansible?

How to copy file in ansible?

copy module is available in ansible to copy file to different file path.

srr and dest parameters are required to specify source and destination files path.

- hosts: localhost
  connection: local
  gather_facts: no

  tasks:

	- name: initialize file path
  	set_fact:
    	src_file_path: "test/testing.txt"

	- name: destination file
  	set_fact:
    	dest_file_path: "test/testing1.txt"

	- name: "Copy {{ src_file_path }} file in {{ dest_file_path }}"
  	copy:
    	src: "{{ src_file_path }}"
    	dest: "{{ dest_file_path }}"

	- name: Print the msg
  	debug:
    	msg: "Copied {{ src_file_path }} file."

Output:

$ ansible-playbook copy_files.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [localhost] ************************************************************************************************************************************************************

TASK [initialize file path] *************************************************************************************************************************************************
ok: [localhost]

TASK [destination file] *****************************************************************************************************************************************************
ok: [localhost]

TASK [Copy test/testing.txt file in test/testing1.txt] **********************************************************************************************************************
changed: [localhost]

TASK [Print the msg] ********************************************************************************************************************************************************
ok: [localhost] => {
	"msg": "Copied test/testing.txt file."
}

PLAY RECAP ******************************************************************************************************************************************************************
localhost              	: ok=4	changed=1	unreachable=0	failed=0	skipped=0	rescued=0	ignored=0   

Lets see whether file is copied correctly or not,

$ ls -all test/
-rw-rw-r--. 1 root root   48 Nov 13 07:15 testing1.txt
-rw-rw-r--. 1 root root  48 Nov 13 07:14 testing.txt

Here we can see file permission is created as same as source file.

How to copy file with file permission?

mode parameter is used to specify the file permissions to the destination file.

mode value can be octal format numbers or symbolic format.

- hosts: localhost
  connection: local
  gather_facts: no

  tasks:

	- name: initialize file path
  	set_fact:
    	src_file_path: "test/testing.txt"

	- name: destination file
  	set_fact:
    	dest_file_path: "test/testing1.txt"

	- name: "Copy {{ src_file_path }} file in {{ dest_file_path }}"
  	copy:
    	src: "{{ src_file_path }}"
    	dest: "{{ dest_file_path }}"
    	mode: '0644'

	- name: Print the msg
  	debug:
    	msg: "Copied {{ src_file_path }} file."

Output:

$ ansible-playbook copy_files.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [localhost] ************************************************************************************************************************************************************

TASK [initialize file path] *************************************************************************************************************************************************
ok: [localhost]

TASK [destination file] *****************************************************************************************************************************************************
ok: [localhost]

TASK [Copy test/testing.txt file in test/testing1.txt] **********************************************************************************************************************
changed: [localhost]

TASK [Print the msg] ********************************************************************************************************************************************************
ok: [localhost] => {
	"msg": "Copied test/testing.txt file."
}

PLAY RECAP ******************************************************************************************************************************************************************
localhost              	: ok=4	changed=1	unreachable=0	failed=0	skipped=0	rescued=0	ignored=0  

Lets see whether files copied with mentioned file permissions,

$ ls -all test/
-rw-r--r--. 1 root root   48 Nov 13 07:58 testing1.txt
-rw-rw-r--. 1 root root   48 Nov 13 07:14 testing.txt

testing1.txt is created with different file permission than source file testing.txt. here read and write to owner, read only to gorup and other users as speciifed in mode parameter '644'.

How to copy file with file permission using symbolic representation?

- hosts: localhost
  connection: local
  gather_facts: no

  tasks:

	- name: initialize file path
  	set_fact:
    	src_file_path: "test/testing.txt"

	- name: destination file
  	set_fact:
    	dest_file_path: "test/testing1.txt"

	- name: "Copy {{ src_file_path }} file in {{ dest_file_path }}"
  	copy:
    	src: "{{ src_file_path }}"
    	dest: "{{ dest_file_path }}"
    	mode: u=rw,g=r,o=r

	- name: Print the msg
  	debug:
    	msg: "Copied {{ src_file_path }} file."

Output:

$ ansible-playbook copy_files.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [localhost] ************************************************************************************************************************************************************

TASK [initialize file path] *************************************************************************************************************************************************
ok: [localhost]

TASK [destination file] *****************************************************************************************************************************************************
ok: [localhost]

TASK [Copy test/testing.txt file in test/testing1.txt] **********************************************************************************************************************
changed: [localhost]

TASK [Print the msg] ********************************************************************************************************************************************************
ok: [localhost] => {
	"msg": "Copied test/testing.txt file."
}

PLAY RECAP ******************************************************************************************************************************************************************
localhost              	: ok=4	changed=1	unreachable=0	failed=0	skipped=0	rescued=0	ignored=0   

Lets see whether files copied with mentioned file permissions,

$ ls -all test/
-rw-r--r--. 1 root root  48 Nov 13 07:58 testing1.txt
-rw-rw-r--. 1 root root   48 Nov 13 07:14 testing.txt

testing1.txt is created with different file permission than source file testing.txt. here read and write to owner, read only to gorup and other users as speciifed in mode parameter 'u=rw,g=r,o=r'.

Adding some permissions and removing others

Another way of symbolic mode example like adding some permissions and removing for others

- hosts: localhost
  connection: local
  gather_facts: no

  tasks:

	- name: initialize file path
  	set_fact:
    	src_file_path: "test/testing.txt"

	- name: destination file
  	set_fact:
    	dest_file_path: "test/testing1.txt"

	- name: "Copy {{ src_file_path }} file in {{ dest_file_path }}"
  	copy:
    	src: "{{ src_file_path }}"
    	dest: "{{ dest_file_path }}"
    	mode: u+rwx,g+rx,o-wx

	- name: Print the msg
  	debug:
    	msg: "Copied {{ src_file_path }} file."

Lets see whether files copied with mentioned file permissions,

$ ls -all test/
-rwxrwxr--. 1 root root   48 Nov 13 09:37 testing1.txt
-rw-rw-r--. 1 root root   48 Nov 13 07:14 testing.txt

testing1.txt is created with different file permission than source file testing.txt. here read, write and execute to owner and group users, read only to other users as speciifed in mode parameter u+rwx,g+rx,o-wx.

How to copy file with owner?

owner parameter is used to specifiy the destination file owner and also group parameter is used to specify group users.

- hosts: localhost
  connection: local
  gather_facts: no

  tasks:

	- name: initialize file path
  	set_fact:
    	src_file_path: "test/testing.txt"

	- name: destination file
  	set_fact:
    	dest_file_path: "test/testing1.txt"

	- name: "Copy {{ src_file_path }} file in {{ dest_file_path }}"
  	copy:
    	src: "{{ src_file_path }}"
    	dest: "{{ dest_file_path }}"
    	owner: user1
    	group: user1
    	mode: '0644'

	- name: Print the msg
  	debug:
    	msg: "Copied {{ src_file_path }} file."

Output:

$ ansible-playbook copy_files.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [localhost] ************************************************************************************************************************************************************

TASK [initialize file path] *************************************************************************************************************************************************
ok: [localhost]

TASK [destination file] *****************************************************************************************************************************************************
ok: [localhost]

TASK [Copy test/testing.txt file in test/testing1.txt] **********************************************************************************************************************
changed: [localhost]

TASK [Print the msg] ********************************************************************************************************************************************************
ok: [localhost] => {
	"msg": "Copied test/testing.txt file."
}

PLAY RECAP ******************************************************************************************************************************************************************
localhost              	: ok=4	changed=1	unreachable=0	failed=0	skipped=0	rescued=0	ignored=0   

Lets see whether files copied with mentioned owner,

$ ls -all test/
-rw-r--r--. 1 user1 user1   48 Nov 13 09:37 testing1.txt
-rw-rw-r--. 1 root root  48 Nov 13 07:14 testing.txt

testing1.txt is created with a different owner than source file testing.txt.




Python installation

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^