<p>You can set up a proxy. A <a href="https://en.wikipedia.org/wiki/Reverse_proxy" rel="nofollow noreferrer">reverse proxy</a> can be a good choice for this kind of setup. You would configure each machine to use the proxy as its apt/pip repository. The first time a package is requested the proxy downloads it from the internet and stores it, the next machine that requires it will get it from the proxy directly.</p>

<p>One popular choice is Squid. Another one is Nginx, set up in a reverse proxy configuration.</p>

<p>The advantage over mirroring entire repositories is that you'll only get the packages you need. The advantage over having Ansible copy the files is that it acts like the real proxy, dependencies and upgrades and all. But you'll  have to change apt and pip repository configurations. Or "hijack" those repository names through your local DNS.</p>

CC BY-SA 4.0
2019-02-11T16:34:42.643Z
2019-02-11T16:34:42.643Z
bgdnlp
<p>If your local network is faster than your internet connexion you can do the following: </p>

<ol>
<li>Download the tarball in your ansible host using run_once option.</li>
<li>Copy it to each target. </li>
<li>Install it locally from a file ; most of package managers support local install.</li>
</ol>

<p><strong>Yum rpm example</strong></p>

<pre><code>- name: Download RPM locally
  get_url:
    url: http://example.com/path/file.rpm
    dest: /local_path/file.rpm
  delegate_to: localhost
  run_once: yes

- name: Copying file.rpm 
  copy:
    src: /local_path/file.rpm
    dest: /remote_path/file.rpm

- name: Install rpm from a local file
  yum:
    name: /remote_path/file.rpm
    state: present
</code></pre>

<p><strong>Local install example with pip</strong>  </p>

<pre><code>- pip:
  name: file:///remote_path/file.gz
</code></pre>

<p><strong>Local install example with apt</strong></p>

<pre><code>- name: Install a .deb package
  apt:
    deb: /remote_path/file.deb
</code></pre>

<p>The problem with this method, is when you have many dependencies ; you have to download and install them all before your package (Optionally with a requirements.txt or so depending on the package manager you use..)</p>

CC BY-SA 4.0
2019-01-23T13:28:40.093Z
Thanks. Yes dependencies is a problem with this method. Another problem is to find out all rpm and pip packages' link manually, right?
Can
CC BY-SA 4.0
2019-01-24T08:44:20.347Z
@Can: It depends .. if you use a local repository or a nexus server which is a common practice, finding packages is not a hard task.
Also consider using variables for all links to make your code maintainable
storm
CC BY-SA 4.0
2019-01-23T08:47:46.270Z
2019-01-23T09:12:31.290Z
2019-01-23T09:12:31.290Z
storm
<blockquote>
  <p>Can Ansible use a local repository?</p>
</blockquote>

<p>I just take the title because the answer lies in it. </p>

<blockquote>
  <p>What is the best practice when there are lots of hosts and lots of packages?</p>
</blockquote>

<p>You have a many hosts, hopefully from the same distribution. So it would make sense to create a local mirror (repository), which can be on your Ansible machine by the way, and to configure, for either <a href="https://packaging.python.org/guides/index-mirrors-and-caches/" rel="nofollow noreferrer">pip</a> or <a href="https://help.ubuntu.com/community/Rsyncmirror" rel="nofollow noreferrer">apt</a>. </p>

<p>This takes space, but will save you a lot of time downloading updates and packages. </p>

<p>You have to calculate if it is worth it, as it takes a fair amount of storage, but setting this up isn't very complicated.</p>

<p>Then just update the <code>apt</code> configuration of each hosts to point to your local mirror with <em>Ansible</em>. </p>

<blockquote>
  <p>Can ansible download these deb or pip packages to its local (or anywhere) and install to each host to decrease time.</p>
</blockquote>

<p>For that part, I believe @storm gave a very good answer:</p>

<pre><code>delegate_to: localhost
run_once: yes
</code></pre>

CC BY-SA 4.0
2019-01-24T07:29:45.490Z
2019-01-24T07:29:45.490Z
jayooin
<p>I have multiple hosts and my playbook has apt, pip modules to download necessary packages from repositories. When any (apt,pip) download module attempt to download packages, it does it for each host one by one. So I have to wait too long. Can ansible download these deb or pip packages to its local (or anywhere) and install to each host to decrease time.
What is the best practice when there are lots of hosts and lots of packages?</p>

<ansible>
Can Ansible use a local repository?
Can
# Can Ansible use a local repository?

(Asked by: username_0 on 2019-01-22)

I have multiple hosts and my playbook has apt, pip modules to download necessary packages from repositories. When any (apt,pip) download module attempt to download packages, it does it for each host one by one. So I have to wait too long. username_0 ansible download these deb or pip packages to its local (or anywhere) and install to each host to decrease time.
What is the best practice when there are lots of hosts and lots of packages?

**Tags:** ansible


---

## 3 answers

### Answer by username_1 on 2019-02-11. Score: 2

You can set up a proxy. A [reverse proxy](https://en.wikipedia.org/wiki/Reverse_proxy) can be a good choice for this kind of setup. You would configure each machine to use the proxy as its apt/pip repository. The first time a package is requested the proxy downloads it from the internet and stores it, the next machine that requires it will get it from the proxy directly.

One popular choice is Squid. Another one is Nginx, set up in a reverse proxy configuration.

The advantage over mirroring entire repositories is that you'll only get the packages you need. The advantage over having Ansible copy the files is that it acts like the real proxy, dependencies and upgrades and all. But you'll have to change apt and pip repository configurations. Or "hijack" those repository names through your local DNS.


---

### Answer by username_2 on 2019-01-23. Score: 1

If your local network is faster than your internet connexion you can do the following: 

Download the tarball in your ansible host using run_once option.
Copy it to each target. 
Install it locally from a file ; most of package managers support local install.

Yum rpm example
- name: Download RPM locally
  get_url:
    url: http://example.com/path/file.rpm
    dest: /local_path/file.rpm
  delegate_to: localhost
  run_once: yes

- name: Copying file.rpm 
  copy:
    src: /local_path/file.rpm
    dest: /remote_path/file.rpm

- name: Install rpm from a local file
  yum:
    name: /remote_path/file.rpm
    state: present

Local install example with pip 
- pip:
  name: file:///remote_path/file.gz

Local install example with apt
- name: Install a .deb package
  apt:
    deb: /remote_path/file.deb

The problem with this method, is when you have many dependencies ; you have to download and install them all before your package (Optionally with a requirements.txt or so depending on the package manager you use..)

> **Comments:**
> **username_0**: Thanks. Yes dependencies is a problem with this method. Another problem is to find out all rpm and pip packages' link manually, right?  
> **username_2**: @username_0: It depends .. if you use a local repository or a nexus server which is a common practice, finding packages is not a hard task. Also consider using variables for all links to make your code maintainable  

---

### Answer by username_3 on 2019-01-24. Score: 1

username_0 Ansible use a local repository?

I just take the title because the answer lies in it. 

What is the best practice when there are lots of hosts and lots of packages?

You have a many hosts, hopefully from the same distribution. So it would make sense to create a local mirror (repository), which can be on your Ansible machine by the way, and to configure, for either pip or apt. 
This takes space, but will save you a lot of time downloading updates and packages. 
You have to calculate if it is worth it, as it takes a fair amount of storage, but setting this up isn't very complicated.
Then just update the apt configuration of each hosts to point to your local mirror with Ansible. 

username_0 ansible download these deb or pip packages to its local (or anywhere) and install to each host to decrease time.

For that part, I believe @username_2 gave a very good answer:
delegate_to: localhost
run_once: yes


---

