Ansible yum:无法正确循环本地文件的项目

问题描述 投票:0回答:3

执行此类任务以安装本地软件包时(ala yum localinstall):

- name: Install local packages
  yum:
    name="/my/path/{{ item }}"
    state=installed
  with_items: list_of_packages

list_of_packages 在 vars/main.yml 中定义为:

---
list_of_packages:
  - package1.rpm
  - package2.rpm
  ...    

Yum 尝试变得聪明,将所有项目放入一个命令中,但最终仅添加第一个包的路径:

REMOTE_MODULE yum name="/my/path/package1.rpm,package2.rpm" state=已安装

显然找不到第二个包裹

我尝试在里面添加路径,例如:

{{ "/my/path/"+item }}
并继续执行相同的操作。

除了复制“list_of_packages”并添加基本路径之外,还有其他方法吗? (我正在使用的解决方法)

编辑:详细的测试手册(ansible版本1.9.2):

# cat playbooks/testlocalinstall.yml
---
- hosts: all:!localhost

  vars:

    list_of_packages:
      - oracleasmlib-2.0.4-1.el6.x86_64.rpm
      - oracleasm-support-2.1.8-1.el6.x86_64.rpm


  tasks:

    - name: Install Packages
      yum: name=/software/{{ item }} state=installed
      with_items: list_of_packages

结果:

# ansible-playbook playbooks/testlocalinstall.yml -i inventory/myinv -vvv

PLAY [all:!localhost] *********************************************************

GATHERING FACTS ***************************************************************
<testmachine> ESTABLISH CONNECTION FOR USER: root on PORT 22 TO testmachine
<testmachine> REMOTE_MODULE setup
<testmachine> EXEC /bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1441783338.15-74757921814888 && echo $HOME/.ansible/tmp/ansible-tmp-1441783338.15-74757921814888'
<testmachine> PUT /tmp/tmp_955vP TO /root/.ansible/tmp/ansible-tmp-1441783338.15-74757921814888/setup
<testmachine> EXEC /bin/sh -c 'LANG=en_US.UTF-8 LC_CTYPE=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1441783338.15-74757921814888/setup; rm -rf /root/.ansible/tmp/ansible-tmp-1441783338.15-74757921814888/ >/dev/null 2>&1'
ok: [testmachine]

TASK: [Install Packages] ******************************************************
<testmachine> ESTABLISH CONNECTION FOR USER: root on PORT 22 TO testmachine
<testmachine> REMOTE_MODULE yum name=/software/oracleasmlib-2.0.4-1.el6.x86_64.rpm,oracleasm-support-2.1.8-1.el6.x86_64.rpm state=installed
<testmachine> EXEC /bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1441783339.35-66981904309720 && echo $HOME/.ansible/tmp/ansible-tmp-1441783339.35-66981904309720'
<testmachine> PUT /tmp/tmpF0hiqP TO /root/.ansible/tmp/ansible-tmp-1441783339.35-66981904309720/yum
<testmachine> EXEC /bin/sh -c 'LANG=en_US.UTF-8 LC_CTYPE=en_US.UTF-8 /usr/bin/python -tt /root/.ansible/tmp/ansible-tmp-1441783339.35-66981904309720/yum; rm -rf /root/.ansible/tmp/ansible-tmp-1441783339.35-66981904309720/ >/dev/null 2>&1'
failed: [testmachine] => (item=oracleasmlib-2.0.4-1.el6.x86_64.rpm,oracleasm-support-2.1.8-1.el6.x86_64.rpm) => {"changed": false, "failed": true, "item": "oracleasmlib-2.0.4-1.el6.x86_64.rpm,oracleasm-support-2.1.8-1.el6.x86_64.rpm", "rc": 0, "results": []}
msg: No Package file matching 'oracleasm-support-2.1.8-1.el6.x86_64.rpm' found on system

msg:
No Package file matching 'oracleasm-support-2.1.8-1.el6.x86_64.rpm' found on system

FATAL: all hosts have already failed -- aborting

PLAY RECAP ********************************************************************
           to retry, use: --limit @/root/testlocalinstall.retry

testmachine          : ok=1    changed=0    unreachable=0    failed=1

添加列表中每个包的路径效果很好

谢谢。

installation ansible yum
3个回答
1
投票

我无法重现此问题:

  • 使用
    --downloadonly
    yum 选项下载 3 个软件包(link
  • Ansible 详细模式显示相同的结果 - 只有第一个包具有路径前缀,但安装有效。

请尝试这个:

  1. 检查 Ansible 版本 - 1.9.2 之前的版本(我正在测试的版本)中可能存在错误

  2. Yum 找不到您提供的软件包列表的依赖项 - 确保使用

    yum --downloadonly
    下载的所有软件包都在列表中。它们应该与目标包一起明确说明。

  3. 作为解决方法,您可以使用模板模块创建本地 yum 存储库,该模块指向同一文件夹

    /my/path/
    。默认情况下应禁用此存储库。仅在
    yum
    任务期间使用
    enablerepo
    ( link )

  4. 启用此存储库

这是我的剧本:

---
- hosts: all

  vars:

    list_of_packages:
      - nc-1.84-24.el6.x86_64.rpm
      - telnet-0.17-48.el6.x86_64.rpm
      - screen-4.0.3-18.el6.x86_64.rpm


  tasks:

    - name: Install Packages
      yum: name=/vagrant/{{ item }} state=installed
      with_items: list_of_packages

输出:

vagrant@localhost vagrant]$ sudo ansible-playbook -vvv -c local -i "localhost," test.yml

PLAY [all] ********************************************************************

GATHERING FACTS ***************************************************************
<localhost> REMOTE_MODULE setup
<localhost> EXEC ['/bin/sh', '-c', 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1441656987.66-146848178907207 && echo $HOME/.ansible/tmp/ansible-tmp-1441656987.66-146848178907207']
<localhost> PUT /tmp/tmp7TIwxU TO /root/.ansible/tmp/ansible-tmp-1441656987.66-146848178907207/setup
<localhost> EXEC ['/bin/sh', '-c', u'LANG=C LC_CTYPE=C /usr/bin/python /root/.ansible/tmp/ansible-tmp-1441656987.66-146848178907207/setup; rm -rf /root/.ansible/tmp/ansible-    tmp-1441656987.66-146848178907207/ >/dev/null 2>&1']
ok: [localhost]

TASK: [Install Packages] ******************************************************
<localhost> REMOTE_MODULE yum name=/vagrant/nc-1.84-24.el6.x86_64.rpm,telnet-0.17-48.el6.x86_64.rpm,screen-4.0.3-18.el6.x86_64.rpm,lftp-4.0.9-6.el6.x86_64.rpm state=installed
<localhost> EXEC ['/bin/sh', '-c', 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1441656987.78-72871194424865 && echo $HOME/.ansible/tmp/ansible-tmp-1441656987.78-72871194424865']
<localhost> PUT /tmp/tmpTy7Al1 TO /root/.ansible/tmp/ansible-tmp-1441656987.78-72871194424865/yum
<localhost> EXEC ['/bin/sh', '-c', u'LANG=C LC_CTYPE=C /usr/bin/python -tt /root/.ansible/tmp/ansible-tmp-1441656987.78-72871194424865/yum; rm -rf /root/.ansible/tmp/ansible-    tmp-1441656987.78-72871194424865/ >/dev/null 2>&1']
changed: [localhost] => (item=nc-1.84-24.el6.x86_64.rpm,telnet-0.17-48.el6.x86_64.rpm,screen-4.0.3-18.el6.x86_64.rpm,lftp-4.0.9-6.el6.x86_64.rpm) => {"changed": true, "item": "nc-1.84-24.el6.x86_64.rpm,    telnet-0.17-48.el6.x86_64.rpm,screen-4.0.3-18.el6.x86_64.rpm,lftp-4.0.9-6.el6.x86_64.rpm", "msg": "", "rc": 0, "results": ["Loaded plugins: fastestmirror\nSetting up Install Process\nExamining /vagrant/    nc-1.84-24.el6.x86_64.rpm: nc-1.84-24.el6.x86_64\nMarking /vagrant/nc-1.84-24.el6.x86_64.rpm to be installed\nLoading mirror speeds from cached hostfile\n * base: mirror.isoc.org.il\n * epel: mirror.de.    leaseweb.net\n * extras: mirror.isoc.org.il\n * updates: mirror.isoc.org.il\nExamining telnet-0.17-48.el6.x86_64.rpm: 1:telnet-0.17-48.el6.x86_64\nMarking telnet-0.17-48.el6.x86_64.rpm to be     installed\nExamining screen-4.0.3-18.el6.x86_64.rpm: screen-4.0.3-18.el6.x86_64\nMarking screen-4.0.3-18.el6.x86_64.rpm to be installed\nResolving Dependencies\n--> Running transaction check\n--->     Package nc.x86_64 0:1.84-24.el6 will be installed\n---> Package screen.x86_64 0:4.0.3-18.el6 will be installed\n---> Package telnet.x86_64 1:0.17-48.el6 will be installed\n--> Finished Dependency     Resolution\n\nDependencies Resolved\n\n================================================================================\n Package    Arch       Version            Repository                           Size\n================================================================================\nInstalling:\n nc         x86_64     1.84-24.el6        /nc-1.84-24.el6.x86_64          109 k\n screen         x86_64     4.0.3-18.el6       /screen-4.0.3-18.el6.x86_64     795 k\n telnet     x86_64     1:0.17-48.el6      /telnet-0.17-48.el6.x86_64      109 k\n\nTransaction     Summary\n================================================================================\nInstall       3 Package(s)\n\nTotal size: 1.0 M\nInstalled size: 1.0 M\nDownloading Packages:\nRunning     rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r  Installing : nc-1.84-24.el6.x86_64                                        1/3 \n\r  Installing : 1:telnet-    0.17-48.el6.x86_64                                  2/3 \n\r  Installing : screen-4.0.3-18.el6.x86_64                                   3/3 \n\r  Verifying  : screen-4.0.3-18.el6.    x86_64                                   1/3 \n\r  Verifying  : 1:telnet-0.17-48.el6.x86_64                                  2/3 \n\r  Verifying  : nc-1.84-24.el6.    x86_64                                        3/3 \n\nInstalled:\n  nc.x86_64 0:1.84-24.el6                screen.x86_64 0:4.0.3-18.el6           \n  telnet.x86_64 1:0.17-48.el6               \n\nComplete!\n"]}

PLAY RECAP ********************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0

0
投票

为了使用

with_items
我相信它需要一个 yaml 列表,而不是逗号分隔的列表。

 name: install local packages   
 yum: name="/my/path/{{ item }}" state=installed  
 with_items:
      - package1.rpm
      - package2.rpm
      - package3.rpm

http://docs.ansible.com/ansible/playbooks_loops.html#standard-loops


0
投票

Ansible 文档不建议在包管理模块中使用循环(

with_items
也是循环),包括
yum
:

loop:
一起使用时,每个包将被单独处理,将列表直接传递给
name
选项会更有效。

对于最初的问题,模块文档包含以下解释:

在 1.9.2 之前的版本中,此模块单独安装和删除提供给 yum 模块的每个包。当文件名或 url 指定的包必须一起安装或删除时,这会导致问题。在 1.9.2 中,这一问题已得到修复,以便将软件包安装在一个 yum 事务中。但是,如果其中一个软件包添加了其他软件包来自的新 yum 存储库(例如 epel-release),则需要在单独的任务中安装该软件包。这模仿了 yum 的命令行行为。

© www.soinside.com 2019 - 2024. All rights reserved.