Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ansible major version extraction #13516

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

reimarstier
Copy link

@reimarstier reimarstier commented Oct 25, 2024

While working on a test environment created with vagrant 2.4.1 and the ansible local provisioner I noticed that my provisioning was no longer working since ansible version 10. See this issue for details.

Error encountered

vagrant up --provision --debug

vagrant version 2.4.1 detects ansible version 10.5.0 and then chooses ansible compatibility mode with version 1.8 for some reason.

 INFO ssh: Execute: python3 -c "import importlib.metadata; print('ansible ' + importlib.metadata.version('ansible'))" (sudo=false)       
DEBUG ssh: stderr: 41e57d38-b4f7-4e46-9c38-13873d338b86-vagrant-ssh                                                                      
DEBUG ssh: Exit status: 0                                                                                                                
 INFO ansible_guest: Vagrant has automatically selected the compatibility mode '1.8'                                                     
according to the Ansible version installed (10.5.0).                                                                                     

Ansible then adds the no longer existing sudo flag and fails:

INFO ssh: Execute: cd /vagrant && PYTHONUNBUFFERED=1 ANSIBLE_FORCE_COLOR=true ANSIBLE_ROLES_PATH='/vagrant/.ci/docker/vagrant/local_roles/:
/vagrant/.ci/docker/vagrant/downloaded_roles' ansible-playbook --limit="opendut-vm" --inventory-file=/tmp/vagrant-ansible/inventory --extra-
vars=\{\"opendut_repo_root\":\"/vagrant\"\} --sudo --tags=all --skip-tags=desktop,firefox /vagrant/.ci/docker/vagrant/playbook.yml (sudo=fal
se)                                                                                                                                         
DEBUG ssh: stderr: 41e57d38-b4f7-4e46-9c38-13873d338b86-vagrant-ssh                                                                         
[...]
ansible-playbook: error: unrecognized arguments: --sudo

Root cause

Curious to the cause of this behavior I found a method that is extracting the ansible major version from a string. Now the regular expression apparently captures only the last digit of the first number before a dot. This is not exactly the intended behavior.

  • The following snippet resolves version 0 but it should resolve version 10
    version_string="ansible 10.5.0"
    ansible_version_pattern = version_string.match(/(^ansible\s+)(.+)$/)
    _, @gathered_version, _ = ansible_version_pattern.captures
    @gathered_version.match(/(\d)\..+$/).captures[0].to_i
    

This subsequently leads to vagrant dropping to a compatibility mode that fails the ansible playbook execution.

Since this is such a minor code change I thought a quick fix is highly appreciated. Looking forward to your thoughts and comments on this.

  • Alternative without regex
@gathered_version.split(".")[0].to_i

Copy link

hashicorp-cla-app bot commented Oct 25, 2024

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant