循环jinja2模板中的主机作为变量

我知道Ansible支持以下形式的模板中的循环:

{% for host in groups['all'] %}
join= {{ hostvars[host]['ansible_eth0']['ipv4']['address'] }}:29016
{% endfor %}

我想将主机库存更改为动态,并采用主机变量

例如 想象一下这个设置:

# hosts
[testOne]
192.168.42.2
192.168.42.10 
192.168.42.20

[testTwo]
192.168.42.212
192.168.42.101 
192.168.42.202

playbook.yml

---

- hosts: all
  gather_facts: no
  tasks:
    - name: Hosts
      template: src=myhosts.j2 dest=./myhosts.json
      delegate_to: 127.0.0.1
      run_once: yes

{% for host in groups['{{ hosts }}'] %}
join= {{ hostvars[host]['ansible_eth0']['ipv4']['address'] }}:29016
{% endfor %}

并运行为:

ansible-playbook -i hosts playbook.yml -e"hosts=testTwo"

但是Ansible不知道要解释主机变量以列出并循环遍历

有帮助吗? 谢谢!

yuanlei99 回答:循环jinja2模板中的主机作为变量

您应使用ansible_play_hosts_all选择您的主机/组

hosts.ini变量在其上循环。


演示:

[group1] host1 ansible_host=1.2.3.4 ansible_connection=local host2 ansible_host=2.3.4.5 ansible_connection=local [group2] host3 ansible_host=3.4.5.6 ansible_connection=local host4 ansible_host=4.5.6.7 ansible_connection=local

play.yml

--- - hosts: all gather_facts: no tasks: - debug: msg: | {% for host in ansible_play_hosts_all -%} {{ host }},{%- endfor %} delegate_to: localhost run_once: yes

$ ansible-playbook -i hosts.ini play.yml --limit group1

PLAY [all] *********************************************************************************************************************************

TASK [debug] *******************************************************************************************************************************
ok: [host1 -> localhost] => {
    "msg": "host1,host2,\n"
}

PLAY RECAP *********************************************************************************************************************************
host1                      : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
$ ansible-playbook -i hosts.ini play.yml --limit host1,host3

PLAY [all] *********************************************************************************************************************************

TASK [debug] *******************************************************************************************************************************
ok: [host1 -> localhost] => {
    "msg": "host1,host3,\n"
}

PLAY RECAP *********************************************************************************************************************************
host1                      : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

type Sizes = 
| Big 
| Medium 
| Small
;;

//defines cup/can/bottle with size
type Containment = 
| CupDrink of s:Sizes
| CannedDrink of s:Sizes
| BottledDrink of s:Sizes
;;

// defines record for each type for drink
type Coffee = {DrinkName : string; price: double  }
type Soda = {DrinkName : string; price: double }
type Brew = {DrinkName : string; price: double }

// union for type of drink
type liquid = 
| Coffee of c:Coffee
| Cola of s:Soda
| Beer of b:Brew
;;

let Guiness = Beer {DrinkName = "Guiness"; price = 0.15}
let CocaCola = Cola {DrinkName = "Cola"; price = 0.15}

let smallCup = CupDrink Small // it could be just containment | size
let bigBottle= BottledDrink Big


let findPricePrML(dr:liquid) =
     let price = 0.0
     match dr with 
    |Beer(b=h)->  h.price 
    |Cola(s=h) ->h.price 
    |Coffee(c=h)  -> h.price 
    |_-> failwith "not found"
// returns size in ML fro each  size available // asuming that small bottle,can and cup have same size,//if not another kind of program can be made but it's not part of the assingment
let find size = 
    match size with
    |Big -> 250.00
    |Medium -> 125.00
    |Small -> 75.00
本文链接:https://www.f2er.com/3165747.html

大家都在问