Override hosts variable of Ansible playbook from the command line

前端 未结 11 1729
隐瞒了意图╮
隐瞒了意图╮ 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:11

    This is a bit late, but I think you could use the --limit or -l command to limit the pattern to more specific hosts. (version 2.3.2.0)

    You could have - hosts: all (or group) tasks: - some_task

    and then ansible-playbook playbook.yml -l some_more_strict_host_or_pattern and use the --list-hosts flag to see on which hosts this configuration would be applied.

    0 讨论(0)
  • 2020-12-12 13:19

    Here's a cool solution I came up to safely specify hosts via the --limit option. In this example, the play will end if the playbook was executed without any hosts specified via the --limit option.

    This was tested on Ansible version 2.7.10

    ---
    - name: Playbook will fail if hosts not specified via --limit option.
      # Hosts must be set via limit. 
      hosts: "{{ play_hosts }}"
      connection: local
      gather_facts: false
      tasks:
      - set_fact:
          inventory_hosts: []
      - set_fact:
          inventory_hosts: "{{inventory_hosts + [item]}}"
        with_items: "{{hostvars.keys()|list}}"
    
      - meta: end_play
        when: "(play_hosts|length) == (inventory_hosts|length)"
    
      - debug:
          msg: "About to execute tasks/roles for {{inventory_hostname}}"
    
    0 讨论(0)
  • 2020-12-12 13:21

    I am using ansible 2.5 (2.5.3 exactly), and it seems that the vars file is loaded before the hosts param is executed. So you can set the host in a vars.yml file and just write hosts: {{ host_var }} in your playbook

    For example, in my playbook.yml:

    ---
    - hosts: "{{ host_name }}"
      become: yes
      vars_files:
        - vars/project.yml
      tasks:
        ... 
    

    And inside vars/project.yml:

    ---
    
    # general
    host_name: your-fancy-host-name
    
    0 讨论(0)
  • 2020-12-12 13:22

    For anyone who might come looking for the solution.
    Play Book

    - hosts: '{{ host }}'
      tasks:
      - debug: msg="Host is {{ ansible_fqdn }}"
    

    Inventory

    [web]
    x.x.x.x
    
    [droplets]
    x.x.x.x
    

    Command: ansible-playbook deplyment.yml -i hosts --extra-vars "host=droplets" So you can specify the group name in the extra-vars

    0 讨论(0)
  • 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 == "")
    
    0 讨论(0)
  • 2020-12-12 13:24

    An other solution is to use the special variable ansible_limit which is the contents of the --limit CLI option for the current execution of Ansible.

    - hosts: "{{ ansible_limit | default(omit) }}"
    

    If the --limit option is omitted, then Ansible issues a warning, but does nothing since no host matched.

    [WARNING]: Could not match supplied host pattern, ignoring: None
    
    PLAY ****************************************************************
    skipping: no hosts matched
    
    0 讨论(0)
提交回复
热议问题