简体   繁体   English

有没有办法在环绕Ansible playbook中的角色时设置环境变量?

[英]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.

相关问题 Ansible剧本,设置环境变量不起作用 - Ansible playbook, set environment variables not working 在Ansible playbook中激活Conda环境 - Activating a Conda environment in Ansible playbook 有没有办法从 Ansible 剧本调用 Python 脚本并将 output 作为变量再次传递给 Ansible 剧本以进行下一个任务? - Is there any way to call a Python script from an Ansible playbook and pass the output as variable to an Ansible playbook back again for the next task? 是否可以使用ansible playbook创建python虚拟环境? - Is it possible to create python virtual environment with ansible playbook? 循环结果集时插入行 - Inserting rows while looping over result set Ansible playbook 在新的 Ubuntu/Ansible/Python 环境中停止工作 - Ansible playbook stops working in new Ubuntu/Ansible/Python environment 有没有办法在剧本中自动化 ansible 保险库 - Is there a way to automate ansible vault within the playbook jinja2 '{% set ... %}' 在 ansible playbook 中处理 - jinja2 '{% set ... %}' processing in ansible playbook 如何使用 Ansible 读取先前任务中使用 shell 模块设置的环境变量 - How to read environment variable set with shell module in previous task with Ansible 脚本运行时设置虚拟环境变量? - set virtual environment variable while script runs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM