Ansible copy files to multiple servers.

There are many ways to copy a file over to many servers, but I think using Ansible with a adhoc command is a simple way to get it done with my method.  For official documentation see here: http://docs.ansible.com/ansible/intro_adhoc.html#file-transfer

Step 1. Install Ansible

I like to install ansible in a virtualenv with pip.

Create a VirtualEnv

Install pre-reqs

  • Ubuntu: apt-get update && apt-get install python-apt python-pip build-essential python-dev git python-virtualenv -y
  • RHEL/CentOS: yum install python-pip git python-devel python-virtualenv gcc -y

Setup VirtualEnv

virtualenv /root/ansible
cd /root/ansible; source bin/activate

Install Ansible with Pip:

pip install paramiko PyYAML jinja2 httplib2 ansible

Step 2. Create the Inventory File

Next we setup the inventory file at /root/ansible/hosts.
Example inventory file:

web01  ansible_connection=local
web02 ansible_host=192.168.3.2
db01 ansible_host=10.1.2.3

[webservers]
web[01:02]

[dbservers]
db01

Step 3. Test

Here is the basics for running a adhoc command with ansible:

ansible <host-pattern> [-i inventory_file] [-m module_name] [-a module_args]

Test:

ansible web -i /root/ansible/hosts -m command -a ping

Note: The ansible ping module is not an ICMP ping. Instead, it tests Ansible’s connection to the hosts; it is an application-level ping.

Step 4. File Transfer

To transfer a file to many servers :

    ansible web -m copy -a “src=/path/file dest=/path/file” 

      If you use playbooks, you can also take advantage of the template module, which takes this a step further and change ownerships and permissions. 

      jim has written 83 articles

      Leave a Reply

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

      You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>