Architecting Enterprise Egress Control with Ansible and Squid Proxy

The deployment of a robust proxy infrastructure is a cornerstone of secure network architecture, particularly when managing egress traffic in highly regulated environments. By leveraging Ansible, an open-source automation engine, administrators can transition from manual, error-prone installations to a state of predictable, repeatable, and scalable proxy deployments. This integration focuses on Squid, a sophisticated forward proxy for the web that supports HTTP, HTTPS, and FTP. Squid functions by intercepting network traffic directed toward external networks—typically the internet—and forwarding those requests on behalf of internal systems. This intermediary role not only enhances security by masking internal IP addresses but also optimizes network performance through the caching and reuse of frequently requested web pages, thereby reducing total bandwidth consumption and improving response times for end-users.

In the context of the Ansible Automation Platform, an egress proxy becomes critical for ensuring that platform components can communicate with external resources. Whether the deployment utilizes containerized methods via Podman or traditional RPM installations, the egress proxy serves as the bridge. For containerized environments, configuring the system proxy at the Podman level often resolves the majority of connectivity hurdles. However, for RPM-based installations, a dual-layer configuration is required: both the system-level and the component-level proxy settings must be meticulously aligned to ensure seamless traffic flow.

Technical Specifications and Role Defaults

The operational efficiency of a Squid proxy is determined by its initial configuration parameters. When using an Ansible role to manage Squid, these parameters are typically defined in a defaults file, such as roles/squid/defaults/main.yml. This ensures that the proxy behaves consistently across different environments, from development to production.

The following table delineates the primary configuration defaults used for a standard Ansible-managed Squid deployment:

Parameter Default Value Technical Purpose
squid_port 3128 The TCP port on which Squid listens for client requests.
squid_cache_dir /var/spool/squid The filesystem path where cached web content is stored.
squid_cache_size_mb 10000 The maximum disk space allocated for the cache (10GB).
squid_max_object_size_mb 100 The maximum size of a single object that can be stored in the cache.
squid_log_dir /var/log/squid The directory where access and system logs are archived.

The definition of these variables allows the Ansible role to dynamically generate the squid.conf file. For instance, the squid_port is not merely a number but the gateway through which all client traffic enters the proxy. If this port is blocked by a firewall, the entire proxy service becomes unreachable, rendering the deployment a failure. Similarly, the squid_cache_dir must be mapped to a partition with sufficient IOPS and storage capacity to prevent system crashes due to disk exhaustion.

Network Access Control and Security Policies

A primary function of a forward proxy is to control who can access the internet and what content they can retrieve. Ansible enables the definition of these policies through structured lists in the variable files, which are then processed by Jinja2 templates.

The configuration includes a granular definition of allowed networks to prevent unauthorized access to the proxy server.

  • internal_net: defined as 10.0.0.0/8
  • office_net: defined as 192.168.1.0/24

By specifying these CIDR blocks, the administrator ensures that only devices within the trusted organizational boundary can utilize the proxy. This prevents the proxy from becoming an "open proxy," which could be exploited by external actors to launch attacks or bypass their own network restrictions.

Beyond network access, Squid allows for content filtering via blocked domains and file extensions. This is implemented by deploying specific lists to the server, such as blocked_domains.txt.

  • Blocked Domains: .facebook.com, .twitter.com, .tiktok.com, .reddit.com
  • Blocked Extensions: .exe, .msi, .torrent

The technical layer of this restriction involves Squid checking the requested URL against these lists before forwarding the request. The impact is a significant increase in organizational productivity and a reduction in the risk of malware infiltration, as executable files (.exe, .msi) and peer-to-peer sharing files (.torrent) are intercepted at the perimeter.

Advanced Proxy Features: Authentication and Temporal Control

For environments requiring higher security, Ansible can be used to toggle authentication and time-based access rules. While these may be disabled by default (squid_auth_enabled: false), they can be activated to ensure that only verified users can access external resources.

When authentication is enabled, the system requires a user database. For example:

  • Username: proxyuser
  • Password: {{ vault_squid_proxy_password }}

The use of vault_squid_proxy_password indicates the integration of Ansible Vault, a security feature used to encrypt sensitive data at rest. This prevents passwords from appearing in plain text within version control systems like GitHub or GitLab.

Furthermore, Squid supports time-based access control, allowing administrators to restrict internet access to specific business hours.

  • Start Time: 09:00
  • End Time: 18:00
  • Days: MTWHF (Monday, Tuesday, Wednesday, Thursday, Friday)

This temporal restriction is processed by the Squid daemon, which denies requests falling outside these parameters. This is particularly useful in educational or corporate settings to ensure that network resources are used primarily for professional purposes.

Ansible Task Implementation and Deployment Workflow

The actual deployment of the Squid proxy is handled through a series of Ansible tasks located in roles/squid/tasks/main.yml. These tasks follow a logical sequence to move the system from a clean state to a fully operational proxy.

The first step involves the installation of the necessary software packages.

yaml - name: Install Squid proxy server apt: name: - squid - apache2-utils state: present update_cache: yes

The inclusion of apache2-utils is critical for providing the htpasswd utility, which is necessary for creating the password files used in authenticated proxy setups.

Following installation, the environment must be prepared by creating the cache directory.

yaml - name: Create Squid cache directory file: path: "{{ squid_cache_dir }}" state: directory owner: proxy group: proxy mode: '0755'

The ownership is set to the proxy user and group to ensure the Squid service has the necessary permissions to write cached data to the disk.

The core of the configuration is the deployment of the squid.conf file via a template.

yaml - name: Deploy Squid configuration template: src: squid.conf.j2 dest: /etc/squid/squid.conf owner: root group: root mode: '0644' validate: "squid -k parse -f %s" notify: restart squid

The validate parameter is a critical safety mechanism. It executes squid -k parse -f %s to check the syntax of the configuration file before it is finalized. If the syntax is invalid, the task fails, preventing the deployment of a broken configuration that would crash the proxy service. The notify trigger ensures that the squid service is restarted only if the configuration file has actually changed.

Egress Proxy Configuration for Ansible Automation Platform

When integrating Squid as an egress proxy for the Ansible Automation Platform, specific port configurations must be applied to allow the control plane to function. A failure to open these ports will result in the platform being unable to perform essential tasks, such as downloading updates or communicating with remote nodes.

The following access control lists (ACLs) must be configured within Squid to ensure the platform's stability.

Safe Ports:

  • acl Safe_ports port 81
  • acl Safe_ports port 82
  • acl Safe_ports port 389
  • acl Safe_ports port 444
  • acl Safe_ports port 445

SSL Ports:

  • acl SSL_ports port 22
  • acl SSL_ports port 444
  • acl SSL_ports port 445
  • acl SSL_ports port 8443
  • acl SSL_ports port 8444
  • acl SSL_ports port 8445
  • acl SSL_ports port 8446
  • acl SSL_ports port 44321
  • acl SSL_ports port 44322

To enforce these rules, the following access denials are implemented:

text http_access deny !Safe_ports http_access deny CONNECT !SSL_ports

This configuration tells Squid to deny any request that does not match the defined Safe_ports or SSL_ports. This "deny-by-default" approach is a security best practice, ensuring that the proxy cannot be used to tunnel traffic over non-standard ports.

Certificate Management and Transparent Proxy Automation

In advanced scenarios, such as the deployment of a transparent proxy on Rocky Linux, the use of SSL/TLS certificates is required to intercept and inspect encrypted traffic. Automating the generation of these certificates via Ansible can be challenging because the openssl command-line tool is designed to be interactive.

To automate certificate generation, administrators must use non-interactive parameters such as -subject, -passin, and -passout. A typical manual process for creating a self-signed certificate involves the following command:

bash openssl req -new -newkey rsa:4096 -sha256 -days 3650 -nodes -x509 -extensions v3_ca -keyout certificat.pem -out certificat.pem

In this command:
- rsa:4096 specifies the key strength.
- -days 3650 sets the certificate validity to 10 years.
- -nodes ensures the private key is not encrypted with a passphrase, which is necessary for the proxy to start automatically without manual password entry.

The process also requires the definition of the Distinguished Name (DN) fields:
- Country Name: FR
- State or Province: Gard
- Locality: Montpezat
- Organization: Microlinux
- Common Name: squidbox.sandbox.lan
- Email Address: [email protected]

After generating the .pem file, it must often be converted to DER format for compatibility with certain client devices. This is achieved with the following command:

bash openssl x509 -in certificat.pem -outform DER -out certificat.der

Within an Ansible playbook, these steps are implemented using the shell or command modules, passing the required DN information via the -subj flag to avoid the interactive prompts that would otherwise stall the automation process.

Monitoring and Maintenance of Squid Performance

Once the proxy is deployed via Ansible, it is essential to monitor its performance to ensure that the cache is functioning correctly and that the system is not overwhelmed by requests.

Real-time monitoring of traffic can be performed by observing the access logs:

bash tail -f /var/log/squid/access.log

For deeper insights into cache performance, the deployment includes a custom statistics script located at /usr/local/bin/squid-stats.sh. This script is deployed via the Ansible copy module.

```bash

!/bin/bash

Display Squid cache hit/miss statistics

echo "=== Squid Cache Statistics ==="
squidclient -h localhost -p {{ squidport }} mgr:info 2>/dev/null | grep -E "(Request|Hit|Miss|CPU)"
echo ""
echo "=== Cache Storage ==="
du -sh {{ squid
cache_dir }}
```

The technical operation of this script involves using squidclient to query the Squid manager (mgr:info) for internal statistics. The grep command filters for key metrics:
- Request: The total number of requests processed.
- Hit: Requests served from the local cache.
- Miss: Requests that required a fetch from the origin server.
- CPU: The resource utilization of the proxy process.

The du -sh command provides a human-readable summary of the actual disk space used by the cache directory, allowing administrators to verify if the squid_cache_size_mb limit is being approached.

Conclusion

The automation of a Squid proxy server through Ansible transforms a complex manual configuration process into a streamlined, professional-grade infrastructure deployment. By defining precise role defaults, implementing strict network access controls, and automating the creation of security certificates, organizations can establish a highly secure egress point for their network traffic. The integration of this setup within the Ansible Automation Platform ensures that all control plane communication remains uninterrupted while maintaining the security benefits of a forward proxy. The ability to validate configurations during the deployment phase and monitor performance through automated scripts ensures that the proxy remains stable and efficient, providing a scalable solution for managing internet access and optimizing bandwidth across the enterprise.

Sources

  1. OneUptime Blog
  2. GitHub - mrlesmithjr/ansible-squid
  3. Red Hat Documentation - Configuring Egress Proxy
  4. Rocky Linux Forums - Generate a certificate for Squid using Ansible

Related Posts