通过Ansible在CentOS(节点)上安装多个yum软件包?

7
这是我正在尝试在运行操作系统Centos-7的节点上运行的YAML代码部分......文件层次结构如下: --> roles/install_tools/tasks/main.yml 它不能正常工作,但YAML语法也是有效和正确的! 有人可以帮助我吗?
---
- name: install the Development tools package group
  yum:
   name: "@Development tools"
   state: present

- name: Install common software requirements
  ignore_errors: yes
  become: true
  yum: pkg={{ item }} state=present
  with_items:
    - yum-plugin-fastestmirror
    - epel-release
    - git
    - libyaml-devel 
    - libnet-devel 
    - libnetfilter_queue-devel 
    - libpcap-devel 
    - pcre-devel 
    - file-devel 
    - jansson-devel 
    - nss-devel 
    - libcap-ng-devel 
    - lua-devel 
    - binutils 
    - gmp 
    - gmp-devel 
    - make 
    - ld 
    - glibc.i686 
    - python-pip 
    - perl-Sys-Syslog
    - readline-devel 
    - ncurses-devel 
    - openssl-devel 
    - easy-rsa
    - flex 
    - bison 
    - pcre 
    - zlib 
    - zlib-devel 
    - libpcap 
    - libdnet 
    - libdnet-devel 
    - m4
    - gcc  
    - mysql-devel 
    - python-devel 
    - geoip 
    - geoip-devel 
    - libffi-devel 
    - vim 
    - lsof 
    - wget 
    - mlocate 
    - htop 
    - net-tools 
    - traceroute
    - tcpdump 
    - radiusclient-ng.x86_64 
    - gmp-devel 
    - iptables-services 
    - dnsmasq 
    - pptpd 
    - mariadb-devel
    - lzo-devel.x86_64
    - crontabs
    - gcc 
    - make 
    - rpm-build 
    - autoconf.noarch 
    - zlib-devel 
    - pam-devel 
    - openssl-devel

它显示的错误是:
TASK [install_tools : Install common software requirements] ***********************
failed: [meracentos] (item=[u'yum-plugin-fastestmirror', u'epel-release', u'git', u'libyaml-devel', u'libnet-devel', u'libnetfilter_queue-devel', u'libpcap-devel', u'pcre-devel', u'file-devel', u'jansson-devel', u'nss-devel', u'libcap-ng-devel', u'lua-devel', u'binutils', u'gmp', u'gmp-devel', u'make', u'ld', u'glibc.i686', u'python-pip', u'perl-Sys-Syslog', u'readline-devel', u'ncurses-devel', u'openssl-devel', u'easy-rsa', u'flex', u'bison', u'pcre', u'zlib', u'zlib-devel', u'libpcap', u'libdnet', u'libdnet-devel', u'm4', u'gcc', u'mysql-devel', u'python-devel', u'geoip', u'geoip-devel', u'libffi-devel', u'vim', u'lsof', u'wget', u'mlocate', u'htop', u'net-tools', u'traceroute', u'tcpdump', u'radiusclient-ng.x86_64', u'gmp-devel', u'iptables-services', u'dnsmasq', u'pptpd', u'mariadb-devel', u'lzo-devel.x86_64', u'crontabs', u'gcc', u'make', u'rpm-build', u'autoconf.noarch', u'zlib-devel', u'pam-devel', u'openssl-devel']) => {"changed": false, "failed": true, "item": ["yum-plugin-fastestmirror", "epel-release", "git", "libyaml-devel", "libnet-devel", "libnetfilter_queue-devel", "libpcap-devel", "pcre-devel", "file-devel", "jansson-devel", "nss-devel", "libcap-ng-devel", "lua-devel", "binutils", "gmp", "gmp-devel", "make", "ld", "glibc.i686", "python-pip", "perl-Sys-Syslog", "readline-devel", "ncurses-devel", "openssl-devel", "easy-rsa", "flex", "bison", "pcre", "zlib", "zlib-devel", "libpcap", "libdnet", "libdnet-devel", "m4", "gcc", "mysql-devel", "python-devel", "geoip", "geoip-devel", "libffi-devel", "vim", "lsof", "wget", "mlocate", "htop", "net-tools", "traceroute", "tcpdump", "radiusclient-ng.x86_64", "gmp-devel", "iptables-services", "dnsmasq", "pptpd", "mariadb-devel", "lzo-devel.x86_64", "crontabs", "gcc", "make", "rpm-build", "autoconf.noarch", "zlib-devel", "pam-devel", "openssl-devel"], "msg": "No package matching 'ld' found available, installed or updated", "rc": 126, "results": ["yum-plugin-fastestmirror-1.1.31-40.el7.noarch providing yum-plugin-fastestmirror is already installed", "git-1.8.3.1-6.el7_2.1.x86_64 providing git is already installed", "binutils-2.25.1-22.base.el7.x86_64 providing binutils is already installed", "gmp-1:6.0.0-12.el7_1.x86_64 providing gmp is already installed", "make-1:3.82-23.el7.x86_64 providing make is already installed", "No package matching 'ld' found available, installed or updated"]}

这里有一个错误信息:找不到匹配 'ld' 的可用软件包 - Konstantin Suvorov
好吧,我也已经删除了“ld”并进行了多次检查,但它不起作用!我无法弄清楚为什么会发生这种情况,我之前在UBUNTU中使用过这种策略,并且对我来说效果很好!但是对于CentOS,它会出现这样的错误。 - Saad
@SyedSaadAhmed 目标机器上启用了正确的存储库吗? - PhilipGough
我不明白你说的“启用存储库”是什么意思?@user2983542 - Saad
我已经解决了这个问题,原因是ansible版本的问题! 一些旧版本不支持新的软件包,我正在运行一个较旧的版本,所以我才会遇到这个问题! 谢谢 :-) - Saad
4个回答

18

with_items 在使用 ansible yum 时已经过时。您应该按以下方式将列表传递给 name:

- name: Install common software requirements
  become: true
  yum:
    state: present
    name: 
      - yum-plugin-fastestmirror
      - epel-release
      - git
      - libyaml-devel 
      - libnet-devel 
      - libnetfilter_queue-devel 
      - libpcap-devel 
      - pcre-devel 
      - file-devel 
      - jansson-devel 
      - nss-devel 
      - libcap-ng-devel 
      - lua-devel 
      - binutils 
      - gmp 
      - gmp-devel 
      - make 
      - ld 
      - glibc.i686 
      - python-pip 
      - perl-Sys-Syslog
      - readline-devel 
      - ncurses-devel 
      - openssl-devel 
      - easy-rsa

之前的解决方案会给你以下错误:

Invoking "yum" only once while using a loop via squash_actions is  deprecated. Instead of using a loop to supply multiple items and specifying `name: {{ item }}`, 

1

你的配置文件中有两个错误。

  1. yum 语法错误
  2. with_items 已经过时了。

示例:

- name: Install Tools
  yum:
   state: present
   name:
    - telnet
    - htop

1
你可以使用简单的列表:

    - name: Ensure useful util libs are present
      yum:
        name: ['httpie', 'the_silver_searcher', 'fasd', 'multitail', 'vim']
        state: present 
      become: True

或者作为YAML列表
    - name: Ensure useful util libs are present
      yum:
        name:
          - httpie
          - the_silver_searcher
          - fasd
          - multitail
          - vim
        state: present 
      become: True

1
以下代码对我有效 ->

文件 - yumInstall.yml

---
- hosts: localhost
  gather_facts: False
  serial: 1
  tasks:
    - name: 1. Install Apache Packages
      yum: name={{ item }} state=present
      with_items:
          - httpd
          - yum-plugin-fastestmirror
          - epel-release
          - git
          - libyaml-devel
          - libnet-devel
          - libnetfilter_queue-devel
          - libpcap-devel
          - pcre-devel
          - file-devel
          - jansson-devel
          - nss-devel
          - libcap-ng-devel
          - lua-devel
          - binutils
          - gmp
          - gmp-devel
          - make
#          - ld
          - glibc.i686
          - python-pip
          - perl-Sys-Syslog
          - readline-devel
          - ncurses-devel
          - openssl-devel
          - easy-rsa
          - flex
          - bison
          - pcre
          - zlib
          - zlib-devel

执行playbook:-

ansible-playbook -i hosts yumInstall.yml

[警告]: 发现分组和主机具有相同的名称: localhost

PLAY [localhost] *****************************************************************************************************************************************************************

TASK [1. 安装Apache软件包] ************************************************************************************************************************************************ changed: [localhost] => (item=[u'httpd', u'yum-plugin-fastestmirror', u'epel-release', u'git', u'libyaml-devel', u'libnet-devel', u'libnetfilter_queue-devel', u'libpcap-devel', u'pcre-devel', u'file-devel', u'jansson-devel', u'nss-devel', u'libcap-ng-devel', u'lua-devel', u'binutils', u'gmp', u'gmp-devel', u'make', u'glibc.i686', u'python-pip', u'perl-Sys-Syslog', u'readline-devel', u'ncurses-devel', u'openssl-devel', u'easy-rsa', u'flex', u'bison', u'pcre', u'zlib', u'zlib-devel'])

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

Screenshot


实际上,我正在使用ansible 2.2.0.0版本! 在测试了几台机器后,我发现我的代码只能在ansible 2.3.0.0版本上正常工作,所以我只是升级了版本,现在代码可以执行。 在ansible 2.2.0.0版本中,“yum”模块一定存在缺陷! - Saad
可能是,我正在使用ansible 2.3.0.0版本。 - SPM
是的,你可以自己验证! 升级到ansible版本2.2.0.0,你的YAML模板会出现错误。 我已经测试过了。 - Saad
with_items已被弃用,请使用名称列表。 - 9nix00

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接