Ansible,字段args的值无效

Tuo*_*nen 2 configuration provisioning configuration-management ansible ansible-playbook

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

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"}
Run Code Online (Sandbox Code Playgroud)

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

- name: Host is present
  lineinfile:
    dest: /etc/hosts
    regexp: "^{{ item.host_ip }}"
    line: "{{ item.host_ip }} {{ item.host_name }}"
  with_items: hosts
  tags: [common]
Run Code Online (Sandbox Code Playgroud)

这是内容 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
Run Code Online (Sandbox Code Playgroud)

为什么ansible声称args是未定义的.我正在使用的例子不是由我做的,而是几个月前,所以我想,如果ansible处理角色变量的方式发生了变化,那么它就不起作用了.

Kon*_*rov 12

with_items:hosts

这是过时的语法.您不能将裸变量用于循环.

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