在Ansible Playbook中解压Yaml值

安全敏捷

我刚加入电晕时正处于专业领域,因为我从工作中就开始参与理解和编写Ansible代码,在某种程度上,我通过观察SO帖子来了解各种壁虱和技巧,从而在某种程度上成长。

我有以下有趣的剧本。

1-一个custom_pkgs.yml基本上使用yum命令安装一些自定义构建软件包的地方,它调用一个pkgs.yml列出要安装软件包文件。

我有点理解代码,但是

$ vi custom_pkgs.yml
---
- name: Install License
  hosts: all
  become: yes
  become_user: root
  become_method: sudo
  tasks:
    - name: Include the variables to install the license software
      include_vars:
        file: "vars/pkg.yml"
        name: license

    - name: Install license software
      shell: "yum install -y {{ license[ item ] | join(' ') }}"
      with_items: "{{ license }}"
      changed_when: True
      when: item != "remove"

    - name: Remove any unwanted RPMS
      shell: "yum remove -y {{ license.remove | join(' ') }}"
      changed_when: True
      when: license.remove is defined
...

以下是pkg.yml

$ cat pkg.yml
---
license:
 - fenixlmd.noarch
 - tmpwatch
 - xorg-x11-deprecated-libs.i386
 - Tasking.noarch
 - rotate_fix.noarch
 - plexim.noarch
 - interrad.noarch
 - idsd.noarch
 - gsi.noarch
 - java-1.8.0-openjdk
 - java-1.8.0-openjdk-devel
 - java-1.8.0-openjdk-debug
 - flexnet_agent
 - magillem.noarch
 - redhat-lsb-printing
 - redhat-lsb-printing.i686
 - redhat-lsb-core
 - redhat-lsb-core.i686
 - redhat-lsb
 - redhat-lsb.i686
 - git
 - gcc
 - python-devel
...

我想知道的是:

我想了解以下两行。

shell: "yum install -y {{ license[ item ] | join(' ') }}"when: item != "remove"

我已经讲完了在SO中提出问题的所有基本知识,以防万一我提出了一些不希望提出的问题,因为这是我的第一篇文章。

问候 ..

伊德里斯·埃里根

感谢您学习Ansible,这是一个古老的Ansible代码。pkg.yml文件定义了一个名为license的列表变量,其中包含要使用Ansible Shell模块安装在远程主机上的yum软件包列表with_items: "{{ license }}"告诉模块迭代该可变项。当条件移除时,帮助跳过该包阅读Ansible条件文档:https ://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html#the-when-statement

但正如我所说的,这是一个旧代码。在最新版本的Ansible上,您将使用yum模块来安装yum软件包(创建两个软件包列表:package_to_installpackage_to_remove


- name: Install License
  hosts: all
  become: yes
  become_user: root
  become_method: sudo
  tasks:
    - name: Include the variables to install the license software
      include_vars:
        file: "vars/pkg.yml"
        name: license

    - name: Install license software
      yum:
        name: "{{ package_to_install }}"
        changed_when: True
      when: item != "remove"

    - name: Remove any unwanted RPMS
      yum:
        name: "{{ package_to_remove }}"
        state: absent
      changed_when: True

确保使用最新的Ansible版本(2.9)。

阅读yum模块文档:https : //docs.ansible.com/ansible/latest/modules/yum_module.html

您还可以仅使用两个字段定义一个包变量列表:

  license:
    - { name: fenixlmd.noarch, state: present }
    - { name: tmpwatch, state: absent }
    - { name: xorg-x11-deprecated-libs.i386, state: present }
    .
    .
    .

然后像这样使用yum模块:

- name: Install License
  hosts: all
  become: yes
  become_user: root
  become_method: sudo
  tasks:
    - name: Include the variables to install the license software
      include_vars:
        file: "vars/pkg.yml"
        name: license
    - name: Remove any unwanted or install needs package RPMS
      yum:
        name: "{{ item.name }}"
        state: "{{ item.state }}"
      with_items: "{{ licence }}"

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章