Override hosts variable of Ansible playbook from the command line

前端 未结 11 1785
隐瞒了意图╮
隐瞒了意图╮ 2020-12-12 12:29

This is a fragment of a playbook that I\'m using (server.yml):

- name: Determine Remote User
  hosts: web
  gather_facts: false
  roles:
    - {         


        
11条回答
  •  一个人的身影
    2020-12-12 13:24

    I changed mine to default to no host and have a check to catch it. That way the user or cron is forced to provide a single host or group etc. I like the logic from the comment from @wallydrag. The empty_group contains no hosts in the inventory.

    - hosts: "{{ variable_host | default('empty_group') }}"
    

    Then add the check in tasks:

       tasks:
       - name: Fail script if required variable_host parameter is missing
         fail:
           msg: "You have to add the --extra-vars='variable_host='"
         when: (variable_host is not defined) or (variable_host == "")
    

提交回复
热议问题