"msg": "该任务包含一个带有未定义变量的选项 - 打印步骤的输出
"msg": "The task includes an option with an undefined variable - printing an output of a step
所以我有一个用来终止用户的剧本:
---
- name: "Playbook: Terminate user"
hosts: localhost
gather_facts: True
roles:
- { role: common, tags: ['always'] }
- { role: ad_termination, tags: ['ad_termination', 'never'] }
- name: "Playbook: Terminate storage"
hosts: storage
gather_facts: False
roles:
- { role: storage_termination, tags: ['storage_termination', 'always'] }
- name: "Playbook: User environment"
hosts: localhost
gather_facts: False
roles:
- { role: common,
tags: ['always'],
}
- { role: send_summary_email,
tags: ['send_summary_email', 'always']
}
我使用以下命令调用它:
ansible-playbook userOperations/userTerminationPB.yml -t ad_termination -e "username=user"
。然后它遍历每个剧本(总共 3 个)。
前 2 个剧本(终止用户和终止存储)一切正常。我在从第二个剧本 (terminate storage
) 传递变量时遇到问题。
如您所见,为存储剧本配置的 hosts
称为 storage
。它拥有几台服务器。
这是剧本本身:
- name: Logical block of home-storage tasks
block:
- name: Check if home folder exists
stat:
path: "{{ hostvars[inventory_hostname].fspath }}/{{ username }}"
register: home_dir_details
- name: Move user home folder to terminated home folder
command: mv {{ hostvars[inventory_hostname].fspath }}/{{ username }} "{{ hostvars[inventory_hostname].fspath }}/terminated/{{ username }}"
when: home_dir_details.stat.exists
register: storage_moved
- name: Printing and debugging
ansible.builtin.debug:
msg: "{{ storage_moved }}"
when:
- hostvars[inventory_hostname].fsname is search("home")
我试图在一封电子邮件中总结所有内容。我的电子邮件是我使用那些传递的变量编辑的 jinja2 模板。
我的问题是当我在我的 Jinja2 模板(发送电子邮件的第三个剧本)中使用 {{ storage_moved }}
时,它没有被识别为变量。
我不确定要更改什么才能识别它。我只是在我的模板中使用 {{ storage_moved }}
,但它不起作用。
它应该打印以下内容:
ok: [storage.server.com] => {
"msg": {
"changed": true,
"cmd": [
"mv",
"/path/user",
"/path/terminated/user"
],
"delta": "0:00:00.006022",
"end": "2021-10-19 16:13:03.468398",
"failed": false,
"rc": 0,
"start": "2021-10-19 16:13:03.462376",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
}
}
如有任何帮助,我们将不胜感激。目前我得到:
"msg": "The task includes an option with an undefined variable. The error was: 'storage_moved' is undefined
编辑:
通过在我的 jinja2 tempalte 中使用以下循环,我能够访问 storage_moved
中的所有信息:
{% for host in groups['storage'] %}
{{ hostvars[host]['storage_moved'] }}
{% endfor %}
未测试,但您可以在定义存储的剧本中使用 var 添加虚拟主机
- name: Register dummy host with variable
add_host:
name: "DUMMY_HOST"
STORAGE_NEW: "{{ storage_moved }}" #storage_moved.stdout
在另一本剧本中,您回显了变量:
tasks:
- name: Echo the output - storage_new var
shell: cat {{ hostvars['DUMMY_HOST']['STORAGE_NEW'] }} |tail -1
register: STORAGE_RESULT
我最终使用了:
{% for host in groups['storage'] %}
<li>{{ hostvars[host]['storage_moved']['cmd'] | default('') }}</li>
{% endfor %}
修复方法是将 | default('') }}
添加到我的搜索中,因为在循环期间 cmd
变量可能并不总是存在。
所以我有一个用来终止用户的剧本:
---
- name: "Playbook: Terminate user"
hosts: localhost
gather_facts: True
roles:
- { role: common, tags: ['always'] }
- { role: ad_termination, tags: ['ad_termination', 'never'] }
- name: "Playbook: Terminate storage"
hosts: storage
gather_facts: False
roles:
- { role: storage_termination, tags: ['storage_termination', 'always'] }
- name: "Playbook: User environment"
hosts: localhost
gather_facts: False
roles:
- { role: common,
tags: ['always'],
}
- { role: send_summary_email,
tags: ['send_summary_email', 'always']
}
我使用以下命令调用它:
ansible-playbook userOperations/userTerminationPB.yml -t ad_termination -e "username=user"
。然后它遍历每个剧本(总共 3 个)。
前 2 个剧本(终止用户和终止存储)一切正常。我在从第二个剧本 (terminate storage
) 传递变量时遇到问题。
如您所见,为存储剧本配置的 hosts
称为 storage
。它拥有几台服务器。
这是剧本本身:
- name: Logical block of home-storage tasks
block:
- name: Check if home folder exists
stat:
path: "{{ hostvars[inventory_hostname].fspath }}/{{ username }}"
register: home_dir_details
- name: Move user home folder to terminated home folder
command: mv {{ hostvars[inventory_hostname].fspath }}/{{ username }} "{{ hostvars[inventory_hostname].fspath }}/terminated/{{ username }}"
when: home_dir_details.stat.exists
register: storage_moved
- name: Printing and debugging
ansible.builtin.debug:
msg: "{{ storage_moved }}"
when:
- hostvars[inventory_hostname].fsname is search("home")
我试图在一封电子邮件中总结所有内容。我的电子邮件是我使用那些传递的变量编辑的 jinja2 模板。
我的问题是当我在我的 Jinja2 模板(发送电子邮件的第三个剧本)中使用 {{ storage_moved }}
时,它没有被识别为变量。
我不确定要更改什么才能识别它。我只是在我的模板中使用 {{ storage_moved }}
,但它不起作用。
它应该打印以下内容:
ok: [storage.server.com] => {
"msg": {
"changed": true,
"cmd": [
"mv",
"/path/user",
"/path/terminated/user"
],
"delta": "0:00:00.006022",
"end": "2021-10-19 16:13:03.468398",
"failed": false,
"rc": 0,
"start": "2021-10-19 16:13:03.462376",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
}
}
如有任何帮助,我们将不胜感激。目前我得到:
"msg": "The task includes an option with an undefined variable. The error was: 'storage_moved' is undefined
编辑:
通过在我的 jinja2 tempalte 中使用以下循环,我能够访问 storage_moved
中的所有信息:
{% for host in groups['storage'] %}
{{ hostvars[host]['storage_moved'] }}
{% endfor %}
未测试,但您可以在定义存储的剧本中使用 var 添加虚拟主机
- name: Register dummy host with variable
add_host:
name: "DUMMY_HOST"
STORAGE_NEW: "{{ storage_moved }}" #storage_moved.stdout
在另一本剧本中,您回显了变量:
tasks:
- name: Echo the output - storage_new var
shell: cat {{ hostvars['DUMMY_HOST']['STORAGE_NEW'] }} |tail -1
register: STORAGE_RESULT
我最终使用了:
{% for host in groups['storage'] %}
<li>{{ hostvars[host]['storage_moved']['cmd'] | default('') }}</li>
{% endfor %}
修复方法是将 | default('') }}
添加到我的搜索中,因为在循环期间 cmd
变量可能并不总是存在。