Override hosts variable of Ansible playbook from the command line

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

    I don't think Ansible provides this feature, which it should. Here's something that you can do:

    hosts: "{{ variable_host | default('web') }}"

    and you can pass variable_host from either command-line or from a vars file, e.g.:

    ansible-playbook server.yml --extra-vars "variable_host=newtarget(s)"
    
    0 讨论(0)
  • 2020-12-12 13:29

    We use a simple fail task to force the user to specify the Ansible limit option, so that we don't execute on all hosts by default/accident.

    The easiest way I found is this:

    ---
    - name: Force limit
      # 'all' is okay here, because the fail task will force the user to specify a limit on the command line, using -l or --limit
      hosts: 'all'
    
      tasks:
      - name: checking limit arg
        fail:
          msg: "you must use -l or --limit - when you really want to use all hosts, use -l 'all'"
        when: ansible_limit is not defined
        run_once: true
    

    Now we must use the -l (= --limit option) when we run the playbook, e.g.

    ansible-playbook playbook.yml -l www.example.com
    

    Limit option docs:

    Limit to one or more hosts This is required when one wants to run a playbook against a host group, but only against one or more members of that group.

    Limit to one host

    ansible-playbook playbooks/PLAYBOOK_NAME.yml --limit "host1"

    Limit to multiple hosts

    ansible-playbook playbooks/PLAYBOOK_NAME.yml --limit "host1,host2"

    Negated limit.
    NOTE: Single quotes MUST be used to prevent bash interpolation.

    ansible-playbook playbooks/PLAYBOOK_NAME.yml --limit 'all:!host1'

    Limit to host group

    ansible-playbook playbooks/PLAYBOOK_NAME.yml --limit 'group1'

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

    If you want to run a task that's associated with a host, but on different host, you should try delegate_to.

    In your case, you should delegate to your localhost (ansible master) and calling ansible-playbook command

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

    Just came across this googling for a solution. Actually, there is one in Ansible 2.5. You can specify your inventory file with --inventory, like this: ansible --inventory configs/hosts --list-hosts all

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

    I'm using another approach that doesn't need any inventory and works with this simple command:

    ansible-playbook site.yml -e working_host=myhost
    

    To perform that, you need a playbook with two plays:

    • first play runs on localhost and add a host (from given variable) in a known group in inmemory inventory
    • second play runs on this known group

    A working example (copy it and runs it with previous command):

    - hosts: localhost
      connection: local
      tasks:
      - add_host:
          name: "{{ working_host }}"
          groups: working_group
        changed_when: false
    
    - hosts: working_group
      gather_facts: false
      tasks:
      - debug:
          msg: "I'm on {{ ansible_host }}"
    

    I'm using ansible 2.4.3 and 2.3.3

    0 讨论(0)
提交回复
热议问题