简介

  • ansibleinventory是一个静态的ini文件,可以使用子组的方式记录列出所有被管理节点机器的清单, 默认配置文件路径/etc/ansible/hosts,当然,你也可以使用-i 选项在命令行中指定其他清单文件

inventory (INI格式)示例

[vagrant@controller my_ansible_dir]$ cat /etc/hosts | tail -n 3
192.168.56.6 node1
192.168.56.7 node2
192.168.56.4 node3
  • 没有配置的小伙伴可以直接使用IP也是可以的(inventory.ini)
node1  ansible_connection=ssh ansible_user=vagrant ansible_ssh_pass=vagrant
node2  ansible_connection=ssh ansible_user=vagrant ansible_ssh_pass=vagrant
node3  ansible_connection=ssh ansible_user=vagrant ansible_ssh_pass=vagrant
[vagrant@controller inventory]$ pwd
/data/my_ansible_dir/inventory
[vagrant@controller inventory]$ ansible all -m ping -i inventory.ini 
node3 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    }, 
    "changed": false, 
    "ping": "pong"
}
node2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
node1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
  • 当然我们也可以对单一的节点服务器使用 ping命令检查服务器存活
[vagrant@controller inventory]$ ansible node1 -m ping -i inventory.ini 
node1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
  • 亦或者我们可以对节点服务器进行分组以及按分组检查服务器存活(inventory.ini)
[web1]
node1  ansible_connection=ssh ansible_user=vagrant ansible_ssh_pass=vagrant
node2  ansible_connection=ssh ansible_user=vagrant ansible_ssh_pass=vagrant
[web2]
node3  ansible_connection=ssh ansible_user=vagrant ansible_ssh_pass=vagrant
[vagrant@controller inventory]$ ansible web1 -m ping -i inventory.ini 
node2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
node1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
[vagrant@controller inventory]$ ansible web2 -m ping -i inventory.ini 
node3 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    }, 
    "changed": false, 
    "ping": "pong"
  • 我们还可以使用类似正则表达式的方法来配置节点服务器清单
# 假如我们web1分组有3个节点服务器
[web1]
node[1:3]  ansible_connection=ssh ansible_user=vagrant ansible_ssh_pass=vagrant

免密登陆

  • 配置管理节点免密登陆,设置用于节点鉴权的SSH密钥
  • 密码写到inventory.ini 容易泄露,为了安全考虑,一般会采用密钥验证方式登录主机。通过证书签名达到 ssh无密码访问。使用 ssh-keygen 与 ssh-copy-id 来实现快速证书的生成及公钥下发。
  • 其实ansible自动化运维工具环境准备 这一篇文章中,我们已经通过脚本实现了免密登陆,这里我再单独拿出来做个简单的演示。
ssh-keygen 或者 ssh-keygen -t rsa -b 2048
ssh-copy-id vagrant@node1
ssh-copy-id vagrant@node2
ssh-copy-id vagrant@node3
  • 如果感觉需要交互麻烦的话可以使用脚本expect工具免交互式执行
  • 需要安装expect工具 sudo yum -y install expect 或者 sudo apt update && sudo apt-get -y install expect
  • 需要了解expect工具的可以移步 Linux之expect工具免交互式shell脚本执行 查看
touch ssh_key.sh && chmod +x ssh_key.sh
./shh_key.sh node1 node2 node3
  • 脚本内容
#!/usr/bin/sh

run_ssh_keygen(){
        rm -rf $rsa_pub
        /usr/bin/expect<<EOF
        set timeout 10
        spawn ssh-keygen -t rsa -b 2048
        expect {
               "Enter file in" {send "\n"; exp_continue}
               "Overwrite (y/n)" {send "y\n"; exp_continue}
               "Enter passphrase" {send "\n"; exp_continue}
               "passphrase again" {send "\n"; exp_continue}
           }
EOF
}

send_ssh_key(){
        pwd=vagrant
        /usr/bin/expect<<EOF
        set timeout 30
        spawn ssh-copy-id vagrant@$1
        expect {
              "connecting (yes/no)?" {send "yes\n"; exp_continue}
              "password:" {send "$pwd\n"; exp_continue}
        }
EOF
}

rsa_pub=$HOME/.ssh/id_rsa.pub

if [ ! -f $rsa_pub ]; then
   run_ssh_keygen
fi

nodes=${@:-node1 node2 node3}

if [ -f $rsa_pub ]; then
   for node in $nodes
   do
     send_ssh_key $node
   done
fi
  • 修改 inventory.ini
[web1]
node1  ansible_connection=ssh
node2  ansible_connection=ssh
[web2]
node3  ansible_connection=ssh
  • 使用 ping命令检查服务器存活
[vagrant@controller inventory]$ ansible all -m ping -i inventory.ini 
node3 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    }, 
    "changed": false, 
    "ping": "pong"
}
node2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
node1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
Last modification:July 22, 2022
如果觉得我的文章对你有用,请随意赞赏