如何告诉ansible在不同的环境中使用不同的用户?(How to tell ansible to use different user for different environment?)

编程入门 行业动态 更新时间:2024-10-24 02:26:55
如何告诉ansible在不同的环境中使用不同的用户?(How to tell ansible to use different user for different environment?)

我正在使用Ansible在ec2上构建一台机器。 但我也有流浪汉在本地测试脚本。

这是我的剧本

- name: Provision ec2 hosts: all user: vagrant sudo: yes gather_facts: true vars: gsutil_user_home: "vagrant"

如果我想将此剧本用于EC2,我必须将user和gsutil_user_home更改为ubuntu以使其正常工作。 无论如何,我可以将其作为一个变量,并根据一些参数告诉Ansible它需要运行哪个用户?

I'm building a machine on ec2 using Ansible. But I also have vagrant to test the script locally.

Here's my playbook

- name: Provision ec2 hosts: all user: vagrant sudo: yes gather_facts: true vars: gsutil_user_home: "vagrant"

If I want to use this playbook for EC2 I have to change the user and gsutil_user_home to be ubuntu to get it to work. Is there anyway I can make that as a variable and tell Ansible which user it needs to run based on some argument?

最满意答案

如果这些值通常与您的剧本持续时间保持一致,您通常会将它们设置为库存中的组/主机变量,并为不同的环境设置不同的库存或组。 但是如果你真的想用vars做它并用-e传递它们,那也没关系:

- name: Provision ec2 hosts: all user: {{ my_ec2_user }} sudo: yes gather_facts: true vars: gsutil_user_home: {{ gsutil_user_home }}

If these values are generally consistent for the duration of your playbooks, you'd usually set them as group/host vars in inventory, and have different inventories or groups for different environments. But if you really want to do it with vars and pass them in with -e, that's fine too:

- name: Provision ec2 hosts: all user: {{ my_ec2_user }} sudo: yes gather_facts: true vars: gsutil_user_home: {{ gsutil_user_home }}

更多推荐

本文发布于:2023-07-22 18:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1222330.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:环境   用户   ansible   user   environment

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!