Ansible copy file - ansible

How can I copy a file from machine A to machine B and machine C on different location.
ie:
On machine A I have file abc and I want to copy it on the /tmp area of machine B and /op area of machine C

Assuming an inventory that was structured something like this:
[remote-servers]
192.168.X.1
192.168.X.10
192.168.X.20
192.168.X.30
ran the following copy task:
- name: copy the file to the remote machine
hosts: remote-servers
copy:
src: /path/to/file
dest: /path/to/dest

Related

send local files to specific remote hosts

I would like to copy the local files to remote hosts, the files in local are in the folder named by the remote host(as it shows in the screenshot), how could I send them to the remote host respectively? (*.pem in centos8-8 will be sent to centos8-8 only, etc). I tried group['clients'] as the loop but it does not work.
Thanks.
use the copy module with the magic variable inventory_hostname
- name: Copy dir
ansible.builtin.copy:
src: path/{{ inventory_hostname }}
dest: pathdest

ansible-playbook gather information from a file

I want to read a file by ansible and find specific thing and store all of them in a file in my localhost
for example there is /tmp/test file in all host and I want to grep specific thing in this file and store all of them in my home.
What should I do?
There might be many ways to accomplish this. The choice of Ansible modules (or even tools) can vary.
One approach is (using only Ansible):
Slurp the remote file
Write new file with filtered content
Fetch the file to Control machine
Example:
- hosts: remote_host
tasks:
# Slurp the file
- name: Get contents of file
slurp:
src: /tmp/test
register: testfile
# Filter the contents to new file
- name: Save contents to a variable for looping
set_fact:
testfile_contents: "{{ testfile.content | b64decode }}"
- name: Write a filtered file
lineinfile:
path: /tmp/filtered_test
line: "{{ item }}"
create: yes
when: "'TEXT_YOU_WANT' in item"
with_items: "{{ testfile_contents.split('\n') }}"
# Fetch the file
- name: Fetch the filtered file
fetch:
src: /tmp/filtered_test
dest: /tmp/
This will fetch the file to /tmp/<ANSIBLE_HOSTNAME>/tmp/filtered_test.
You can use the Ansible fetch module to download files from the remote system to your local system. You can then do the processing locally, as shown in this Ansible cli example:
REMOTE=[YOUR_REMOTE_SERVER]; \
ansible -m fetch -a "src=/tmp/test dest=/tmp/ansiblefetch/" $REMOTE && \
grep "[WHAT_YOU_ARE_INTERESTED_IN]" /tmp/ansiblefetch/$REMOTE/tmp/test > /home/user/ansible_files/$REMOTE
This snippet runs the ad-hoc version of Ansible, calling the module fetch with the source folder (on the remote) and the destination folder (locally) as arguments. Fetch copies the file into a folder [SRC]/[REMOTE_NAME]/[DEST], from which we then grep what we are interested in, and output that in the /home/user/ansible_files/$REMOTE.

How to run linux like cp command on same server..but copy says it does not find remote server

I am trying to emulate scenario of copying local file from one directory to another directory on same machine..but ansible copy command is looking for remote server always..
code I am using
- name: Configure Create directory
hosts: 127.0.0.1
connection: local
vars:
customer_folder: "{{ customer }}"
tasks:
- file:
path: /opt/scripts/{ customer_folder }}
state: directory
- copy:
src: /home/centos/absample.txt
dest: /opt/scripts/{{ customer_folder }}
~
I am running this play book like
ansible-playbook ab_deploy.yml --extra-vars "customer=ab"
So two problem i am facing
It should create a directory called ab under /opt/scripts/ but it creating folder as { customer_folder }}..its not taking ab as name of directory
second, copy as i read documentation, copy only work to copy files from local to remote machine, But i want is simply copy from local to local..
how can i achieve this..might be silly, i am just trying out things
Please suggest.
I solved it..i used cmd under shell module then it worked.

Bulk Copy Using Wildcard in Ansible

Please be informed that, im trying to copy a bulk files from my source server to the destination server using ansible. While trying an error. Please help me.
---
- name: Going to copy bulk files
hosts: test
vars_prompt:
- name: copy
prompt: Enter the Bulk File to Copy
private: no
tasks:
- name: Copy bulk files
shell: cp /tmp/guru/{{ copy }}* /ansible/sri
The shell module executes a shell command on the destination server, which explains the error message cp: cannot stat ‘/tmp/guru/a*’: No such file or directory: the source files of the cp does not exists on the destination server.
Ansible provide a lot of modules which are more appropriate to use than executing shell commands.
In your case, the copy module is the one you need: it copies files from source server to destination server. You can combine it with a with_fileglob loop:
tasks:
- name: Copy bulk files
copy:
src: "{{ item }}"
dest: /ansible/sri
with_fileglob: "/tmp/guru/{{ copy }}*"

Copying files from the specified hosts to others in ansible

I have three hosts available in my inventory file
[controller]
1.1.1.1
2.2.2.2
3.3.3.3
i have a variable in group_var folder which specifies the master node
master=1.1.1.1
sql.conf is available in my home directory (/home/ubuntu/sql.conf) of all 3 controller hosts.
Now, i need to copy the file (test.txt) from master to others . Is there any way in ansible to copy the files from one specific server to others.
i am trying like this but couldnt achieve though.
- hosts: all
sudo: yes
tasks:
- name: copy files
local_action: command rsync -a /home/ubuntu/test.txt {{ master }}:///home/ubuntu/test.txt
One option is to use the fetch module to copy the file from the master node to your local node, and then use the copy module normally to distribute that file to other nodes. Something like:
- hosts: master
tasks:
- fetch:
src: /path/to/myfile.txt
dest: tmp/
- hosts: all:!master
tasks:
- copy:
src: tmp/master/myfile.txt
dest: /path/to/myfile.txt

Resources