英文:
How to loop through a list of variables set-up in an Ansible inventory file?
问题
以下是翻译后的内容:
拥有这样的 Ansible 主机清单文件:
hostname1 users=user1,user2,user3
hostname2 users=user1,user2
我需要一个 Ansible playbook,它将循环遍历每个用户变量列表,并针对每个主机/用户变量运行 grep username /etc/passwd
命令。
到目前为止,我有以下内容:
---
- name: Grep shadow entries for users
hosts: all
tasks:
- name: Get shadow entry for each user
shell: "grep {{ item }} /etc/shadow"
register: shadow_entry
with_items: "{{ hostvars[inventory_hostname].users }}"
- name: Print shadow entry
debug:
msg: "{{ shadow_entry.results }}"
但这不会循环遍历每个用户列表,而是运行 grep [user1,user2,user3] /etc/passwd
。
我还尝试了不同的设置清单文件的变化,如:
hostname1 users=["user1","user2","user3"]
hostname1 users='["user1","user2","user3"]'
hostname1 users="user1","user2","user3"
似乎什么都不起作用。
英文:
Having an ansible inventory file like this:
hostname1 users=user1,user2,user3
hostname2 users=user1,user2
I need an Ansible playbook that will loop through each of the user variable list and run the grep username /etc/passwd
command for each hostname/user variable
What I have so far is:
---
- name: Grep shadow entries for users
hosts: all
tasks:
- name: Get shadow entry for each user
shell: "grep {{ item }} /etc/shadow"
register: shadow_entry
with_items: "{{ hostvars[inventory_hostname].users }}"
- name: Print shadow entry
debug:
msg: "{{ shadow_entry.results }}"
But this does not loop through each user list but rather goes and runs grep [user1,user2,user3] /etc/passwd
.
Also tried different variations on setting the inventory file like:
hostname1 users=["user1","user2","user3"]
hostname1 users='["user1","user2","user3"]'
hostname1 users="user1","user2","user3"
Nothing seems to work.
答案1
得分: 1
以下是已翻译的内容:
如何循环遍历在Ansible清单文件中设置的变量列表?
对于一个名为 cat hosts
的文件
[test]
test.example.com USERS="['user1','user2','user3']"
一个示例的Playbook
---
- hosts: test
become: false
gather_facts: false
tasks:
- name: 显示用户
debug:
msg: "{{ item }}"
loop: "{{ hostvars[inventory_hostname].USERS }}"
- name: 显示用户
debug:
msg: "{{ item }}"
loop: "{{ USERS }}"
将都产生以下输出:
任务 [显示用户] ***********************
ok: [test.example.com] => (item=user1) =>
msg: user1
ok: [test.example.com] => (item=user2) =>
msg: user2
ok: [test.example.com] => (item=user3) =>
msg: user3
类似的问答
请注意,建议通过getent
模块 - unix getent实用程序的包装器完全读取帐户数据库。
针对其各种数据库之一运行
getent
,并将信息返回到主机的事实中,以getent_
为前缀的变量
查看问答
英文:
How to loop through a list of variables set-up in an Ansible inventory file?
For a file cat hosts
[test]
test.example.com USERS="['user1','user2','user3']"
an example playbook
---
- hosts: test
become: false
gather_facts: false
tasks:
- name: Show users
debug:
msg: "{{ item }}"
loop: "{{ hostvars[inventory_hostname].USERS }}"
- name: Show users
debug:
msg: "{{ item }}"
loop: "{{ USERS }}"
will both result into an output of
TASK [Show users] ***********************
ok: [test.example.com] => (item=user1) =>
msg: user1
ok: [test.example.com] => (item=user2) =>
msg: user2
ok: [test.example.com] => (item=user3) =>
msg: user3
Similar Q&A
- How specify a list value as variable in Ansible inventory file?
- Ansible: How to define a list in host inventory?
Please take note that it is recommended to proceed further with reading the account database fully via getent
module – A wrapper to the unix getent utility.
> Runs getent
against one of it’s various databases and returns information into the host’s facts, in a getent_
prefixed variable
See Q&A
答案2
得分: 1
- 在第一种情况下
hostname1 users=user1,user2,user3
如果要迭代列表,请分割项目
- debug:
msg: "{{ item }}"
loop: "{{ users.split(',') }}"
得到
ok: [hostname1] => (item=user1) =>
msg: user1
ok: [hostname1] => (item=user2) =>
msg: user2
ok: [hostname1] => (item=user3) =>
msg: user3
- 在第二种情况下
hostname2 users=[user1,user2,user3]
字符串是有效的YAML列表。使用过滤器from_yaml将字符串转换为列表
- debug:
msg: "{{ item }}"
loop: "{{ users|from_yaml }}"
得到相同的结果
ok: [hostname2] => (item=user1) =>
msg: user1
ok: [hostname2] => (item=user2) =>
msg: user2
ok: [hostname2] => (item=user3) =>
msg: user3
- 如果在字符串中加入空格,您必须引用该字符串,可以使用单引号或双引号
hostname2 users='[user1, user2, user3]'
- 使用模块getent读取*/etc/shadow*。在远程系统上,查看
man getent
以确定数据库是否可用
- getent:
database: shadow
此模块将在变量ansible_facts.getent_shadow中创建一个字典。您可以使用它来迭代users,例如,给定users
hostname2 users=operator,nobody
下面的任务
- debug:
msg: "{{ item }}: {{ ansible_facts.getent_shadow[item]|to_yaml }}"
loop: "{{ users.split(',') }}"
得到
ok: [hostname2] => (item=operator) =>
msg: |- operator: ['*', '18397', '0', '99999', '7', '', '', '']
ok: [hostname2] => (item=nobody) =>
msg: |- nobody: ['*', '18397', '0', '99999', '7', '', '', '']
英文:
- Both options below are strings
hostname1 users=user1,user2,user3
hostname2 users=[user1,user2,user3]
You can test it
- debug:
var: users|type_debug
gives in both cases
users|type_debug: str
See Defining variables in INI format
- In the first case
hostname1 users=user1,user2,user3
split the items if you want to iterate the list
- debug:
msg: "{{ item }}"
loop: "{{ users.split(',') }}"
gives
ok: [hostname1] => (item=user1) =>
msg: user1
ok: [hostname1] => (item=user2) =>
msg: user2
ok: [hostname1] => (item=user3) =>
msg: user3
- In the second case
hostname2 users=[user1,user2,user3]
the string is a valid YAML list. Use the filter from_yaml to convert the string to the list
- debug:
msg: "{{ item }}"
loop: "{{ users|from_yaml }}"
gives the same result
ok: [hostname2] => (item=user1) =>
msg: user1
ok: [hostname2] => (item=user2) =>
msg: user2
ok: [hostname2] => (item=user3) =>
msg: user3
- You have to quote the string, either single or double if you put spaces into it
hostname2 users='[user1, user2, user3]'
<hr>
- Use module getent to read /etc/shadow. On the remote system, see
man getent
whether the database is available or not
- getent:
database: shadow
This module will create a dictionary in the variable ansible_facts.getent_shadow. You can use it, instead of grep, to iterate users. For example, given the users
hostname2 users=operator,nobody
The task below
- debug:
msg: "{{ item }}: {{ ansible_facts.getent_shadow[item]|to_yaml }}"
loop: "{{ users.split(',') }}"
gives
ok: [hostname2] => (item=operator) =>
msg: |-
operator: ['*', '18397', '0', '99999', '7', '', '', '']
ok: [hostname2] => (item=nobody) =>
msg: |-
nobody: ['*', '18397', '0', '99999', '7', '', '', '']
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论