Ansible任务安装包异常:指定php70u却安装php71u问题排查
Alright, let’s dig into why Ansible is installing php71u instead of the php70u you specified—especially since manual yum works perfectly. Here are the most likely culprits and step-by-step checks to fix this:
1. Check for Variable Overrides
First, make sure your Ansible task isn’t accidentally using a variable that’s set to 71 instead of 70. Sometimes group_vars, host_vars, or playbook-level variables can overwrite the explicit package name you set.
For example, if your task looks like this:
- name: Install PHP yum: name: php{{ php_version }}u state: present become: yes
Double-check that php_version is actually set to 70 (not 71) in your variables. To rule this out entirely, hardcode the package name temporarily:
- name: Install PHP 7.0 explicitly yum: name: php70u state: present update_cache: yes become: yes
2. Verify Yum Metadata & Cache
Ansible’s yum module might be relying on outdated cached metadata, which could be pointing to the wrong version. Even if you manually ran yum install php70u successfully, Ansible might still be using old cache.
- Add
update_cache: yesto your yum task (as shown above) to force a metadata refresh. - If using the command module, explicitly clean the cache first:
- name: Clean yum cache before install command: yum clean all become: yes - name: Install PHP 7.0 via command command: yum install php70u -y args: warn: false become: yes - You can also clear Ansible’s local cache by deleting the
~/.ansible/cachedirectory on your control node.
3. Inspect Available Packages in Ansible’s Context
It’s possible that when Ansible runs yum, it’s seeing different package options than when you run it manually. Let’s debug what packages Ansible sees:
Add these tasks to your playbook to list available PHP versions:
- name: List available PHP packages command: yum list php70u php71u --showduplicates register: php_package_list become: yes - name: Print package details debug: var: php_package_list.stdout_lines
Run the playbook and compare this output to what you get when running yum list php70u php71u --showduplicates manually. Look for:
- Is php70u actually available in the repos Ansible is using?
- Is php71u marked as the "default" or highest priority version?
4. Check Repository Priorities
Even if the IUS repo is deployed, another repo (like EPEL or a custom repo) might have a higher priority, causing yum to pick php71u instead of php70u.
- Check your repo configuration files in
/etc/yum.repos.d/forprioritysettings. Lower numbers mean higher priority. Ensure the IUS repo has a higher priority (lower number) than other repos that carry PHP packages. - If you don’t have priorities set, install
yum-plugin-prioritiesand addpriority=1to the[ius]section of/etc/yum.repos.d/ius.repo, then clean the cache and retry the install.
5. Verify Ansible Execution Environment
Sometimes differences in the environment when Ansible runs (vs. your manual shell) can cause issues:
- Ensure Ansible is running with full root privileges. Add
become: yesto your tasks, or explicitly setbecome_user: root. - Check if there are any environment variables affecting yum. Add a task to print environment variables and compare to your manual shell:
Look for variables like- name: Print environment variables command: env register: ansible_env become: yes - name: Show environment debug: var: ansible_env.stdout_linesYUM_CONFIGthat might be pointing to a non-standard config file.
6. Rule Out Ansible Module Bugs
Older versions of the Ansible yum module had occasional bugs with package name resolution. Try updating Ansible to the latest stable version, or switch to the dnf module if your system supports it (RHEL/CentOS 8+):
- name: Install PHP 7.0 with dnf dnf: name: php70u state: present update_cache: yes become: yes
内容的提问来源于stack exchange,提问作者TechFanDan

