运行剧本时,各种主机上的未定义变量错误"dict 对象"



EDIT:似乎这只发生在使用--check参数运行时。实时运行此剧本不会引发此错误。但是,知道是什么原因造成的仍然是很好的。

我开始使用 Ansible AWX 来管理一堆服务器,并且以前从未使用过 Ansible,尽管我已经浏览了许多在线教程并且对它感到很舒服。

我正在尝试运行一个将更新安装到许多网络服务器的剧本。

它抛出了一个错误,奇怪的是,该错误出现在不同运行的不同主机上。例如,如果我运行剧本,主机server3.mydomain.com失败并显示此错误。如果我从清单中删除该服务器,则在server2.mydomain.com上收到相同的错误,依此类推。

错误输出没有提供足够的信息让我弄清楚为什么会失败,即使它将其隔离到一小部分,并且我还没有设法通过在线搜索找到问题。

这是剧本(来自我在网上找到的模板,有一些更改(:

---
- name: ensure services are up before doing anything
hosts: webservers
become: true
any_errors_fatal: true
serial: 1
tasks:
- name: upgrade packages and reboot (if necessary)
hosts: webservers
become: true
serial: 1 
any_errors_fatal: true
max_fail_percentage: 0
tasks: 
- name: apt-get update
apt:
update-cache: yes
changed_when: 0
- name: get list of pending upgrades
command: apt-get --simulate dist-upgrade
args:
warn: false 
register: apt_simulate
changed_when: 0
- name: parse apt-get output to get list of changed packages
set_fact: 
updates: '{{ apt_simulate.stdout_lines | select("match", "^Inst ") | list | sort }}'
changed_when: 0
- name: show pending updates
debug:
var: updates
when: updates.0 is defined
- name: apt-get autoremove
command: apt-get -y autoremove
args:
warn: false
when: '"Inst linux-image-" in apt_simulate.stdout'
changed_when: 0
- name: apt-get dist-upgrade
apt:
upgrade: dist 
register: upgrade_output
- name: check if reboot needed
stat: path=/var/run/reboot-required
register: file_reboot_required
- meta: end_play
when: not file_reboot_required.stat.exists
- name: reboot node
shell: sleep 2 && shutdown -r now "Reboot triggered by ansible"
async: 1
poll: 0
ignore_errors: true
- name: wait for node to finish booting
wait_for_connection:
connect_timeout=10
delay=30
timeout=120
- name: wait for ssh to start fully
pause:
seconds: 45

这是错误:

fatal: [server3.mydomain.com]: FAILED! => {
"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'stdout_lines'nnThe error appears to have been in '/var/lib/awx/projects/_8__infrastructure_management/projects/infrastructure-management/test/test.yml': line 30, column 7, but maynbe elsewhere in the file depending on the exact syntax problem.nnThe offending line appears to be:nnn    - name: parse apt-get output to get list of changed packagesn      ^ heren"

因此,该错误似乎与此块有关,但除此之外,我卡住了:

- name: parse apt-get output to get list of changed packages
set_fact: 
updates: '{{ apt_simulate.stdout_lines | select("match", "^Inst ") | list | sort }}'
changed_when: 0

看不到我在这里缺少什么。

使用--check运行无法"试运行"command:shell:模块,因为它无法预测什么是安全的运行,什么是不安全的。因此,由于command:不运行,因此它不会在该apt_simulate变量中创建任何stdout_lines。有趣的是,使用debug: var=apt_simulate表明它实际上确实说apt_simulate.skipped=Trueapt_simulate.msg="remote module (command) does not support check mode"。因此,您可以自己决定是否希望仅用when: not apt_simulate.skipped来保护该updates:参考,甚至甚至when: not {{ansible_check_mode}}

值得庆幸的是,如果您确定(就像您的命令一样(即使在检查模式下运行命令确实是安全的,您可以通过指定check_mode: no来覆盖command的行为。

最新更新