[英]Is there a way to set an environment variable while looping over a role in Ansible playbook?
I need loop over an Ansible role and save the index of the iteration. 我需要循环一个Ansible角色并保存迭代的索引。
My goal is to use the number(which is INDEX in this case) in my-role for each iteration. 我的目标是在每个迭代中使用my-role中的数字(在本例中为INDEX)。 My-role executes other playbooks and I need the value of the INDEX for each iteration.
我的角色执行其他剧本,我需要每次迭代的INDEX值。 I want to use that index in another ansible-playbook.
我想在另一个ansible-playbook中使用该索引。 So my plan is to set an environment variable to read from the other plaaybook.
所以我的计划是设置一个环境变量来从另一个plaaybook中读取。
This is my code: 这是我的代码:
- name: my-role
with_sequence: 'start=0 end={{ number_of_users|int }}'
include_role:
name: my-role
vars:
user_name: '{{ user_temp[item|int] }}'
queue_name: '{{ queue_temp[item|int] }}'
lineinfile:
dest: "/etc/environment"
state: present
line: 'export INDEX=[item|int]'
Apparently I cannot do two statements at the same time. 显然我不能同时做两个陈述。
The error message is: 错误消息是:
ERROR!
错误! conflicting action statements: include_role, lineinfile
冲突的动作语句:include_role,lineinfile
Is there a way to set an environment variable while looping over a role? 有没有办法在循环角色时设置环境变量?
Is there a way to set an environment variable while looping over a role?
有没有办法在循环角色时设置环境变量?
Yes, using the apply:
option of include_role:
是的,使用
include_role:
的apply:
选项include_role:
- with_sequence: start=0 end=3
include_role:
name: my-role
apply:
environment:
INDEX: '[{{item}}]'
BTW, even if ansible had let you run lineinfile
alongside that role, just putting an entry in /etc/environment
is highly unlikely to automatically expose that environment variable to the role. 顺便说一句,即使ansible 已经让你运行
lineinfile
旁边那个角色,只是把一个条目/etc/environment
是极不可能自动在该环境变量暴露的作用。 It would require that every ssh connection made for every task actually source /etc/environment
, which might happen but it's unwise to count on it when ansible offers you an environment:
directive that is explicitly designed to do that. 它需要为每个任务实现的每个ssh连接实际上都是源
/etc/environment
,这可能会发生,但是当ansible为您提供environment:
时指望它是不明智的environment:
指令明确地设计用于执行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.