From 43650cb966daf6dd059d44d3e7bd0fc1b0dac945 Mon Sep 17 00:00:00 2001 From: Mark Goddard Date: Fri, 21 Aug 2020 16:58:32 +0100 Subject: [PATCH] Performance: refactor pip configuration Currently we perform pip configuration for a list of users, including pip_conf.yml in a loop for each user. It is slightly more efficient to loop over each task, since individual tasks have a higher overhead than loops. This change refactors the code into multiple loops, with a single include. We keep the include to allow skipping the tasks when pip configuration is not required. Change-Id: I698d38613f45bd03a2e51572f6e6fb69e934f614 Story: 2007993 Task: 40719 --- ansible/roles/pip/tasks/main.yml | 6 ++---- ansible/roles/pip/tasks/pip_conf.yml | 21 ++++++++++++--------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/ansible/roles/pip/tasks/main.yml b/ansible/roles/pip/tasks/main.yml index 3bf421d0a..956ba9ff0 100644 --- a/ansible/roles/pip/tasks/main.yml +++ b/ansible/roles/pip/tasks/main.yml @@ -1,6 +1,4 @@ --- -- include_tasks: pip_conf.yml - loop: "{{ pip_applicable_users }}" - loop_control: - loop_var: user +- name: Ensure pip is configured + include_tasks: pip_conf.yml when: (pip_local_mirror | bool) or (pip_proxy | length > 0) diff --git a/ansible/roles/pip/tasks/pip_conf.yml b/ansible/roles/pip/tasks/pip_conf.yml index a9261e0b3..6d473da0e 100644 --- a/ansible/roles/pip/tasks/pip_conf.yml +++ b/ansible/roles/pip/tasks/pip_conf.yml @@ -1,12 +1,13 @@ --- -- name: Create local .pip directory for {{ user }} +- name: Create local .pip directory file: - path: "~{{ user }}/.pip" + path: "~{{ item }}/.pip" state: directory become: True - become_user: "{{ user }}" + become_user: "{{ item }}" + loop: "{{ pip_applicable_users }}" -- name: Create pip.conf for {{ user }} +- name: Create pip.conf copy: content: | [global] @@ -22,16 +23,18 @@ {% if pip_proxy | length > 0 -%} proxy = {{ pip_proxy }} {% endif -%} - dest: "~{{ user}}/.pip/pip.conf" + dest: "~{{ item }}/.pip/pip.conf" become: True - become_user: "{{ user }}" + become_user: "{{ item }}" + loop: "{{ pip_applicable_users }}" -- name: Create .pydistutils.cfg for {{ user }} +- name: Create .pydistutils.cfg copy: content: | [easy_install] index-url = {{ pip_index_url }} - dest: "~{{ user}}/.pydistutils.cfg" + dest: "~{{ item }}/.pydistutils.cfg" when: pip_index_url | length > 0 become: True - become_user: "{{ user }}" + become_user: "{{ item }}" + loop: "{{ pip_applicable_users }}"