50
53

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ansibleのshellモジュールをchanged_whenと組み合わせて柔軟に

Last updated at Posted at 2014-02-28

ふつうにshell叩くと常に実行されてchanged=1になり気持ち悪い. changedになる条件をchanged_whenで定義することが出来る.

tasks:
  - name: install python-apt
    shell: LC_ALL=en_EN.UTF-8 sudo apt-get install -y python-apt
    register: result
    changed_when: '"is already the newest version" not in result.stdout'
  - debug: var=result
  • ポイント
    • commandではなくshell moduleを使う. commandだとchanged_when行でerror while evaluating conditionalとエラーが発生
    • 日本語出力だとマッチさせにくいのでLC_ALL=en_EN.UTF-8を付ける
    • shell実行結果を変数にregister. register.stdout, register.stderr等が使える
    • 試す時はdebug行つけるとよい

追記

上の例はAnsibleでaptモジュールを使うためにpython-aptを入れる必要があったため作成したタスクで, 単純にapt-getでなにか入れたいだけなら, shellでなくaptモジュールで

apt.yml
- name: install nginx
  sudo: yes
  apt: pkg=nginx update_cache=yes

こんな風に書くだけで良い

50
53
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
50
53

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?