Ansible:混乱的变量?

2024-04-26 03:57:11 发布

您现在位置:Python中文网/ 问答频道 /正文

TASK [deploy_aker : Copy SSH key L2 users to host] ****************************************************************************************************************************************************************
fatal: [test.florius.com]: FAILED! => {"changed": false, "checksum": "cfcc6cdae818178456c4eca8e89ebce0c14ec91b", "msg": "Destination directory /home/[u'test1', u'test2', u'test3', u'test4']/.ssh does not exist"}

它试图创建一个名为[u'test1', u'test2', u'test3', u'test4']的目录,当然最好是/home/test1/等等。。。 我做错了什么:

- name: Copy SSH key L2 users to host
  copy:
    src: files/L2.pub
    dest: /home/{{ l2_users }}/.ssh/authorized_keys
    mode: 600

我试着引用我的变量,比如'{{ l2_users }}',但是我的目录被引用了。。。 我做错了什么,我该如何纠正?你知道吗

我使用{{ l2_users }}将用户添加到主机,这很好,但是复制文件会出错。。。你知道吗

谢谢你!你知道吗

编辑:我的VARS文件,以防它提供线索:

---
l2_users:
  - test1
  - test2
  - test3
  - test4

Tags: tokey目录hosthomeuserssshcopy
1条回答
网友
1楼 · 发布于 2024-04-26 03:57:11

您可以循环列表并将密钥复制到主目录中

示例:

- name: Copy SSH key L2 users to host
  copy:
    src: files/L2.pub
    dest: /home/{{ item }}/.ssh/authorized_keys
    mode: 600
  with_items: l2_users

但是我建议使用authorized_key模块来获得最佳实践,而不是复制密钥。你知道吗

- name: Set authorized key took from file
  authorized_key:
    user: '{{ item }}'
    state: present
    key: "{{ lookup('file', '/path/of/id_rsa.pub') }}"
  with_items: l2_users

相关问题 更多 >