Ansible copy template file to remote server

#Introduction

How to copy the template file to the remote server using Ansible. I use vagrant to create VMs. My example Ansible creates multiple servers here. In Ansible I use with_fileblob:

Ansible copy template file to remote server

Ansible file and folder

[vagrant@ansible_controller ansible]$ tree .
.
├── ansible.cfg
├── copy-template.yml
├── hosts
└── roles
    └── copyfiles
        ├── tasks
        │   └── main.yml
        └── templates
            ├── devopsroles.conf.tmp
            ├── file1.conf.tmp
            └── file2.conf.tmp

4 directories, 7 files

Ansible copy template file to remote server script

copy-template.yml file.

---
- hosts: web-server
  become: yes
  roles:
       - copyfiles

Example: roles/copyfiles/tasks/main.yml file

---
- name: "Copy files template to remote server"
  template:
    src: "{{ item }}"
    dest: "/home/vagrant/dest/{{ item | basename | regex_replace('.tmp','') }}"
    owner: "root"
    group: "root"
    mode: 0644
  with_fileglob:
    - templates/*.tmp

Ansible run command 

[vagrant@ansible_controller ansible]$ ansible-playbook -i hosts copy-template.yml

The output terminal on the ansible controller

[vagrant@ansible_controller ansible]$ ansible-playbook -i hosts copy-template.yml
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details

PLAY [web-server] ***************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************
ok: [server1]

TASK [copyfiles : Copy files template to remote server] *************************************************************
changed: [server1] => (item=/home/vagrant/ansible/roles/copyfiles/templates/devopsroles.conf.tmp)
changed: [server1] => (item=/home/vagrant/ansible/roles/copyfiles/templates/file1.conf.tmp)
changed: [server1] => (item=/home/vagrant/ansible/roles/copyfiles/templates/file2.conf.tmp)

PLAY RECAP **********************************************************************************************************
server1                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

The result on remote server1.

[vagrant@server1 dest]$ pwd
/home/vagrant/dest
[vagrant@server1 dest]$ ll
total 12
-rw-r--r--. 1 root root 3 Jan 17 08:53 devopsroles.conf
-rw-r--r--. 1 root root 2 Jan 17 08:53 file1.conf
-rw-r--r--. 1 root root 2 Jan 17 08:53 file2.conf

Conclusion

You have to use Ansible copy template file to remote server. I hope will this your helpful. Thank you for reading the DevopsRoles page!

About HuuPV

My name is Huu. I love technology, especially Devops Skill such as Docker, vagrant, git, and so forth. I like open-sources, so I created DevopsRoles.com to share the knowledge I have acquired. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.