Good evening. I have an inventory file of the following form:

[main] cl-main ansible_host=10.0.9.700 ansible_user=ansible [cluster] cl-node1 ansible_host=10.0.9.701 ansible_user=ansible cl-node2 ansible_host=10.0.9.702 ansible_user=ansible cl-node3 ansible_host=10.0.9.703 ansible_user=ansible 

In the directory / playbooks is a set of playbooks. The hosts indicate where the playbook will be played.

Playback for example:

 --- - hosts: cl-main roles: - kiask.cassandra 

What if I want to play a playbook for cl-node1? It is necessary to change the addressee in the playbook itself (which is not good), or can this be done by one team selecting the host from the inventory file?

Thank you for attention!

    2 answers 2

    Use the -l , --limit :

      -l SUBSET, --limit=SUBSET further limit selected hosts to an additional pattern 

    To describe a pattern (pattern) in Ansible, use the following syntax :

    For a single host or group:

     ansible-playbook playbook.yml -i hosts -l 'cl-node1' 

    Several hosts can be listed through:

     ansible-playbook playbook.yml -i hosts -l 'cl-node1:cl-node2:cl-node3' 

    You can specify a range:

     ansible-playbook playbook.yml -i hosts -l 'cl-node[1-2]' 

    You can use masks:

     ansible-playbook playbook.yml -i hosts -l 'cl-node*' 

    There is also the intersection of sets, the negation, and all this can be combined. Examples can be found in the documentation .

    In addition, you can set inventory dynamically . For example, to execute a playbook on a local machine:

     ansible-playbook playbook.yml -i 'localhost,' 

    Note: the comma after localhost is important, because this is a list!

    • 2
      I think you need to rearrange this answer. - AK
    • one
      Thank you, I gathered a lot of useful information for myself. And for everyone else, this topic will probably be interesting. - Alexander Sergeev

    The command line cannot be selected less than a group. One point host can not be selected, apparently ideologically ansible sharpened immediately under the host groups.

    When such (and often often) is necessary, the following typical method is often used: transferring the host parameter to extra-vars .

    In the playbook write something like:

     --- # This playbook updates all packages. - name: yum update all packages hosts: '{{ target | default("all") }}' become: true become_user: root tasks: - name: yum update all yum: name=* state=latest 

    And call it for a specific host with the command:

     ansible-playbook -i hosts_production yum_update_all.yml --extra-vars "target=cl-node2" 

    If you omit the variable, the command will be executed by default on all servers:

     ansible-playbook -i hosts_production yum_update_all.yml 

    I’ve gotten used to this method; I’ve been starting all playbooks for a long time with a similar introduction.

    Yes, instead of default("all") you can specify any group, for example, specify default("cluster") .

    • Thank you, chew well!)) - Alexander Sergeev
    • 3
      How less than a group? And ansible-playbook ... -l 'host1:host2:hostblabla*' ? - Nick Volynkin
    • @NickVolynkin Wow, live and learn. Did not know, thanks! - AK
    • one
      And this method is good for a situation when you need to start a playbook only with a clearly specified host. To do this, you will need to remove | default("all") | default("all") - Nick Volynkin