LoginSignup
1
1

More than 3 years have passed since last update.

ansible-playbook で limit (-l / --limit) を強制するタスク(指定がなければ実行終了)

Posted at

tl;dr

play

limit.yaml
- name: limit
  hosts: all
  gather_facts: no

  pre_tasks:
  - name: force use limit
    fail:
      msg: "use -l or --limit option when run."
    when: ansible_limit is not defined
    run_once: True

  tasks:
  - name: ansible_limit
    debug:
      msg: "{{ ansible_limit }}"

こまけぇこたぁいいんだよ。

$ ansible-playbook playbooks/limit.yml

PLAY [limit] ************************************************************************************************************

TASK [force use limit] ************************************************************************************************************
fatal: [xxxxxxxxxx]: FAILED! => {"changed": false, "msg": "use -l or --limit option when run."}

NO MORE HOSTS LEFT ************************************************************************************************************

PLAY RECAP ************************************************************************************************************
xxxxxxxxxx           : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0


$ ansible-playbook playbooks/limit.yml -l localhost

PLAY [limit] *********************************************************************************************************

TASK [force use limit] *********************************************************************************************************
skipping: [localhost]

TASK [ansible_limit] *********************************************************************************************************
ok: [localhost] => {
    "msg": "localhost"
}

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

蛇足

  • pre_tasks にすることで早い段階で処理(もちろんどこにでも書けるのでいくつか処理してから止めるのもできる)
  • -l に何も値を与えないと ansible がエラーにしてくれる
  • -l に値を与えてターゲットホストがなくなっても ansible がエラーにしてくれる
  • ansible_limit は 2.5 くらいからの機能 see Special Variables
1
1
0

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
1
1