简体   繁体   English

是否可以将多个变量分配给单个主机?

[英]Is it possible to assign multiple variables to individual hosts in ansible?

Example. 例。

[main] 192.168.1.1 users="test1, test2" [main] 192.168.1.1 users =“ test1,test2”

This example does not work since this type of var requires a key value. 由于此类型的var需要键值,因此此示例无法正常工作。 But is it possible to do this in anyway? 但是是否有可能这样做呢? And if so what is the correct syntax to use? 如果是这样,使用的正确语法是什么?

For a better look at what I want to do. 为了更好地了解我想做什么。 I have hundreds of users that I need to remove off of hundreds of different servers. 我有数百个用户需要从数百个不同的服务器中删除。 So I want to assign a list of users to each different host. 因此,我想为每个不同的主机分配一个用户列表。 Something like this. 这样的事情。

host1 users={{ list }} host1 users = {{list}}

Or something like that? 或类似的东西?

Is this possible? 这可能吗?

In python you can use dictionaries, which are essentially key-value pairs. 在python中,您可以使用字典,这些字典本质上是键值对。 They are optimized to fetch values based on keys, but not the other way around. 它们经过优化,可根据键来获取值,但并非相反。 So in your case, if you have a list of user data, and want to efficiently assign it to a user, you can set the key to the username, and the value to the list. 因此,在您的情况下,如果您有一个用户数据列表,并且想要有效地将其分配给用户,则可以将键设置为用户名,将值设置为列表。 It would look something like this 看起来像这样

users = {"user1" = ["Bill", "42", "Bald"],
         "user2" = ["Sally", "25", "Red"]}

It might be more efficient to use classes, because you would be able to reference the different attributes, which are essentially things that identify an object of that class. 使用类可能会更有效,因为您将能够引用不同的属性,这些属性本质上是标识该类对象的事物。 So each user would have an attribute name , age , and hair , perhaps. 因此,每个用户可能都有一个属性nameagehair

Classes are a huge topic, so it might be good to read the Python documentation for 2.7 or 3.5 , or just do a quick google search 类是一个巨大的话题,因此最好阅读2.73.5的Python文档,或者只是进行快速的Google搜索

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

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