我正在Ansible剧本中设置一个新剧本,该剧本在一组机器上进行一些操作。 这些操作需要以串行模式1 x 1播放。
我的工作在第一台机器上运行良好,但在其他机器上,似乎变量没有值。
- name: "My play"
hosts: my_hosts_group
tags: [test]
serial: 1
become: yes
become_user: root
tasks:
- shell: echo {{ MYVARIBLE.MYSOFTWARE.souche_path }} > /home/USER/test.txt
register: result
- debug: var=result
这是我的数据模型:
MYVARIBLE:
MYSOFTWARE:
souche_path: "/mybinpath/mybin"
对于第一台机器,它运行良好,并且我收到此消息:
ok: [host1] => {
"changed": false,
"result": {
"changed": true,
"cmd": "echo my_variable_value > /home/USER/test.txt",
"delta": "0:00:00.005541",
"end": "2019-04-26 16:27:09.415017",
"failed": false,
"rc": 0,
"start": "2019-04-26 16:27:09.409476",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
}
}
但是要给他一个人,我得到这个:
fatal: [host2]: FAILED! => {}
MSG:
The task includes an option with an undefined variable. The error was: 'MYVARIABLE' is undefined
The error appears to have been in '/my playbook/install.yml': line 85, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
tasks:
- shell: echo {{ MYVARIBLE }} > /home/USER/test.txt
^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:
with_items:
- {{ foo }}
Should be written as:
with_items:
- "{{ foo }}"
exception type: <class 'ansible.errors.AnsibleUndefinedVariable'>
exception: 'MYVARIBLE' is undefined
我已在并行模式下播放它,并且运行良好。因此,我可以确定问题在于变量仅在第一个循环中被赋值。
我想念什么吗?