From 9f5ddb8a90c1688189e83302c8eec5798f859a2e Mon Sep 17 00:00:00 2001 From: Dan Mick Date: Tue, 30 Sep 2025 19:20:44 -0700 Subject: [PATCH] builder.yml: update to use virtualenv pip/python no longer like to install modules in global dirs. Create a virtualenv and use it when running the jenkins_node module. This is done by adding a second play to start that creates the virtualenv and sets ansible_python_interpreter to the interpreter in the venv. That means it affects the following play in the playbook, which is the original playbook. So technically the virtualenv python is used for all plays. Also, remove the #!/usr/bin/python3 from library/jenkins_node, which was doing no good, but caused havoc by execing the non-venv python when running the module. Signed-off-by: Dan Mick --- ansible/examples/builder.yml | 65 +++++++++++++++--------------------- ansible/library/jenkins_node | 1 - 2 files changed, 27 insertions(+), 39 deletions(-) diff --git a/ansible/examples/builder.yml b/ansible/examples/builder.yml index ac684594..d422ffa1 100644 --- a/ansible/examples/builder.yml +++ b/ansible/examples/builder.yml @@ -12,6 +12,33 @@ ## secrets files jenkins.ceph.com.apitoken.yml and 2.jenkins.ceph.com.apitoken.yml must ## exist in ANSIBLE_SECRETS_PATH +- hosts: all + vars: + venv: "/home/{{ ansible_user }}/.venv" + tasks: + - name: "clean up any existing {{ venv }}" + ansible.builtin.file: + path: "{{ venv }}" + state: absent + tags: always + + - name: Create "{{ venv }}" + ansible.builtin.command: "python3 -mvenv {{ venv }}" + args: + creates: "{{ venv }}/bin/python" + tags: always + + - name: Install python-jenkins + ansible.builtin.command: + cmd: "{{ venv }}/bin/pip install python-jenkins" + become_user: "{{ ansible_user }}" + tags: always + + - name: switch python interpreters + set_fact: + ansible_python_interpreter: "{{ venv }}/bin/python" + tags: always + - hosts: all become: true user: ubuntu # This should be overridden on the CLI (e.g., -e user=centos). It doesn't matter on a mita/prado builder because the playbook is run locally by root. @@ -888,44 +915,6 @@ path: '/etc/ssh/ssh_known_hosts' key: "{{ github_host_key }}" - ## PIP TASKS - - set_fact: - pip_version: pip - ansible_python_interpreter: /usr/bin/python - when: (ansible_os_family == "RedHat" and ansible_distribution_major_version|int <= 7) or - (ansible_os_family == "Debian" and ansible_distribution_major_version|int <= 16) - tags: - - register - - interpreter - - - set_fact: - pip_version: pip3 - ansible_python_interpreter: /usr/bin/python3 - when: (ansible_os_family == "RedHat" and ansible_distribution_major_version|int >= 7) or - (ansible_os_family == "Debian" and ansible_distribution_major_version|int >= 18) or - ansible_os_family == "Suse" - tags: - - register - - interpreter - - - name: Install six, latest one - pip: - name: six - state: latest - executable: "{{ pip_version }}" - when: ansible_os_family != "Suse" - tags: - - register - - - name: Install python-jenkins - # https://review.openstack.org/460363 - pip: - name: python-jenkins - version: 0.4.15 - executable: "{{ pip_version }}" - tags: - - register - ## LIBVIRT SERVICE TASKS - name: start libvirt services service: diff --git a/ansible/library/jenkins_node b/ansible/library/jenkins_node index 617daa24..af5639ea 100644 --- a/ansible/library/jenkins_node +++ b/ansible/library/jenkins_node @@ -1,4 +1,3 @@ -#!/usr/bin/python3 # -*- coding: utf-8 -*- DOCUMENTATION = """ -- 2.39.5