<p>I have used below code to resolve the issue.</p>

<pre><code>    - name: Copy var directory
      shell: |
        cp -rpf /home_old/* /home
        cp -rpf /var_old/* /var
      args:
        executable: /bin/bash
</code></pre>

<p>Thanks.</p>

CC BY-SA 4.0
2020-04-15T17:51:05.560Z
2020-04-15T17:51:05.560Z
Santosh Garole
<p>The <a href="https://docs.ansible.com/ansible/latest/modules/copy_module.html" rel="nofollow noreferrer">copy</a> module copies a file from the local or remote machine to a location on the remote machine. </p>

<p>In order to copy the file from the remote machine itself, set <code>remote_src</code> to <code>yes</code>.</p>

<pre><code>- name: Copy multiple Directories
          copy:
            src: "{{ item.src }}"
            dest: "{{ item.dest }}"
            mode: "{{ item.mode }}"
            remote_src: yes
          with_items:
             - { src: '/home_old/',dest: '/home/',mode: preserve}
             - { src: '/var_old/',dest: '/var/',mode: preserve}
</code></pre>

CC BY-SA 4.0
2020-04-14T18:32:27.233Z
2020-04-14T18:32:27.233Z
Siva
<p>I am using the below playbook for mounting partitions on another EBS volumes while provisioning instance on AWS </p>

<pre><code>    ---
    - name: Mounting necessary partitions
      hosts: localhost
      become: yes
      become_user: root

      tasks:

        - name: Ansible formating partitions.
          command: "{{ item }}"
          loop:
            - mkswap -f {{ ebs_swap }}
            - mkfs.xfs -f {{ ebs_home }}
            - mkfs.xfs -f {{ ebs_var }}
            - mkfs.xfs -f {{ ebs_tmp }}
            - mkfs.xfs -f {{ ebs_app }}   
            - mv -f /home /home_old
            - mv -f /var /var_old    

        - name: Dispaly uuid &amp; store in variable
          command: blkid -s UUID -o value {{ ebs_swap }}
          register: uuid_swap

        - name: Dispaly uuid &amp; store in variable
          command: blkid -s UUID -o value {{ ebs_home }}
          register: uuid_home

        - name: Dispaly uuid &amp; store in variable
          command: blkid -s UUID -o value {{ ebs_var}}
          register: uuid_var

        - name: Dispaly uuid &amp; store in variable
          command: blkid -s UUID -o value {{ ebs_tmp }}
          register: uuid_tmp

        - name: Dispaly uuid &amp; store in variable
          command: blkid -s UUID -o value {{ ebs_app }}
          register: uuid_app

        - name: Creates directory
          file:
            path: "{{ item }}"
            state: directory
          with_items:
            - /home
            - /var
            - /tmp
            - /app

        - name: Add the below lines
          blockinfile:
            path: /etc/fstab
            state: present
            block: |
              UUID={{ uuid_swap.stdout }}   swap      swap defaults                         0   0
              UUID={{ uuid_home.stdout }}   /home     xfs  defaults,nodev                   0   0
              UUID={{ uuid_var.stdout }}    /var      xfs  defaults,noexec,nosuid,nodev     0   0
              UUID={{ uuid_tmp.stdout }}    /tmp      xfs  defaults,noexec,nosuid,nodev     0   0
              UUID={{ uuid_app.stdout }}    /app      xfs  defaults                         0   0

        - name: Mounting disks permanently
          command: "{{ item }}"
          loop:
            - swapon -a
            - mount -a

        - name: Copy multiple Directories
          copy:
            src: "{{ item.src }}"
            dest: "{{ item.dest }}"
            mode: "{{ item.mode }}"
          with_items:
             - { src: '/home_old/',dest: '/home/',mode: preserve}
             - { src: '/var_old/',dest: '/var/',mode: preserve}
</code></pre>

<p>I have tried multiple ways to copy the data back to newly mounted EBS volumes at task name
<code>Copying /home data on new EBS</code>.</p>

<p>I have tried using <code>copy</code> <code>synchronization</code> &amp; <code>rsync</code> modules. I know using <code>cp</code> is not recommended option but I can not see any other option related to syncing the data between to directories with preserving permissions.</p>

<p>But when I do it manually it works perfectly.</p>

<p>I am mounting these partitions on another EBS is just coz of CIS hardening steps includes it.</p>

<p>I have followed a couple of blogs already. such as 
1. <a href="https://stackoverflow.com/questions/34254164/what-is-cp-cannot-stat-error-in-unix-i-get-this-error-when-trying-to-copy-thin">one</a> 2. <a href="https://stackoverflow.com/questions/53494035/ansible-synchronize-module-asks-for-password-no-solution-yet">two</a></p>

<p>Can anyone help in solving this? This is very urgent.</p>

<blockquote>
  <p>ERROR: <code>failed: [localhost] (item={u'dest': u'/var/', u'src': u'/var_old/'}) =&gt; {"ansible_loop_var": "item", "changed": false, "item": {"dest": "/var/", "src": "/var_old/"}, "msg": "could not find src=/run/acpid.socket, Could not find or access '/run/acpid.socket' on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote_src option"}</code></p>
</blockquote>

<ansible><linux>
How to sync two directories on same host using Ansible?
Santosh Garole
# How to sync two directories on same host using Ansible?

(Asked by: username_0 on 2020-04-14)

I am using the below playbook for mounting partitions on another EBS volumes while provisioning instance on AWS 
    ---
    - name: Mounting necessary partitions
      hosts: localhost
      become: yes
      become_user: root

      tasks:

        - name: Ansible formating partitions.
          command: "{{ item }}"
          loop:
            - mkswap -f {{ ebs_swap }}
            - mkfs.xfs -f {{ ebs_home }}
            - mkfs.xfs -f {{ ebs_var }}
            - mkfs.xfs -f {{ ebs_tmp }}
            - mkfs.xfs -f {{ ebs_app }}   
            - mv -f /home /home_old
            - mv -f /var /var_old    

        - name: Dispaly uuid & store in variable
          command: blkid -s UUID -o value {{ ebs_swap }}
          register: uuid_swap

        - name: Dispaly uuid & store in variable
          command: blkid -s UUID -o value {{ ebs_home }}
          register: uuid_home

        - name: Dispaly uuid & store in variable
          command: blkid -s UUID -o value {{ ebs_var}}
          register: uuid_var

        - name: Dispaly uuid & store in variable
          command: blkid -s UUID -o value {{ ebs_tmp }}
          register: uuid_tmp

        - name: Dispaly uuid & store in variable
          command: blkid -s UUID -o value {{ ebs_app }}
          register: uuid_app

        - name: Creates directory
          file:
            path: "{{ item }}"
            state: directory
          with_items:
            - /home
            - /var
            - /tmp
            - /app

        - name: Add the below lines
          blockinfile:
            path: /etc/fstab
            state: present
            block: |
              UUID={{ uuid_swap.stdout }}   swap      swap defaults                         0   0
              UUID={{ uuid_home.stdout }}   /home     xfs  defaults,nodev                   0   0
              UUID={{ uuid_var.stdout }}    /var      xfs  defaults,noexec,nosuid,nodev     0   0
              UUID={{ uuid_tmp.stdout }}    /tmp      xfs  defaults,noexec,nosuid,nodev     0   0
              UUID={{ uuid_app.stdout }}    /app      xfs  defaults                         0   0

        - name: Mounting disks permanently
          command: "{{ item }}"
          loop:
            - swapon -a
            - mount -a

        - name: Copy multiple Directories
          copy:
            src: "{{ item.src }}"
            dest: "{{ item.dest }}"
            mode: "{{ item.mode }}"
          with_items:
             - { src: '/home_old/',dest: '/home/',mode: preserve}
             - { src: '/var_old/',dest: '/var/',mode: preserve}

I have tried multiple ways to copy the data back to newly mounted EBS volumes at task name
Copying /home data on new EBS.
I have tried using copy synchronization & rsync modules. I know using cp is not recommended option but I can not see any other option related to syncing the data between to directories with preserving permissions.
But when I do it manually it works perfectly.
I am mounting these partitions on another EBS is just coz of CIS hardening steps includes it.
I have followed a couple of blogs already. such as 
1. one 2. two
Can anyone help in solving this? This is very urgent.

ERROR: failed: [localhost] (item={u'dest': u'/var/', u'src': u'/var_old/'}) => {"ansible_loop_var": "item", "changed": false, "item": {"dest": "/var/", "src": "/var_old/"}, "msg": "could not find src=/run/acpid.socket, Could not find or access '/run/acpid.socket' on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote_src option"}

**Tags:** ansible, linux


---

## 2 answers

### Answer by username_1 on 2020-04-14. Score: 3 | Accepted answer

The copy module copies a file from the local or remote machine to a location on the remote machine. 
In order to copy the file from the remote machine itself, set remote_src to yes.
- name: Copy multiple Directories
          copy:
            src: "{{ item.src }}"
            dest: "{{ item.dest }}"
            mode: "{{ item.mode }}"
            remote_src: yes
          with_items:
             - { src: '/home_old/',dest: '/home/',mode: preserve}
             - { src: '/var_old/',dest: '/var/',mode: preserve}


---

### Answer by username_0 on 2020-04-15. Score: 0

I have used below code to resolve the issue.
    - name: Copy var directory
      shell: |
        cp -rpf /home_old/* /home
        cp -rpf /var_old/* /var
      args:
        executable: /bin/bash

Thanks.


---

