Ansible,字段 args 的值无效 [英] Ansible, the field args has an invalid value

查看:47
本文介绍了Ansible,字段 args 的值无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 playbook.yml 中添加了一个名为 common 的角色,但配置失败并显示以下消息:

I added a role called common to my playbook.yml, and provisioning fails with the message:

TASK [common : Host is present] ************************************************
==> cd: fatal: [localhost]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'unicode object' has no attribute 'host_ip'\n\nThe error appears to have been in '/vagrant/ansible/roles/common/tasks/main.yml': line 7, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Host is present\n  ^ here\n"}

这里是roles/common/tasks/main.yml的内容,失败的特定任务

here is the content of roles/common/tasks/main.yml, the particular tasks which fails

- name: Host is present
  lineinfile:
    dest: /etc/hosts
    regexp: "^{{ item.host_ip }}"
    line: "{{ item.host_ip }} {{ item.host_name }}"
  with_items: hosts
  tags: [common]

这里是roles/common/defaults/main.yml

hosts: [
  { host_ip: "10.100.198.200", host_name: "cd"},
  { host_ip: "10.100.198.201", host_name: "prod"},
  { host_ip: "10.100.198.202", host_name: "logging"},
  { host_ip: "10.100.194.201", host_name: "serv-disc-01"},
  { host_ip: "10.100.194.202", host_name: "serv-disc-02"},
  { host_ip: "10.100.194.203", host_name: "serv-disc-03"},
  { host_ip: "10.100.193.200", host_name: "proxy"},
  { host_ip: "10.100.192.200", host_name: "swarm-master"},
  { host_ip: "10.100.192.201", host_name: "swarm-node-1"},
  { host_ip: "10.100.192.202", host_name: "swarm-node-2"},
]

obsolete_services:
  - puppet
  - chef-client

为什么 ansible 声称 args 是未定义的.我正在使用的示例不是我制作的,并且已经有几个月的历史了,所以我想知道,是否 ansible 处理角色中变量的方式发生了变化,因此它不起作用.

Why ansible claims that args is undefined. The example I'm using is not made by me and is few months old, so I wonder, if the way ansible processes variables in roles has changed, and thus it doesn't work.

推荐答案

with_items:主机

with_items: hosts

这是过时的语法.不能为循环使用裸变量.

This is obsolete syntax. You can't use bare variables for loops.

正确的语法:with_items: "{{ hosts }}"

这篇关于Ansible,字段 args 的值无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆