0

我有一个创建用户的剧本,当所有三个变量都被硬编码时,剧本运行良好,而如果我传递变量,我会遇到错误。我通过带有额外变量的 AWX 控制台执行此剧本并得到“未定义变量错误

剧本

#User creation playbook
---
- hosts: "all"
  gather_facts: yes
  become: yes
  become_method: sudo
  #vars:
  #  users:
  #  - username: "{{ user }}"
  #    groups: "{{ groups }}"
  #    password: "{{ password }}"
      #replace newpassword with whatever password you want to set for the user
  #  - username: "user2"
  #    groups: "groupC"
  #    password: "{{ 'newpassword' | password_hash('sha512') }}"
  tasks:
  - name: "Create user accounts"
    user:
      name: "{{ username }}"
      groups: "{{ groups }}"
      password: "{{ password }}"
      update_password: on_create
    #with_items: "{{ users }}"
  #- name: "Add authorized keys"
  #  authorized_key:
  #    user: "{{ item.username }}"
  #    key: "{{ lookup('file', 'ssh_keys/'+ item.username + '.key.pub') }}"
  #  with_items: "{{ users }}"

额外变量

---
vars:
 - username: testuserxx
   groups: nginx,mysql
   password: $6$UxUZNNsVnWUSM9ge$0viDFuXUwtLYyx/U4tFmX8w3TknD3C19Oab.clfi67WGGqJUDrkGpMOvHE1zwudXyKl0gmqAw8piR8KFGi55R/

我使用创建了密码哈希mkpasswd --method=sha-512

下面是我得到的错误

fatal: [172.16.12.70]: FAILED! => {
    "msg": "The task includes an option with an undefined variable. The error was: 'password' is undefined\n\nThe error appears to be in '/tmp/awx_56_skpglhjz/project/user_creation_AWXold.yml': line 17, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n  tasks:\n  - name: \"Create user accounts\"\n    ^ here\n"
}
4

2 回答 2

0

只需像这样定义您的额外变量:

---
username: testuserxx
groups: nginx,mysql
password: $6$UxUZNNsVnWUSM9ge$0viDFuXUwtLYyx/U4tFmX8w3TknD3C19Oab.clfi67WGGqJUDrkGpMOvHE1zwudXyKl0gmqAw8piR8KFGi55R/
于 2019-10-11T22:06:56.670 回答
0

您可以尝试为组和密码提供报价吗?

groups: 'nginx,mysql'
于 2019-10-10T10:02:10.077 回答