last modified files only moved ansible? - ansible

my requirement is moving files to remote host using ansible playbook.
my ansible script
---
- hosts: webservers
remote_user: root
tasks:
- copy: src=/home/bu/Bilal/site dest=/tmp owner=root group=root mode=777
when run playbook it has moved the file to remote.
when I have ran playbook again it will overwrite the whole folder again. I am looking, what are the files I have modified that files only get overwrite because my folder size is too large its taking so much time even single file change.

Take a look at the Synchronize module:
Uses rsync to make synchronizing file paths in your playbooks quick and easy.
Example:
- name: Sync files
synchronize:
src: "{{ conf.dev_path }}/"
dest: "{{ conf.host_path }}"
delete: yes
rsync_opts:
- "--exclude=.*"

Related

Is there a way to take a .zip file, copy it to a directory on a Windows target and extract the .zip in the directory using Ansible?

I am trying to take a .zip file and copy it to a directory and extract the .zip file afterwards on a Windows target using an Ansible playbook. I was wondering if there's a module or task that can be written to do this nice and clean.
The .zip file itself is stored on another server so the playbook task would be pulling from this location to get the file to copy to the target.
I use these two modules to accomplish that task. win_copy: could be used if the file is on your ansible control host.
- name: download zlib source
win_get_url:
url: "{{ zlib_url }}"
dest: "{{ boost_workdir }}"
register: download_zlib
until: download_zlib is succeeded
retries: 10
delay: 2
- name: unzip zlib source
win_unzip:
src: "{{ download_zlib.dest }}"
dest: "{{ boost_prefix }}"
delete_archive: true
creates: "{{ boost_prefix }}/zlib-{{ zlib_version }}/contrib"
copy: false

Is it possible to add a play inside a play that will use copy module on a define host vi ansible?

Hi I have a playbook that fetch information from remote server and place it local server, is it possible to add a play that will copy that file in local and place it on a specific host? I plan to code it below or you have any recommendation what would be best approach? although the server 1 is not in the inventory file that the playbook used.
- name: Get compliance reporting from remote
fetch:
src: /tmp/compliancereporting.out
dest: /home/ansible/linuxpatchingv2/OUTGOING-COMPLIANCE_v2/inventory_{{ '%y%m%d%H%M%S' | strftime }}
flat: yes
- name: Copy the fetch file
host: server1
copy:
src: /home/ansible/linuxpatchingv2/OUTGOING-COMPLIANCE_v2/inventory_*
dst: /tmp/
The fetch module will copy the file(s) from remote host to Ansible control machine at <dest>/ansible_hostname.
E.g. for host1.example.co and dest: /home/ansible/linuxpatchingv2:
/home/ansible/linuxpatchingv2/host1.example.co/tmp/compliancereporting.out
So in your playbook, you will have two plays. First play will fetch the file to Ansible control machine, second will copy the file fetched in first play to remote machine.
- name: Fetch the file from host1.example.co
hosts: host1.example.co
tasks:
- name: Fetch the file
fetch:
src: /tmp/compliancereporting.out
dest: /home/ansible/linuxpatchingv2/
- name: Copy the file to remote host server1
hosts: server1
tasks:
- name: Copy report to remote path
copy:
src: /home/ansible/linuxpatchingv2/host1.example.co/tmp/compliancereporting.out
dest: /tmp/

how to copy files and folders one folder to another on remote machine by using ansible-playbook?

i am new to ansible. i am trying to copy the files recursively ,got an error.
it does not support recursive.
host.ini
[web]
server1
server2
server3
ansible-playbook1:
tasks:
- name: copy files on remote machine
copy:
src: /home/anil/anilmainfolder
dest: /home/anil/anilbackup
remote_src: yes
i tried like this.but got error recursive problem.
tasks:
- name: copy files on remote machine
synchronize:
src: /home/anil/anilmainfolder
dest: /home/anil/anilbackup
deligate_to: {{ ansible_hostname }}
i also tried like this . but it copied folders and files on one of the server.
help me how to solve this plroblem/

How to delete local directory with ansible

I have to make delete some directory and create them on local before copy to the remote. Is there anyway to delete and create locally?
Currently I'm using 'command'
command: rm -r directory
But warning shows as
Consider using file module with state=absent rather than running rm
Is there any options we can use for local folder changes?
You can use diffrent delegation methods or use the local_action:
- local_action: file path=directory state=absent
If you're running this in a playbook, you can use a section of the playbook that uses a local connection to make changes on the command machine, then copies files to the remote:
---
- hosts: 127.0.0.1
connection: local
tasks:
- name: Delete local directory
file: path=/directory state=absent
- hosts: myhosts
tasks:
copy: src=/directory dest=/foo/directory
Update:
Current Ansible (2.10) does not like - local_action: , instead use delegate_to:
- name: Remove directory 'dir1'
file:
path: "path/to/dir1"
state: absent
delegate_to: localhost

Synchronize directories on remote host

I want to make a copy of a directory on my remote host. The task I tried to use is:
- name: copy old core configs to new Solr
become: yes
become_user: root
synchronize:
src="/usr/local/solr/example/solr/collection1/"
dest="/usr/local/solr-4.10.4/example/solr/collection1"
recursive=yes
delegate_to: "{{ inventory_hostname }}"
But it seemed like it was hanging forever and looking in iotop it didn't seem like anything was being copied. What I expect this to do is SSH into the remote host, and do an rsync from one directory to the other. Am I missing something?
You are mixing YAML syntax styles by using = instead of :. In my experience this can cause misleading errors.
Try:
- name: copy old core configs to new Solr
become: yes
become_user: root
synchronize:
src: "/usr/local/solr/example/solr/collection1/"
dest: "/usr/local/solr-4.10.4/example/solr/collection1"
recursive: yes
delegate_to: "{{ inventory_hostname }}"

Resources