ansible 2.4如果已使用“ with_item”循环和“ when”条件安装,则如何跳过apt软件包的安装

时间:2019-01-23 15:47:22

标签: loops ansible

i would like please to know if there is a way in a loop with_item.

检查并跳过apt依赖项的安装 如果已经安装了“何时”条件

if im adding the "when" condition in the end of the with_item list , its look like the condition check all the list instead of checking just the relevant - in this example python2 

- name: check if python already installed
  shell: dpkg-query -W python2.7
  register: check_python2
  ignore_errors: True


- name: Install apt dependencies
  apt:
    name: "{{item.name}}{{item.version}}"
    state: present
    allow_unauthenticated: yes
    force: yes
  with_items:
    - { name: 'python2.7', version: '' }
    - { name: 'ruby', version: '' }
    - { name: 'postgresql-9.5', version: '' }
    - { name: 'postgresql-contrib-9.5', version: '' }
    - { name: 'libpq-dev', version: '' }
    - { name: 'nodejs', version: '=9.*' }
    - { name: 'python-setuptools', version: '' }
    - { name: 'python-pip', version: '' }
    - { name: 'python-pkg-resources', version: '' }
    - { name: 'sshpass', version: '' }
    - { name: 'zip', version: '' }
    - { name: 'mongodb-org', version: '=4.0.0' }
    - { name: 'libfontconfig', version: '' }
    - { name: 'ntp', version: '' }
    - { name: 'fio', version: '' }
  when: check_python2.rc != 0
  when: check_ruby.rc != 0

如何添加“何时”条件以仅检查正确的依赖项

我想检查所有依赖项:

如果未安装其中之一,请安装,否则跳过

2 个答案:

答案 0 :(得分:0)

我不确定我是否理解问题,您是说两个条件“ check_python2.rc!= 0”和“ check_ruby.rc!= 0”吗?

when: check_python2.rc != 0 and check_ruby.rc != 0

答案 1 :(得分:0)

- hosts: all:!
  gather_facts: False
  vars:
    packages:
      - python2.7
      - ruby
      - postgresql-9.5
      - postgresql-contrib-9.5
      - libpq-dev
      - nodejs
      - python-setuptools
      - python-pip
      - python-pkg-resources
      - sshpass
      - zip
      - mongodb-org=4.0.0    
      - libfontconfig
      - ntp
      - fio

  tasks:
    - name: "Install dependencies"
      become: yes
      allow_unauthenticated: yes
      force: yes
      apt:
        pkg: "{{ packages }}"
        state: present