web-dev-qa-db-ja.com

Aptパッケージが見つからない場合のAnsibleDoタスク

特定のaptパッケージが見つからない場合は、一連のタスクを実行しようとしています。

例えば:

グラファイトカーボンが取り付けられていない場合は、次のようにします。

- apt: name=debconf-utils state=present
- Shell: echo 'graphite-carbon/postrm_remove_databases boolean false' | debconf-set-selections
- apt: name=debconf-utils state=absent

もう一つの例:

statsdがインストールされていない場合は、次のようにします。

- file: path=/tmp/build state=directory
- Shell: cd /tmp/build ; git clone https://github.com/etsy/statsd.git ; cd statsd ; dpkg-buildpackage 
- Shell: dpkg -i /tmp/build/statsd*.deb

どうすればこれをクラックし始めることができますか?

-Shell: dpkg -l|grep <package name>を実行して、何らかの方法でリターンコードをキャプチャできるのではないかと考えています。

11
Simply Seth

私のソリューションは機能しているようです。

これは私がそれをどのように機能させているかの例です:

- Shell: dpkg-query -W 'statsd'
  ignore_errors: True
  register: is_statd

- name: create build dir
  file: path=/tmp/build state=directory
  when: is_statd|failed

- name: install dev packages for statd build
  apt:  name={{ item }} 
  with_items: 
    - git 
    - devscripts 
    - debhelper
  when: is_statd|failed

- Shell: cd /tmp/build ; git clone https://github.com/etsy/statsd.git ; cd statsd ; dpkg-buildpackage 
  when: is_statd|failed

....

別の例を次に示します。

 - name: test if create_superuser.sh exists
  stat: path=/tmp/create_superuser.sh 
  ignore_errors: True
  register: f

- name: create graphite superuser
  command: /tmp/create_superuser.sh
  when: f.stat.exists == True

...そしてもう1つ

- stat: path=/tmp/build
  ignore_errors: True
  register: build_dir

- name: destroy build dir
  Shell: rm -fvR /tmp/build 
  when: build_dir.stat.isdir is defined and build_dir.stat.isdir
6
Simply Seth

package_facts モジュールを使用できます(Ansible 2.5が必要です):

- name: Gather package facts
  package_facts:
    manager: apt

- name: Install debconf-utils if graphite-carbon is absent
  apt:
    name: debconf-utils
    state: present
  when: '"graphite-carbon" not in ansible_facts.packages'

...
2
sduthil

dpkg | grepは正しい方向に進んでいると思いますが、いずれの場合も戻りコードは0になります。ただし、出力を確認するだけで済みます。

- Shell: dpkg-query -l '<package name>'
  register: dpkg_result

- do_something:
  when: dpkg_result.stdout != ""
2
udondan

私はこのパーティーに少し遅れていますが、終了コードを使用する別の例があります-dpkg-queryの結果で目的のステータステキストに明示的に一致することを確認してください:

- name: Check if SystemD is installed
  command: dpkg-query -s systemd | grep 'install ok installed'
  register: dpkg_check
  tags: ntp

- name: Update repositories cache & install SystemD if it is not installed
  apt:
    name: systemd
    update_cache: yes
  when: dpkg_check.rc == 1
  tags: ntp
1
dom_hutton