这个问题没有被回答.有人提到了环境变量.你能详细说明一下吗?
这似乎是一个简单的问题,但不是在ansible.它不断上升.特别是在错误的情况下 我需要一个全局变量.我可以在处理一个主机播放时设置,然后在以后与另一个主机进行检查.简而言之,我可以稍后在剧本中进行分支,具体取决于变量.
我们无法控制自定义软件安装,但如果已安装,我们必须在其他计算机上安装不同的软件.最重要的是,安装会有所不同,具体取决于VM文件夹.我的王国是全球变种.
变量的范围仅与当前的ansible_hostname相关.是的,我们将group_vars/all.yml作为全局变量,但我们无法在游戏中设置它们.如果我设置了一个变量,那么其他主机的播放/任务就无法看到它.我理解变量的范围,但我想设置一个可以在所有剧本剧中阅读的全局变量.实际的实现并不重要,但可变访问是(重要的).
我的问题:有没有办法设置一个在另一台主机上运行不同任务时可以检查的变量?像setGlobalSpaceVar(myvar,true)之类的东西?我知道没有任何这样的方法,但我正在寻找一种解决方法.重新措辞:为一个主机在一个任务中设置一个变量,然后在另一个主机的另一个任务中设置一个变量,读取该变量.
我能想到的唯一方法是更改控制器上的文件,但这似乎是假的.
一个例子
以下内容涉及oracle备份和本地可执行文件,但我保持通用.对于下面 - 是的,我可以做一个run_once,但这不会回答我的问题.这种变量访问问题不断出现在不同的环境中.
我有4台xyz服务器.我有2个程序需要执行,但只能在2台不同的机器上执行.我不知道哪个.对于不同的VM环境,设置可能会发生变化.
我们的programOne在具有驱动器E的服务器上运行.我可以使用ansible找到哪个服务器具有驱动器E,并且每当我设置变量(driveE_machine)时都相应地进行播放.它仅适用于该主机.为此,其他3台机器将没有设置driveE_machine.在稍后的游戏中,我需要在其他3台机器中的其中一台机器上执行另一个程序.这意味着我需要设置一个变量,该变量可由未运行第二个程序的其他两个主机读取.我不知道怎么做.
库存文件:
[xyz]
serverxyz[1:4].private.mystuff
Run Code Online (Sandbox Code Playgroud)
手册示例:
---
- name: stackoverflow variable question
hosts: xyz
gather_facts: no
serial: 1
tasks:
- name: find out who has drive E
win_shell: dir e:\
register: adminPage
ignore_errors: true
# This sets a variable that can only be read for that host
- name: set fact driveE_machine when rc is 0
set_fact:
driveE_machine: "{{inventory_hostname}}"
when: adminPage.rc == 0
- name: run program 1
include: tasks/program1.yml
when: driveE_machine is defined
# program2.yml executes program2 and needs to set some kind of variable
# so this include can only be executed once for the other 3 machines
# (not one that has driveE_machine defined and ???
- name: run program 2
include: tasks/program2.yml
when: driveE_machine is undefined and ???
# please don't say run_once: true - that won't solve my variable access question
Run Code Online (Sandbox Code Playgroud)
有没有办法设置在另一台主机上运行任务时可以检查的变量?
Kon*_*rov 18
不确定您真正想要什么,但是您可以使用单个循环任务(对全局变量的一些模拟)为每个主机设置一个事实:
剧本.yml
---
- hosts: mytest
gather_facts: no
vars:
tasks:
# Set myvar fact for every host in a play
- set_fact:
myvar: "{{ inventory_hostname }}"
delegate_to: "{{ item }}"
with_items: "{{ play_hosts }}"
run_once: yes
# Ensure that myvar is a name of the first host
- debug:
msg: "{{ myvar }}"
Run Code Online (Sandbox Code Playgroud)
主机
[mytest]
aaa ansible_connection=local
bbb ansible_connection=local
ccc ansible_connection=local
Run Code Online (Sandbox Code Playgroud)
结果
PLAY [mytest] ******************
META: ran handlers
TASK [set_fact] ******************
ok: [aaa -> aaa] => (item=aaa) => {"ansible_facts": {"myvar": "aaa"}, "ansible_facts_cacheable": false, "changed": false, "failed": false, "item": "aaa"}
ok: [aaa -> bbb] => (item=bbb) => {"ansible_facts": {"myvar": "aaa"}, "ansible_facts_cacheable": false, "changed": false, "failed": false, "item": "bbb"}
ok: [aaa -> ccc] => (item=ccc) => {"ansible_facts": {"myvar": "aaa"}, "ansible_facts_cacheable": false, "changed": false, "failed": false, "item": "ccc"}
TASK [debug] ******************
ok: [aaa] => {
"msg": "aaa"
}
ok: [bbb] => {
"msg": "aaa"
}
ok: [ccc] => {
"msg": "aaa"
}
Run Code Online (Sandbox Code Playgroud)
小智 6
https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#fact-caching
如文档中其他地方所示,一台服务器可以引用另一台服务器的变量,如下所示:
{{ hostvars['asdf.example.com']['ansible_os_family'] }}
这甚至适用于在剧本中动态设置的变量。
| 归档时间: |
|
| 查看次数: |
6150 次 |
| 最近记录: |