AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 760399
Accepted
hookenz
hookenz
Asked: 2016-02-29 20:08:00 +0800 CST2016-02-29 20:08:00 +0800 CST 2016-02-29 20:08:00 +0800 CST

如何为 ansible 清单中的主机设置配置主机文件?

  • 772

我已经开始学习和使用 ansible 来配置我的登台和生产服务器。我想做的一件事是通过清单文件配置 /etc/hosts 。

看来这是可能的。这是一种这样的用法: https ://gist.github.com/rothgar/8793800

但是,我对 Ansible 有点陌生,我不明白。有人可以用简单的英语解释我如何使它在实践中工作吗?

例如,如果我的库存文件包含。

[compute]
1.2.3.4 
5.6.7.8

[db]
2.3.4.5
6.7.8.9
10.11.12.13

[all]
compute
db

[all:vars]
...

我想说的是,运行剧本后我的主机文件包含

2.3.4.5 db1
6.7.8.9 db2
10.11.12.13 db3
1.2.3.4 compute1
5.6.7.8 compute2

这可能吗?

ansible
  • 2 2 个回答
  • 6200 Views

2 个回答

  • Voted
  1. Best Answer
    larsks
    2016-03-01T10:24:46+08:002016-03-01T10:24:46+08:00

    您可以从模板生成主机条目。遍历组列表,丢弃类似alland的组ungrouped,然后遍历每个组中的主机列表:

    {# this loops over the list of groups.  inside the loop #}
    {# "group" will be the group name and "hosts" will be the #}
    {# list of hosts in that group. #}
    {% for group,hosts in groups.items() %}
    
    {# skip the "all" and "ungrouped" groups, which presumably #}
    {# you don't want in your hosts file #}
    {% if group not in ["ungrouped", "all"] %}
    
    {# generate a hosts entry for each host, using the "loop.index" #}
    {# variable and the group name to generate a unique hostname. #}
    {% for host in hosts %}
    {{host}} {{group}}{{loop.index}}
    {% endfor %}
    
    {% endif %}
    {% endfor %}
    

    以上{{host}}用于IP地址,因为这让我可以在我的系统上测试它,但你可能更喜欢{{hostvars[host]['ansible_default_ipv4']['address']}}在真实环境中,除非你确定你总是在你的库存中使用IP地址。

    • 5
  2. Reza Farshi
    2017-02-26T18:54:39+08:002017-02-26T18:54:39+08:00

    在库存文件中:

      [myhosts]
        192.168.29.2 host_name=host1
        192.168.29.3 host_name=host2
    

    在 playbook.yaml

        - name: Update /etc/hosts from inventory
      lineinfile: dest=/etc/hosts regexp='.*{{item}}$' line='{{hostvars[item]['ansible_eth1']['ipv4']['address'] }}   {{hostvars[item]['host_name']}} ' state=present
      with_items: '{{groups.myhosts}}'
    
    • 0

相关问题

  • Ansible 剧本,使用选项 --disabled-login 创建用户

  • ansible playbook,操作系统不同的用户

  • ansible:为什么文件模块会跳过?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve