Ansible: The copy module

Redhat Ansible

The module ansible.builtin.copy enables users to copy files from the controller node to managed nodes. It also helps to add/modify a destination file with a specific content.

In the below example we use three groups in inventory file as below:

  • [testGRP]
    192.168.48.129
    192.168.48.132
  • [dev]
    192.168.48.132
  • [test]
    192.168.48.129

The playbook uses to copy module to replace the content in /etc/issues file in the managed nodes with content based on the inventory group the specific managed node belongs, and also copies a file name sample.txt from the controller node to the managed nodes based on conditions.

[root@centos9vm ~]# cat sample.txt
Hello SAMPLE

[root@centos9vm ~]# ssh 192.168.48.132 ‘cat /etc/issues’
Hi

[root@centos9vm ~]# ssh 192.168.48.129 ‘cat /etc/issues’
Hi

[root@centos9vm ~]# ssh 192.168.48.132 ‘cat /root/sample.txt’
cat: /root/sample.txt: No such file or directory

[root@centos9vm ~]# ssh 192.168.48.129 ‘cat /root/sample.txt’
cat: /root/sample.txt: No such file or directory

[root@centos9vm ~]# cat modify.yml

======== ===
– – –
– name: Playbook to modify content of /etc/issues
  hosts: testGRP
  tasks:
    – name: Replace the content in dev hosts
      ansible.builtin.copy:
        content: “Development\n”
        dest: /etc/issues
      when:
        inventory_hostname is in groups[‘dev’]

    – name: Replace the content in test hosts
      ansible.builtin.copy:
        content: “Test\n”
        dest: /etc/issues
      when:
        inventory_hostname in groups[‘test’]

    – name: Copy a file to nodes in groups other than in TEST
      ansible.builtin.copy:
        src: sample.txt
        dest: /root/sample.txt
        owner: root
        group: apache
        mode: 0775
      when:
        inventory_hostname not in groups[‘test’]

==== ===

[root@centos9vm ~]# ansible-navigator run -m stdout modify.yml

PLAY [Playbook to modify content of /etc/issues] **************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************
ok: [192.168.48.129]
ok: [192.168.48.132]

TASK [Replace the content in dev hosts] ***********************************************************************************************
skipping: [192.168.48.129]
changed: [192.168.48.132]

TASK [Replace the content in test hosts] **********************************************************************************************
skipping: [192.168.48.132]
changed: [192.168.48.129]

TASK [Copy a file to nodes in groups other than in TEST] ******************************************************************************
skipping: [192.168.48.129]
changed: [192.168.48.132]

PLAY RECAP ****************************************************************************************************************************
192.168.48.129 : ok=2 changed=1 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0
192.168.48.132 : ok=3 changed=2 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0

==== ====

[root@centos9vm ~]# ssh 192.168.48.129 ‘cat /root/sample.txt’
cat: /root/sample.txt: No such file or directory

[root@centos9vm ~]# ssh 192.168.48.132 ‘cat /root/sample.txt’
Hello SAMPLE

[root@centos9vm ~]# ssh 192.168.48.129 ‘cat /etc/issues’
Test

[root@centos9vm ~]# ssh 192.168.48.132 ‘cat /etc/issues’
Development

[root@centos9vm ~]# ssh 192.168.48.132 ‘ls -l /root/sample.txt’
-rwxrwxr-x. 1 root apache 13 Jul 6 02:06 /root/sample.txt