Variables 如何使用ansible找到用户ID并在jinja2模板中使用它?
我必须使用ansible创建大量用户。我在vars部分的ansible play中以列表形式传递用户:Variables 如何使用ansible找到用户ID并在jinja2模板中使用它?,variables,ansible,jinja2,Variables,Ansible,Jinja2,我必须使用ansible创建大量用户。我在vars部分的ansible play中以列表形式传递用户: vars: users: ['user1', 'user2'] 然后,我必须创建一个使用此用户ID作为参数的脚本。脚本中的命令如下所示: blobfuse$1-tmp path=/mnt/resource/{{item}}/-o attr_timeout=240-o entry_timeout=240-o negative_timeout=120-o uid=$USER_ID-o
vars:
users: ['user1', 'user2']
然后,我必须创建一个使用此用户ID作为参数的脚本。脚本中的命令如下所示:
blobfuse$1-tmp path=/mnt/resource/{{item}}/-o attr_timeout=240-o entry_timeout=240-o negative_timeout=120-o uid=$USER_ID-o allow_other-container name={{item}}-文件缓存超时秒数=120-config file=/root/connection-{item}.cfg
除uid外,一切正常=
我已尝试使用lookup的管道插件,但无法获得正确的UID:
{{lookup'pipe','grep-w{{item}/etc/passwd | cut-d:-f3'}
我的最终目标是获取每个已创建用户的UID,并将其传递给上面的blobfuse命令。使用id命令和subshell怎么样?然后你可以做类似的事情
blobfuse$1-tmp path=/mnt/resource/{{item}}/-o attr_timeout=240-o entry_timeout=240-o negative_timeout=120-o uid=$id{{item}-o allow_other-container name={item}}-file cache timeout in seconds=120-config file=/root/connection-{item}.cfg
如果您使用的是命令模块,则必须将其替换为shell
编辑:如果您使用的是模板,并且希望使用看起来更干净的查找插件,那么您可以执行类似的操作。这是在本地linux机器上测试的:
模板yaml
易位命令
ansible-m template-i localhost,all-c local-a src=template.yaml dest=result.txt-e{users:[nobody,root]}
result.txt
在您的例子中,错误是在查找中使用了{{item}},您应该只在{{}块中使用变量名和串联
问:获取每个已创建用户的UID
A:模块正是为了这个目的。比如说
-主机:本地主机
变量:
我的用户:['root','admin']
任务:
-格滕:
数据库:passwd
-调试:
味精:
-{{item}}uid:{{getent_passwd[item].1}
-{{item}}gid:{{getent_passwd[item].2}
-{{item}}主页:{{getent_passwd[item].4}
-{{item}}shell:{{getent_passwd[item].5}
循环:{{my_users}}
给予
确定:[localhost]=>item=root=>{
味精:[
根uid:0,
根gid:0,
root home:/root,
根shell:/bin/bash
]
}
确定:[localhost]=>item=admin=>{
味精:[
管理员uid:1002,
管理gid:1002,
管理员主页:/home/admin,
admin shell:/bin/bash
]
}
模块getent自动创建字典getent\u passwd,可在模板中使用。例如模板
shell>cat template.j2
{%对于my_users%}
{{user}}{{getent_passwd[user].1}
{%endfor%}
-模板:
src:template.j2
dest:my_users.txt
给予
shell>cat my_users.txt
根0
管理员1002
我也有同样的问题 不幸的是,查找插件只在控制节点上有用。 换句话说,具有相同uid的用户需要同时位于控制节点和所有受管节点上。否则任务将失败,返回代码为1 对我来说,使用getent模块是一个不错的解决方案。 或者,所有内容实际上都在用户模块的返回值中。我只需要注册它的返回值就可以访问它们 下面的剧本演示了如何在任意后续任务调试和jinja模板中从用户模块的返回值计算uid
{{ ansible_managed | comment }}
{% for item in created_users.results %}
User: uid:{{ item.uid }} name:{{ item.name }} group:{{ item.group }} home:{{ item.home }} shell:{{ item.shell }}
{% endfor %}
-名称:演示如何在用户创建后计算UID
主持人:全部
变成:真的
收集事实:不
变量:
我的用户:
-u1
-u2
-u3
任务:
-名称:在循环中创建用户
用户:
名称:{{item}}
循环:{{my_users}}
注册:创建用户
-名称:显示创建的用户
调试:
msg:User:uid:{{item.uid}}name:{{{item.name}组:{{item.group}主页:{{item.home}}外壳:{{{item.shell}
循环:{created_users.results}
使用loop_control-label抑制项的详细性
回路控制:
标签:{{item.name}
-名称:使用模板
模板:
src:myusers.j2
dest:/tmp/myusers.txt
这是myusers.j2模板
{{ ansible_managed | comment }}
{% for item in created_users.results %}
User: uid:{{ item.uid }} name:{{ item.name }} group:{{ item.group }} home:{{ item.home }} shell:{{ item.shell }}
{% endfor %}
结果如下:
$ ansible-playbook compute-uid.yml
PLAY [Demonstrate How to Compute UID after User Creations] **********************************************************************************************
TASK [Create users in loop] *****************************************************************************************************************************
changed: [ansible1] => (item=u1)
changed: [ansible1] => (item=u2)
changed: [ansible1] => (item=u3)
TASK [Display created users] ****************************************************************************************************************************
ok: [ansible1] => (item=u1) => {
"msg": "User: uid:1001 name:u1 group:1001 home:/home/u1 shell:/bin/bash"
}
ok: [ansible1] => (item=u2) => {
"msg": "User: uid:1002 name:u2 group:1002 home:/home/u2 shell:/bin/bash"
}
ok: [ansible1] => (item=u3) => {
"msg": "User: uid:1003 name:u3 group:1003 home:/home/u3 shell:/bin/bash"
}
TASK [Work with template] *******************************************************************************************************************************
changed: [ansible1]
PLAY RECAP **********************************************************************************************************************************************
ansible1 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
$ ansible all -a "cat /tmp/myusers.txt"
ansible1 | CHANGED | rc=0 >>
#
# Ansible managed
#
User: uid:1001 name:u1 group:1001 home:/home/u1 shell:/bin/bash
User: uid:1002 name:u2 group:1002 home:/home/u2 shell:/bin/bash
User: uid:1003 name:u3 group:1003 home:/home/u3 shell:/bin/bash
我用的是jinja2。上面的blobfuse命令在一个jinja模板中,这就是我需要获取用户id的地方,这是有效的,因为它将使用$id-u user(即用户id)来评估模板中的$id-u{{item}}。这是一个很好的解决方法you@Florin,更新了模板案例的答案,现在看起来更干净了
$ ansible-playbook compute-uid.yml
PLAY [Demonstrate How to Compute UID after User Creations] **********************************************************************************************
TASK [Create users in loop] *****************************************************************************************************************************
changed: [ansible1] => (item=u1)
changed: [ansible1] => (item=u2)
changed: [ansible1] => (item=u3)
TASK [Display created users] ****************************************************************************************************************************
ok: [ansible1] => (item=u1) => {
"msg": "User: uid:1001 name:u1 group:1001 home:/home/u1 shell:/bin/bash"
}
ok: [ansible1] => (item=u2) => {
"msg": "User: uid:1002 name:u2 group:1002 home:/home/u2 shell:/bin/bash"
}
ok: [ansible1] => (item=u3) => {
"msg": "User: uid:1003 name:u3 group:1003 home:/home/u3 shell:/bin/bash"
}
TASK [Work with template] *******************************************************************************************************************************
changed: [ansible1]
PLAY RECAP **********************************************************************************************************************************************
ansible1 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
$ ansible all -a "cat /tmp/myusers.txt"
ansible1 | CHANGED | rc=0 >>
#
# Ansible managed
#
User: uid:1001 name:u1 group:1001 home:/home/u1 shell:/bin/bash
User: uid:1002 name:u2 group:1002 home:/home/u2 shell:/bin/bash
User: uid:1003 name:u3 group:1003 home:/home/u3 shell:/bin/bash