Ansible:如何为主机设置序列号

ai0*_*307 3 jinja2 ansible ansible-playbook apache-zookeeper ansible-inventory

我正在尝试在EC2上配置主机,因此我正在使用Ansible Dynamic Inventory.

我想做的是; 设置每个节点的序列号.

例如:Zookeeper的"myid"配置

Zookeeper需要为每个节点命名为"myid"的序列号; 1为hostA,2为hostB,3为hostC,依此类推.

这是我的剧本中将"myid"文件复制到主机的部分.

- name: Set myid
  sudo: yes
  template: src=var/lib/zookeeper/myid.j2 dest=/var/lib/zookeeper/myid
Run Code Online (Sandbox Code Playgroud)

而且myid.j2应该是这样的下面.

{{ serial_number }}
Run Code Online (Sandbox Code Playgroud)

问题是:变量"{{serial_number}}"应该是什么样的?

Mar*_*kNS 7

我使用Ansible的with_index_items语法找到了一个很好的干净方法:

tasks:
  - name: Set Zookeeper Id
    set_fact: zk_id={{item.0 + 1}}
    with_indexed_items: "{{groups['tag_Name_MESOS_MASTER']}}"
    when: item.1 == "{{inventory_hostname}}"
Run Code Online (Sandbox Code Playgroud)

然后可以将/ etc/zookeeper/conf/myid模板设置为

{{zk_id}}
Run Code Online (Sandbox Code Playgroud)

这假设您正在使用AWS动态库存.