简体   繁体   English

Ansible-vault没有正确传递给playbook

[英]Ansible-vault not being passed to playbook correctly

I am trying to use ansible and ansible-vault to provision users. 我正在尝试使用ansible和ansible-vault来配置用户。 This is my current directory structure: 这是我目前的目录结构:

|-- ansible-test.out
|-- group_vars
|-- inventory
|-- roles
|   |-- bsd_sudoers
|   |   |-- tasks
|   |   |-- templates
|   |   |   `-- cde-admins
|   |   `-- vars
|   `-- bsd_users
|       |-- files
|       |-- tasks
|       |   `-- main.yml
|       |-- templates
|       `-- vars
|           `-- all.yml
|-- site.retry
|-- site.yml
`-- vars
    `-- all.yml

This is what I have for my playbook so far: 这是我到目前为止我的剧本:

---
- name: Creating Users
  user:
    name: "{{ item.name }}"
    system : "{{ item.sudoer }}"
    shell: /bin/bash
    password: "{{ item.password }}"
    uid: "{{ item.uid }}"
    home: "{{ item.home }}"
  with_items: users

Here is a sample of what I have in ansible vault: 以下是我在ansible vault中的示例:

---
   vars:
     users:
       - name: 'foo'
         home: '/home/foo'
         key: 'ssh-rsa ....'
         password: '!!'
         bash: '/bin/bash'
         sudoer: yes
         uid: "2049"
         guid: '2049'
         group: admin

When I run the playbook, this error message is always generated: 当我运行playbook时,始终会生成此错误消息:

fatal: [ansible-test]: FAILED! => {"failed": true, "msg": "The task includes an option with an undefined variable. The error was: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'uid'\n\nThe error appears to have been in '/Users/ansible/playbooks/roles/bsd_users/tasks/main.yml': line 2, 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: Creating Users\n  ^ here\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'uid'"}
    to retry, use: --limit @/Users/ansible/playbooks/site.retry

Why is this always happening? 为什么总会发生这种情况? I was under the impression that Ansible would naturally pass the encrypted variables to my playbook but this does not seem to be the case. 我的印象是Ansible自然会将加密的变量传递给我的剧本,但事实并非如此。

As Konstantin mentioned before, you need to include the variable file either as -e @vars/all.yml via the command line, or by adding vars_files into the playbook. 正如Konstantin之前提到的,您需要通过命令行将变量文件包含为-e @vars/all.yml ,或者将vars_files添加到playbook中。 You can also add the all.yml file into the group_vars directory, and it will be picked up automatically. 您还可以将all.yml文件添加到group_vars目录中,它将自动被选中。

Regarding the second error message you are receiving, starting with Ansible 2.4 the recommended way of providing the password at the command line is by using --vault-id . 关于您收到的第二条错误消息,从Ansible 2.4开始,在命令行提供密码的推荐方法是使用--vault-id This can be anything from a path to the id file, to @prompt for Ansible to prompt you for a password, or even the path to a script that returns the password. 这可以是从id文件的路径到Ansible的@prompt以提示输入密码,甚至是返回密码的脚本的路径。

Prior to Ansible 2.4, you need to use --ask-vault-pass or --vault-password-file . 在Ansible 2.4之前,您需要使用--ask-vault-pass--vault-password-file

You can also add a variable in ansible.cfg named vault_password_file , where you can define the location of the vault password file (for example vault_password_file = /path/to/.vault_password_file ), so you don't need to specify it every time you run ansible-playbook . 您还可以在ansible.cfg添加一个名为vault_password_file的变量,您可以在其中定义保管库密码文件的位置(例如vault_password_file = /path/to/.vault_password_file ),因此您不需要每次都指定它运行ansible-playbook Starting with Ansible 1.7, this path can also point to a script that returns the password as stdout. 从Ansible 1.7开始,此路径还可以指向将密码作为stdout返回的脚本。

If this helps answer your question, I would appreciate it if you could mark it as the accepted answer. 如果这有助于回答您的问题,如果您能将其标记为已接受的答案,我将不胜感激。

First: 第一:

with_items: users should be with_items: "{{ users }}" . with_items: users应该是with_items: "{{ users }}"
Bare variables are not supported any more. 不再支持裸变量

Second: 第二:

There's no need to define top-level vars dict in vaulted files, use: 无需在存储文件中定义顶级vars字典,使用:

---
users:
  - name: foo
    home: /home/foo
  - name: bar
    home: /home/bar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM