Ansible, APT & Proxies

I’ve been working on a new Ansible setup which is behind a HTTP proxy and I’ve run into what seems to be a common problem: The Ansible modules apt_key and apt_repository don’t work behind proxies. There appear to be multiple bug reports in Ansible about these problems, but no resolution yet despite the bugs being several years old.

Adding a new repository to APT involves two steps: Adding the repository’s key to APT’s GPG keyring, then actually adding the repository.

Step 1 – Adding the key (Method 1)

The apt_key module in Ansible has the nice bug that it doesn’t honor any standard unix method for specifying use of a HTTP proxy.

Putting “Acquire::http::Proxy” in /etc/apt/apt.conf doesn’t work. Nor does setting the http_proxy environment variable.

Instead, you have to execute the apt-key command (not Ansible module) directly and supply a http-proxy parameter to it. This is from my playbook for installing Ansible:

- name: Add Ansible Repo Key
  command:
   argv:
    - /usr/bin/apt-key
    - adv
    - --keyserver-options
    - http-proxy=http://192.168.1.1:3128
    - --keyserver
    - hkp://keyserver.ubuntu.com:80
    - --recv-keys
    - 6125E2A8C77F2818FB7BD15B93C4A3FD7BB9C367

When adding other repositories, you may have to do some digging to get the ID of the key you wish to receive.

Step 1 – Adding the key (Method 2)

Method 1 only works if the repository key is in Ubuntu’s key server. If the key is in a third party site (such as what Gitlab do), things can get even more complicated. This is the best I’ve come up with.

The way I do it, is to create a temporary file, download the key into this temporary file and then pass the key to the apt_key module.

First off, create the temporary file:

- name: Create temp file
  tempfile:
   state: file
  register: tempfile_1

Now we download the key from the remote server and save it into our temporary file:

- name: Get Gitlab Key from remote server
  get_url:
   url: https://packages.gitlab.com/gpg.key
   dest: "{{ tempfile_1.path }}"
  environment:
   http_proxy: "http://192.168.1.1:3128"
   https_proxy: "http://192.168.1.1:3128"

Now we need to pass the file’s contents to apt_key. You may (as I was!) be tempted to use the lookup(‘file’, ‘/somewhere/filename’) helper function. This won’t work ☹

The lookup helper function runs on the local Ansible machine and not the remote machine we’re configuring. So lookup() fails to find the file because it doesn’t exist “here”, only “over there”.

Instead, we have to use the slurp module in Ansible to read the file contents into a variable.

- name: Read Gitlab Key into memory
  slurp:
   src: "{{ tempfile_1.path }}"
  register: key_data

And then we just pass this to apt_key:

- name: Add Gitlab Key
  apt_key:
   data: "{{ key_data['content'] }}"

Except this doesn’t work. This is because when the slurp module reads the file into memory, it Base64 encodes it. So when we read the data back out again, we need to Base64 decode it.

- name: Add Gitlab Key
  apt_key:
   data: "{{ key_data['content'] | b64decode }}"

Step 2 – Adding the repository

The apt_repository is slightly more helpful: It does honor the http_proxy variables. But you also need another variable: APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE This is needed as the apt_repository module still kicks off apt-key somewhere and apt-key really doesn’t want to be run from a script. My play for this step looks like:

- name: Add Ansible repo
  apt_repository: repo="ppa:ansible/ansible" state=present update_cache=yes
  environment:
   http_proxy: http://192.168.1.1:3128
   https_proxy: http://192.168.1.1:3128
   APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE: DontWarn

Note that you have to set both the http_proxy AND https_proxy variables.

Step 3 – Install Ansible

Now we can fall back to standard APT configurations! Create the file /etc/apt/apt.conf.d/10proxy.conf  and set its contents to be:

Acquire {
 HTTP::proxy "http://192.168.1.1:3128";
 HTTPS::proxy "http://192.168.1.1:3128";
}

Then in your Ansible play you can just say:

- name: Add Ansible
  apt:
   name: ansible
   state: present

Or, if you’re installing Gitlab’s Runner package:

- name: Install gitlab runner
  apt:
   name: gitlab-runner
   state: present

That was way more convoluted that it needed to be.

Appendix 1 – Proxy Variables

You will have noticed we replicated the proxy settings in a couple of places. Being the good programmers that we are, we see that this is a bad thing and we want to place these in a variable.

There are several places we can define the a variable. In my simple case, I want it accessible to all hosts, so I put it in the group_var/all.yaml file:

proxy_env:
  no_proxy: example.com
  http_proxy: "http://192.168.1.1:3128"
  https_proxy: "http://192.168.1.1:3128"

Then in our roles, we can just put:

 environment:
  - "{{ proxy_env }}"

And if we need to add any extra variables, we just add them to the bottom of the list:

environment:
 - "{{ proxy_env }}"
 - APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE: DontWarn

Appendix  2 – APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE

In my playing with these issues, I’ve discovered something I don’t understand. For some APT sources, I have to supply the APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE environment variable, yet for others, I don’t.

e.g. To install Ansible, I have to add the variable, but for Gitlab’s runner, I don’t.

I’ll leave it as an exercise for the reader to work out what’s going on.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s