ansible hosts配置


  1. 编辑/etc/ansible/hosts
  2. 添加本机的public SSH key到目标机器的authorized_keys   #ssh-copy-id
  3. 添加本机的私钥到Ansible
  4. 运行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]     # 组名  
      172.18.0.11  # 主机ip  或者1172.18.0.11:65522 自定义端口(不指明,则使用ansible.cfg文件中“remote_port ”定义的端口,如果没有定义则使用默认的22端口)
别名
    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
 
 
 Inventory 分文件管理(文件名即是组名)
  分文件:
          /etc/ansible/group_vars/ vim
          /etc/ansible/group_vars/ webservers
          /etc/ansible/host_vars/ foosball
          vim 文件中的内容: 变量
              ntp_server:  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

 资料来源:https://www.cnblogs.com/lmx1002/p/8339993.html


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM