编辑:看来这仅在使用--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'\n\nThe error appears to have been in '/var/lib/awx/projects/_8__infrastructure_management/projects/infrastructure-management/test/test.yml': line 30, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: parse apt-get output to get list of changed packages\n ^ here\n"
因此,错误似乎与此块有关,但除此之外,我还遇到了问题:
- 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
在这里看不到我想念的东西。
答案 0 :(得分:1)
使用--check
进行运行无法“试运行” command:
或shell:
模块,因为它无法预测运行安全与否的安全性。因此,由于command:
不运行,因此不会在该stdout_lines
变量中创建任何apt_simulate
。有趣的是,使用debug: var=apt_simulate
表明它确实说了apt_simulate.skipped=True
和apt_simulate.msg="remote module (command) does not support check mode"
。因此,您可以自己决定是否只想使用updates:
来保护when: not apt_simulate.skipped
引用,甚至可以保护when: not {{ansible_check_mode}}
值得庆幸的是,您可以通过指定check_mode: no
来覆盖command
的行为,前提是您可以确定-即使在您的命令中确实可以安全地运行该命令,就像您的命令一样检查模式。