如何 运行 Glusterfs-cli 命令 "peer probe" 使用条件为 "when" 的 Ansible 主机组

How to run Glusterfs-cli command "peer probe" using Ansible host group with condition "when"

我正在尝试创建 ansible 角色以在 AWS Centos 中创建 glusterfs 卷副本 instances.I 成功创建卷并安装 glusterfs。我尝试在主机组的每台机器上使用此 topic 建议 运行ning "gluster peer probe "。 我有在主机组上 运行ning "glusterfs" 角色的剧本:

[Server]
devserver1.net ansible_user=ec2-user ansible_ssh_private_key_file=/some.pem
devserver2.net ansible_user=ec2-user ansible_ssh_private_key_file=/some.pem

我现在正在尝试的是 运行 "gluster peer probe...",当此任务 运行 在此主机组的特定主机中时。像那样:

- name: gluster peer probe in server1
  shell: gluster peer probe {{ stackName }}server2.net
  when: "'{{ stackName }}server1.net' == {{ inventory_hostname }}"

- name: gluster peer probe in server2
  shell: gluster peer probe {{ stackName }}server1.net
  when: "'{{ stackName }}server2.net' == {{ inventory_hostname }}"

StackName 是剧本的预定义清单 "dev" 变量。
它失败并出现错误:

FAILED! => {"failed": true, "msg": "The conditional check '{{ stackName }} server1.net in inventory_hostname' failed. The error was: error while evaluating conditional ({{ stackName }}server1.net in inventory_hostname): 'server1' is undefined\n\nThe error appears to have been in 'ansible/roles/glusterfs/tasks/main.yml': line 69, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: gluster peer probe from server1\n ^ here\n"}

有任何关于如何做到这一点的建议吗?或者我在这里做错了什么?

您不应将 Jinja2 模板(带有表达式)添加到 Ansible 中的 when 条件。整个参数应该是一个 Jinja2 表达式本身:

when: "stackName + 'server1.net' == inventory_hostname"

我们这样做的方式(并且对我们有用)

- name: Form a cluster.
  command: "gluster peer probe {{ peernode }}"
  loop: "{{ ansible_play_hosts | flatten(levels=1) }}"
  loop_control:
    loop_var: peernode
    pause: 2
  register: glusterpeerprobe
  become: true
  run_once: true
  any_errors_fatal: true
  changed_when: "'peer probe: success.' in glusterpeerprobe.stdout_lines"