Views: 13029
Run Command on lots of servers via ansible copy directories and files
With Ansible you turn most fields into variables. I need to copy a bunch of files to servers so here is how to do it. You can find more examples at the link below
Openshift heavily uses ansible as part of its installation as well as daily pod building.
Read More at: Ansible – Openshift
However, in order to run ansible you only need to install it on a single server that has passwordless ssh to other servers. Put the other servers into your /etc/ansible/hosts file and start to run cli commands on all of your servers. These only run commands, they don’t run interactively, so dont expect to edit with vi on several servers at a time.
/etc/ansible/hosts
[my-nodes]
node1
node2
node3
some-other-node
Then run:
$ ansible nodes -a "ls -al"
This will show you the file list in the default directory of all the servers listed under [nodes] in your /etc/ansible/hosts file.
Ansible installs modules via ssh to the nodes. The above command uses the default “command” modules. Many common Linux operations have a module. To call a module use -m <module-name>
So in order to copy a file to all of your servers:
ansible nodes -m copy -a "src=/path/to-original/file dest=/path/to/destination"
- hosts: all tasks: - name: Copy multiple files in Ansible with different permissions copy: src: "{{ item.src }}" dest: "{{ item.dest }}" mode: "{{item.mode}}" with_items: - { src: '/home/mdtutorials2/test1',dest: '/tmp/devops_system1', mode: '0777'} - { src: '/home/mdtutorials2/test2',dest: '/tmp/devops_system2', mode: '0707'} - { src: '/home/mdtutorials2/test3',dest: '/tmp2/devops_system3', mode: '0575'}
It can also make backup copies
- hosts: blocks tasks: - name: ansible copy file backup example copy: src: ~/helloworld.txt dest: /tmp backup: yes
Source: How to Copy Files and Directories in Ansible Using Copy and Fetch Modules – Ansible Tutorials
Also: https://docs.ansible.com/ansible/latest/user_guide/intro_adhoc.html