ansible hosts配置

编辑/etc/ansible/hosts
添加本机的public SSH key到目标机器的authorized_keys #ssh-copy-id
添加本机的私钥到Ansible
运行ansible all -m ping 测试是否添加成功
Inventory 分组
Ansible可同时操作属于一个组的多台主机,组和主机之间的关系通过inventory文件配置,默认文件路径为/etc/ansible/hosts

常用参数配置:
ansible_ssh_host # 目标主机地址
ansible_ssh_port # 目标主机端口,默认22
ansible_ssh_user # 目标主机用户
ansible_ssh_pass # 目标主机ssh密码
ansible_sudo_pass # sudo密码
ansible_sudo_exe
ansible_connection # 与主机的连接类型,比如:local,ssh或者paramiko
ansible_ssh_private_key_file # 私钥地址
ansible_shell_type # 目标系统的shell类型
ansible_python_interpreter # python版本

格式:[组名]
例如 :
  [test]   # 组名
  10.0.0.1  # 主机ip 或者10.0.0.1:65522 自定义端口
别名
s1 ansible_ssh_port=65522 ansible_ssh_host=10.0.0.1 ansible_ssh_user=simon   # 别名s1

连续的主机
  [g1]
  g[1:50].example.com
  g[a-f].example.com

#hosts简单配置实例
[all:vars]         # *:vars 块变量,all:vars 全局变量
ansible_ssh_private_key_file=/root/.ssh/id_rsa
ansible_ssh_port=22
ansible_ssh_user=root

[t3:vars]  # t3 使用python解释器是python2
ansible_python_interpreter=/usr/bin/python2
nginx_port=80               # 私有变量在playbooks中使用

[t3]
192.168.11.162

[k8s:children]  # 使用children 定义主机组之间继承关系
master
node

[master]
k8s-master01 ansible_ssh_host=192.168.11.101
k8s-master01 ansible_ssh_host=192.168.11.102

[node]
k8s-node01 ansible_ssh_host=192.168.11.105
k8s-node01 ansible_ssh_host=192.168.11.106

Inventory 分文件管理(文件名即是组名)

分文件:

/etc/ansible/group_vars/vim

      /etc/ansible/group_vars/webservers

      /etc/ansible/host_vars/foosball

      vim 文件中的内容: 变量

          ntp_server: [acme.example.org](http://acme.example.org/)

          db_server: 10.0.0.1

分文件夹:(文件夹就是组名)

/etc/ansible/group_vars/vim/install

      /etc/ansible/group_vars/vim/init

      /etc/ansible/host_vars/foosball
#!/usr/bin/env python2
# _*_coding:utf-8_*_

"""
@Time :    2019/11/8 15:54
@Author:  Simon
@File: get_ansible_hosts.py
@Software: PyCharm
"""

try:
    from ansible.parsing.dataloader import DataLoader
    from ansible.inventory.manager import InventoryManager
    from ansible.vars.manager import VariableManager
except ImportError as e:
    print("请安装ansible模块, 安装方式pip install ansible")


def main(sources):
    """
    # 获取ansible hosts 列表
    :param sources:
    :return:
    """
    hosts_list = []
    loader = DataLoader()
    inventory = InventoryManager(loader=loader, sources=sources)
    variable_manager = VariableManager(loader=loader, inventory=inventory)
    get_groups_dict = inventory.get_groups_dict()
    host_list = inventory.hosts
    for _host in host_list:
        host = inventory.get_host(hostname=_host)
        extra_vars = variable_manager.get_vars(host=host)
        extra_vars_group = extra_vars.get("groups")
        ans_port = extra_vars.get("ansible_ssh_port") if extra_vars.get("ansible_ssh_port") else extra_vars.get(
            "ansible_port")
        ans_host = extra_vars.get("ansible_ssh_host") if extra_vars.get("ansible_ssh_host") else extra_vars.get(
            "ansible_host")
        hosts_list.append({
            "hostname": extra_vars.get("inventory_hostname"),
            "host": ans_host,
            "port": ans_port if ans_port else 22,
            "group_name": extra_vars.get("group_names")
        })

    return hosts_list


if __name__ == "__main__":
    sources = ["/etc/ansible/hosts"]
    print(main(sources))

Python 获取host列表

文章来源于(做了点修改··,以及测试确认):ansible hosts配置

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

少陽君

谢谢老板的拿铁

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值