如何将迭代循环列表转换为字符串以在Linux中使用 我有一个我想重复的角色。
目标是从列表中创建新的用户帐户。剧本调用角色并发送列表进行迭代 操作系统(LinuxDebian8.8)看到了所有的varUnicode“[u'user']” 执行的一些其他测试显示新用户: [“测试”] [u'test'] 我真正想要的就是让var成为一个字符串,这样我就可以创建一个新用户并添加所需的键和其他文件。我还可以将var加入密钥和其他文件的路径中 我一直在寻找一种简单的方法来“| to_string”(不是Ansible)如何将迭代循环列表转换为字符串以在Linux中使用 我有一个我想重复的角色。,linux,ssh,ansible,Linux,Ssh,Ansible,目标是从列表中创建新的用户帐户。剧本调用角色并发送列表进行迭代 操作系统(LinuxDebian8.8)看到了所有的varUnicode“[u'user']” 执行的一些其他测试显示新用户: [“测试”] [u'test'] 我真正想要的就是让var成为一个字符串,这样我就可以创建一个新用户并添加所需的键和其他文件。我还可以将var加入密钥和其他文件的路径中 我一直在寻找一种简单的方法来“| to_string”(不是Ansible) 过滤器“to_yaml”去掉unicode而不是“[]”,并
过滤器“to_yaml”去掉unicode而不是“[]”,并在末尾添加“\n” ssh密钥副本的项,如果用于各种id_uz(type).pub文件 我读过: 代码 一个不同的形式,但仍然错误如下
roles:
- role: common
with_items:
- "{{ UserList }}"
错误
(item=id_rsa.pub)=>{“failed”:true,“invocation”:{“module_args”:{“dest”:“/home/[u'test']/.ssh/id_rsa.pub”,“force”:false,“mode”:600,“src”:“/home/[u'test']/.ssh/id_rsa.pub”},“module_name:“copy”},“item”:“id_rsa.pub”,“msg”:“找不到src=/home/[u'test'/.ssh]/.ssh/.id_rsa.pub”解决方法
我找到了解决问题的方法。我会谨慎地称之为解决方案。在这种情况下就足够了。我需要使用内置的{{item}来“循环”我的var。然后{item}}被用作字符串,我可以创建我需要的路径。我还可以使用“with_nested”遍历一系列项
“用户”是另一个测试运行的异常。我使用using test重新运行并更新了错误消息?[我确实想介绍更多的用户]。我确实从文档中看到了这段代码:
角色:-角色:foo变量:message:“first”-{role:foo,变量:{message:“second”}
那么我应该设置-角色:\n角色:common\n变量:User:{{UserList}
然后在角色中使用{User}?尝试了不同的角色网络格式:roles:\-role:common\with\u items:\-“{{UserList}”
没有更改,我仍然得到“[u'user']”作为变量。我还使用{UserList}进行了测试,没有引号和{},仍然没有效果。嗯,Richard,请不要在评论中添加代码,因为它不可能被阅读;请更新它
UserList file
---
UserList:
- 'test'
...
role/common/main.yml
---
- name: Add user to server
user:
name: "{{ UserList }}"
shell: /bin/bash
- name: make direcotry
file:
path: "/home/{{ UserList }}/.ssh"
state: directory
- name: Copy ssh public key to user/.ssh/_key_.pub
copy:
src: "/home/{{ UserList }}/.ssh/{{ item }}"
dest: "/home/{{ UserList }}/.ssh/{{ item }}"
mode: 600
force: no
with_items:
- id_rsa.pub
- id_dsa.pub
- id_ecdsa.pub
...
roles:
- role: common
with_items:
- "{{ UserList }}"
- name: create empty file
file:
path: "{{ '/home/' + item + '/.ssh/authorized_keys' }}"
state: touch
with_items:
- "{{ UserList }}"
- name: Copy ssh public key to user/.ssh/_key_.pub
copy:
src: "{{ '/home/' + item[1] + '/.ssh/' + item[0] }}"
dest: "{{ '/home/' + item[1] + '/.ssh/' + item[0] }}"
mode: 600
force: no
with_nested:
- [ 'id_rsa.pub' , 'id_dsa.pub' , 'id_ecdsa.pub' ]
- "{{ UserList }}"