You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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: yes to 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/cache directory 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/ for priority settings. 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-priorities and add priority=1 to 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: yes to your tasks, or explicitly set become_user: root.
  • Check if there are any environment variables affecting yum. Add a task to print environment variables and compare to your manual shell:
    - name: Print environment variables
      command: env
      register: ansible_env
      become: yes
    
    - name: Show environment
      debug:
        var: ansible_env.stdout_lines
    
    Look for variables like YUM_CONFIG that 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:32:12