ansible - 如何编写 Ansible 条件 for 循环
问题描述
我有一个文件/tmp/nodescount.txt
,它有一些值,如下所示:
instance-one
instance-two
instance-three
我需要读取此文件line-by-line
并在满足条件时执行另一个任务。
所以我这样做了:
---
- hosts: all_nodes
tasks:
- name: Get the existing node names
with_lines: cat /tmp/nodescount.txt
register: node_names
- name: Join the minions
become: true
shell: |
cd /tmp/
./join.sh
when: ansible_hostname != node_names # should loop over each node_name (instance-one, instance-two, instance-three)
因此,我只想在该特定节点中的值不等于三个值(实例一、实例二、实例三)中的任何一个Join the minions
时才运行任务。ansible_hostname
有人可以帮帮我吗?
解决方案
在这种情况下,循环应该发生在Join the minions
任务中。即在一个任务中读取文件内容,然后在下一个任务中遍历文件中的行。
例子:
- hosts: all_nodes
tasks:
- name: Get the existing node names
command: cat /tmp/nodescount.txt
register: node_names
- name: Join the minions
shell: ./join.sh
args:
chdir: /tmp
when: ansible_hostname != item
loop: "{{ node_names.stdout_lines }}"
become: true
推荐阅读
- android - 为什么 Kotlin 中函数式接口的目的是?
- r - 计算函数输出的均值和方差
- amazon-web-services - ECS Fargate foverver 在“IN_PROGRESS”中具有“rolloutState”,AWS 控制台上没有停止任务
- go - 持有已解析令牌的变量(令牌)返回 nil
- html - 如何覆盖 Boostrap 的导航栏内联搜索栏宽度?
- python - 为什么消除零项()不删除零条目?
- java - 如何在没有 Web 界面的情况下部署和运行 EAR java 程序?
- javascript - 当我单击它时,反应角色没有给我角色,我没有收到任何错误
- excel - 当特定列上没有特定文本时如何在excel中突出显示行
- c# - HttpClient向api发送空值,而不是查看json对象