我有一个在不同主机上运行不同角色的剧本.是否可以将变量从一个主机上运行的一个角色传递到同一个playbook运行中运行的另一个主机上的另一个角色?或任何解决方法?
playbook
host1
role1
here I get some variables: var1 var2 ...etc
host2
role2
here I need to use var1 var2 ... etc from the above host/role
Run Code Online (Sandbox Code Playgroud)
role1中设置teh变量的任务db如下所示:
- shell: cd /ACE/conf && grep ^db.url local1.properties | awk -F/ '{print $4}' | awk -F? '{print $1}'
register: db
Run Code Online (Sandbox Code Playgroud)
更新:在第一台主机上,值是动态的,它就像一个始终更新的配置文件.在我使用role1将值存储在host1上的变量之后,然后移动到host2,运行role2并使用host1存储的变量对这些值进行处理.
我尝试使用hostvars:
{{ hostvars.LBL.db.stdout }}
{{ hostvars['LBL']['db'] }}
{{ hostvars['LBL']['db']['stdout'] }}
Run Code Online (Sandbox Code Playgroud)
我收到错误:
in get_variables raise Exception("host not found: %s" % hostname) Exception: host not found: LBL
Run Code Online (Sandbox Code Playgroud)
LBL存在于主机中,我运行第一个角色
我在一台主机上设置了一个变量,我希望该变量可供其他主机使用.所有这些都在一本剧本中.可以吗?
hostvars不能像这样使用它:
--- …Run Code Online (Sandbox Code Playgroud) 我想在一个剧本中运行10次角色,并且仅在该角色的第5次运行时,我希望它从该角色中运行第二个shell cmd.我该如何解决这个问题?剧本:
- name: bla bla
hosts: ALL
remote_user: root
vars:
some_variable: 0
roles:
- role: nonreg
whentorun:
- post
Run Code Online (Sandbox Code Playgroud)
实际的角色是这样的:
- name: basic
shell: /scripts/nonReg/expoNonRegTest.sh {{ item }}
{{ some variable }} ++ ???
with_items: "{{ whentorun }}"
- name: on 5th run
shell: /scripts/nonReg/expoNonRegTest.sh diff
when: {{ some variable }} == 5 ????
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?如何声明变量并为其赋值(在角色/剧本的运行期间)?语法是什么?在ansible文档中,在变量中,没有一个简单的例子说明如何为变量赋值(不是使用寄存器:P)
我必须做一些基准测试并循环 10 个命令 3 次(运行所有 10x3,而不是第一个 x3 然后第二个 x3 - 所以运行所有 10x3)。我从寄存器变量中的文件中提取的 10 个命令(它不适用于_lines:然后是命令)并执行它们 1,2,3..,10 将输出传输到文件中,回显某些内容,然后再次执行他们...这一切 3 次
这就是我的做法,x3 下面的代码(nagios_check 注册变量中有 10 个命令/行):
... more code above
- name: get the date for naming purpose
shell: date +%Y%m%d-%HH%MM%SS
register: dateext
- name: grep the commands from nagios
shell: grep -R check_http_EDEN_ /etc/nagios/nrpe.cfg | cut -d= -f2-
register: nagios_check
- name: check_eden_before
shell: (printf $(echo '{{ item }}' | awk -F'country=' '{print $2}' | cut -d'&' -f1); printf ' ';{{ item …Run Code Online (Sandbox Code Playgroud) 我想将此输出(来自上一个命令)用作键值数组或同一剧本中下一个命令的清单
stdout:
hot-01: 10.100.0.101
hot-02: 10.100.0.102
hot-03: 10.100.0.103
....
hot-32: 10.100.0.132
Run Code Online (Sandbox Code Playgroud)
像这样:
- shell: "echo {{ item.key }} has value {{ item.value }}"
with_items: "{{ output.stdout_lines }}"
Run Code Online (Sandbox Code Playgroud)
或者:
- add_host: name={{ item.key }} ansible_ssh_host={{ item.value }}
with_items: "{{ output.stdout_lines }}"
Run Code Online (Sandbox Code Playgroud)
echo 命令的期望输出:
hot-01 has value 10.100.0.101
Run Code Online (Sandbox Code Playgroud)
我也试过 with_dict: "{{ output.stdout }}" 但仍然没有运气
"fatal: [ANSIBLE] => with_dict expects a dict"
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用consul v0.9.1后端自动执行vault v0.8.0部署(来自Hashicorp的vaultproject).
因为这是一个试错过程,我需要多次运行"vault init"(直到我做对了)并获得密钥.
不幸的是,我丢失了密钥和根令牌.
我试图阻止保险库和领事服务 - 没有"*保险柜已经初始化"和"*保险柜密封"
我停止了保险库,从领事处删除了保险库路径,启动了保险库 - 同样的结果 - 并且在"保险库保险库初始化"我收到此错误:
* expiration state restore failed: failed to scan for leases: list failed at path '': Unexpected response code: 403
Run Code Online (Sandbox Code Playgroud)
它在领事中再次创建了保险库/路径并保持密封状态.
如何"重置"保险库或使其进行UN初始化并重新启动"vault init"?
这是日志:
Aug 10 05:01:49 TSLASOWROMM01 vault[9156]: ==> Vault server started! Log data will stream in below:
Aug 10 05:03:26 TSLASOWROMM01 vault[9156]: 2017/08/10 05:03:26.238436 [INFO ] core: security barrier not initialized
Aug 10 05:03:26 TSLASOWROMM01 vault[9156]: 2017/08/10 05:03:26.271844 [INFO ] core: security barrier initialized: shares=5 …Run Code Online (Sandbox Code Playgroud) 我有ansible 2.2.1.0并且我有这个角色:
- block:
- shell: echo 1
- shell: fail
- shell: echo 2
rescue:
- shell: echo 3
always:
- shell: echo 4
- name: running something after the block
shell: echo 5
Run Code Online (Sandbox Code Playgroud)
如果我运行这个角色,块的第一部分失败是因为"失败"命令(不存在:)"stderr:/ bin/sh:1:失败:未找到")所以救援开始+总是.但是最后一项任务,就是"在块之后运行的东西"永远不会运行.戏剧结束了!
这是为什么 ?预期的结果我会把它看作:如果块的第一部分失败,则总是(如果有的话)进行救援,然后继续播放其余的任务.
在文档中没有任何关于此的内容.
有解决方法吗?
ansible ×5
roles ×3
variables ×3
allocation ×1
ansible-2.x ×1
consul ×1
increment ×1
loops ×1
output ×1