首页 > 其他分享 >Ansible—Inventory主机清单

Ansible—Inventory主机清单

时间:2022-09-29 12:00:07浏览次数:46  
标签:python ping ansible host2 webserver Ansible Inventory 清单 ssh

含义

清查;存货清单;财产目录;主机清单

1、增加主机组

官方链接
	http://docs.ansible.com/ansible/intro_inventory.html#
    
vi /etc/ansible/hosts
## db-[99:101]-node.example.com
用中括号[]扩起来,中括号里添加信息为  组名(可自定义)
[webserver]
host1
host2

 

测试

[root@ansible ~]# ansible webserver -m ping -o
host2 | UNREACHABLE!: Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
host1 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
[root@ansible ~]#

host2失败原因:因为未对host2主机进行配置免密登陆

2、增加用户名 密码

[root@ansible ~]# vi /etc/ansible/hosts
## db-[99:101]-node.example.com
[webserver]
host1
host2 ansible_ssh_user='root' ansible_ssh_pass='123'

测试为全部成功

[root@ansible ~]# ansible webserver -m ping -o
host1 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
host2 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}

另一种方法

vi /etc/ansible/hosts
## db-[99:101]-node.example.com
[webserver]
host[1:2]
host3 ansible_ssh_port='2222'
[1:2]指的是host主机1和主机2
host3指定了sshd端口号,不指定会执行失败

 

3、增加端口

这里我把host2的sshd程序端口修改为‘2222’进行测试

[root@ansible ~]# ansible webserver -m ping -o
host2 | UNREACHABLE!: Failed to connect to the host via ssh: ssh: connect to host host2 port 22: Connection refused
host1 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
[root@ansible ~]#
失败,因为默认端口已更改

 

配置文件下指定host2端口

vi /etc/ansible/hosts
## db-[99:101]-node.example.com
[webserver]
host1
host2 ansible_ssh_user='root' ansible_ssh_pass='123' ansible_ssh_port='2222'
增加端口信息
[root@ansible ~]# ansible webserver -m ping -o
host1 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
host2 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
成功

4、组:变量

ansible内部变量可以帮助我们简化主机清单的设置

vi /etc/ansible/hosts
[webserver]
host[1:3]
[webserver:vars]           vars指定是‘变量的意思’
ansible_ssh_user='root'
ansible_ssh_pass='123'

常用变量:

5、子分组

将不同的分组进行组合

vim /etc/ansible/hosts
[nginx]
host[1:2]
[apache]
host3
[webserver:children]                 children内置变量,可以理解定义一个组叫webserver
nginx
apache
[webserver:vars]
ansible_ssh_user='root'
ansible_ssh_pass='123'

ansible webserver -m ping -o       这里的webserver指的是组名

 

6、自定义主机列表

创建一个文件名字为hostlist
vi hostlist
[dockers]
host1
host2
[dockers:vars]
ansible_ssh_user='root'
ansible_ssh_pass='123'
ansible -i  hostlist dockers  -m ping  -o
ansible -i 绝对路径 dockers -m ping -o

-i 指的是链接外部主机清单,后边应该加上绝对路径(如果本身就在文件目录下直接指定’文件名‘即可)
hostlist 文件名
dockers  主机组  组名

标签:python,ping,ansible,host2,webserver,Ansible,Inventory,清单,ssh
From: https://www.cnblogs.com/Jqazc/p/16739173.html

相关文章

  • ansible 坐ci
    1,gitlab(100)安装ansiblerpm-Uvhhttp://mirrors.ustc.edu.cn/epel/epel-release-latest-7.noarch.rpmyuminstallepel-release-yyuminstallansible-y2,部署机......
  • ansible启动/停止tomcat
    1、主程序catsend.yaml----name:"tomcat发布war包"hosts:tomcat_modulevars_files:vars_module/war_module.yamlgather_facts:notasks:-name:"war包部......
  • 通过leangoo建立你的清单管理系统
    ​不知道有没有同样的困扰,在浏览和整理任务工作清单时,希望能够在一块电子大白板上按顺序将各个列表排列出来。这样更简洁更透明,也更清楚。告诉朋友这个想法的时候,朋友推......
  • ansible 二进制安装docker
     首先,上传文件docker-20.10.9.tgz到/data/docker/下 1、编辑docker.service文件docker的配置文件vim/data/docker/docker.service【[Unit]Description=DockerA......
  • ansible register
    目录ansibleregister实例1ansibleregisteransibleregister这个功能非常有用。当我们需要判断对执行了某个操作或者某个命令后,如何做相应的响应处理(执行其他ansible......
  • ansible template
    目录ansibletemplatetemplate介绍实例实例1实例2实例3实例4ansibletemplatetemplate介绍Jinja是基于Python的模板引擎。template类是Jinja的另一个重要组件,可以看作一......
  • ansible 二进制安装mysql
    1、编辑mysql.sh脚本vimmysql.sql【#/bin/bash#脚本安装mysql,上传安装包至/rootcd/root#安装日志mysql_log=/root/mysql.log#mysql安装包名mysql_package=mysql-8......
  • ansible 修改/etc/hosts
    1、编辑文件hosts.tmlvim hosts.tml【----hosts: 192.168.59.103 remote_user:root tasks:  -name:addhosts   lineinfile:name=/etc/hostslin......
  • ansible 安装jdk
    1、上传文件到ansible端上/data/jdk/jdk-8u341-linux-x64.tar.gz(下载地址:)2、编辑jdk.yml文件vimjdk.yml【----hosts:192.168.59.103 remote_user:root tasks......
  • ansible 编译安装nginx
    1、准备安装包nginx-1.22.0.tar.gz(地址:https://nginx.org/download/)2、编写剧本vimnginx.yml【----hosts:192.168.59.103 remote_user:root tasks:  -n......