skip to Main Content

ansible-playbook hosts can not use jinja2 for matching more hosts

when i need to install more same service in one host in test env, i write a group like this

[nginx]
demo-region-nginx-1:22 ansible_ssh_host=10.0.0.1 port=80
demo-region-nginx-2:22 ansible_ssh_host=10.0.0.1 port=81
demo-region-nginx-3:22 ansible_ssh_host=10.0.0.1 port=82

my playbook like this

- hosts: "demo-region-nginx-{{ num }}"
  remote_user: root
  roles:
    - role: upgrade_nginx

amosttime just setup a "num" when i want to upgrade one nginx, this is easy, if i upgrade nginx-1 and nginx-3, i must be execute multiple times playbook.

In the future, i will have many such service, I must by setup more variable
,so i use jinja2 to filter the hosts, i think jinja2 can fix my problem, but it seems like error, the playbook like this

- hosts: "{% for num in {{ awx_num }} %}demo-region-nginx-{{ num }}{% endfor %}"
  remote_user: root
  roles:
    - role: upgrade_nginx

this is error log

ERROR! template error while templating string: expected token ':', got '}'. String: {% for num in {{ awx_num }} %}demo-region-nginx-{{ num }}{% endfor %}. expected token ':', got '}'

someone know how to fix this error?

2

Answers


  1. You dont need to set variables between brackets inside jinja expression on the for:

    - hosts: "{% for num in awx_num %}demo-region-nginx-{{ num }}{% endfor %}"
      remote_user: root
      roles:
        - role: upgrade_nginx
    
    Login or Signup to reply.
  2. You are reinventing the wheel and making your life complicated. ansible(-playbook) has a limit option that does exactly what you want.

    Given your above inventory and the dummy playbook:

    - name: Dummy PB to illustrate limit
      hosts: nginx
      gather_facts: false
      
      tasks:
        - ansible.builtin.debug:
            var: inventory_hostname
    

    Here are some example commands making use of patterns for the limit argument and their results:

    $ ansible-playbook -i inventory.ini dummy.yml
    
    PLAY [Dummy PB to illustrate limit] *****************************************************************************************************************************************
    
    TASK [ansible.builtin.debug] ************************************************************************************************************************************************
    ok: [demo-region-nginx-1] => {
        "inventory_hostname": "demo-region-nginx-1"
    }
    ok: [demo-region-nginx-2] => {
        "inventory_hostname": "demo-region-nginx-2"
    }
    ok: [demo-region-nginx-3] => {
        "inventory_hostname": "demo-region-nginx-3"
    }
    
    PLAY RECAP ******************************************************************************************************************************************************************
    demo-region-nginx-1        : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    demo-region-nginx-2        : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    demo-region-nginx-3        : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    
    
    $ ansible-playbook -i inventory.ini dummy.yml -l demo-region-nginx-1
    
    PLAY [Dummy PB to illustrate limit] *****************************************************************************************************************************************
    
    TASK [ansible.builtin.debug] ************************************************************************************************************************************************
    ok: [demo-region-nginx-1] => {
        "inventory_hostname": "demo-region-nginx-1"
    }
    
    PLAY RECAP ******************************************************************************************************************************************************************
    demo-region-nginx-1        : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    
    
    $ ansible-playbook -i inventory.ini dummy.yml -l '!demo-region-nginx-1'
    
    PLAY [Dummy PB to illustrate limit] *****************************************************************************************************************************************
    
    TASK [ansible.builtin.debug] ************************************************************************************************************************************************
    ok: [demo-region-nginx-2] => {
        "inventory_hostname": "demo-region-nginx-2"
    }
    ok: [demo-region-nginx-3] => {
        "inventory_hostname": "demo-region-nginx-3"
    }
    
    PLAY RECAP ******************************************************************************************************************************************************************
    demo-region-nginx-2        : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    demo-region-nginx-3        : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    
    
    $ ansible-playbook -i inventory.ini dummy.yml -l 'nginx[0]'
    
    PLAY [Dummy PB to illustrate limit] *****************************************************************************************************************************************
    
    TASK [ansible.builtin.debug] ************************************************************************************************************************************************
    ok: [demo-region-nginx-1] => {
        "inventory_hostname": "demo-region-nginx-1"
    }
    
    PLAY RECAP ******************************************************************************************************************************************************************
    demo-region-nginx-1        : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    
    
    $ ansible-playbook -i inventory.ini dummy.yml -l '!nginx[0]'
    
    PLAY [Dummy PB to illustrate limit] *****************************************************************************************************************************************
    
    TASK [ansible.builtin.debug] ************************************************************************************************************************************************
    ok: [demo-region-nginx-2] => {
        "inventory_hostname": "demo-region-nginx-2"
    }
    ok: [demo-region-nginx-3] => {
        "inventory_hostname": "demo-region-nginx-3"
    }
    
    PLAY RECAP ******************************************************************************************************************************************************************
    demo-region-nginx-2        : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    demo-region-nginx-3        : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    
    
    $ ansible-playbook -i inventory.ini dummy.yml -l 'nginx[1:]'
    
    PLAY [Dummy PB to illustrate limit] *****************************************************************************************************************************************
    
    TASK [ansible.builtin.debug] ************************************************************************************************************************************************
    ok: [demo-region-nginx-2] => {
        "inventory_hostname": "demo-region-nginx-2"
    }
    ok: [demo-region-nginx-3] => {
        "inventory_hostname": "demo-region-nginx-3"
    }
    
    PLAY RECAP ******************************************************************************************************************************************************************
    demo-region-nginx-2        : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    demo-region-nginx-3        : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    
    
    $ ansible-playbook -i inventory.ini dummy.yml -l '!nginx[1:]'
    
    PLAY [Dummy PB to illustrate limit] *****************************************************************************************************************************************
    
    TASK [ansible.builtin.debug] ************************************************************************************************************************************************
    ok: [demo-region-nginx-1] => {
        "inventory_hostname": "demo-region-nginx-1"
    }
    
    PLAY RECAP ******************************************************************************************************************************************************************
    demo-region-nginx-1        : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search