ansible实践-1

 
不需要安装客户端,通过sshd去通信
基于模块工作,模块可以由任何语言开发
不仅支持命令行使用模块,也支持编写yaml格式的playbook
支持sudo
有提供UI(浏览器图形化)www.ansible.com/tower  10台主机以内免费
 
Ansible 安装
两台机器 172.7.15.106  172.7.15.111
只需要在106上安装ansible即可
 
yum install -y epel-release
yum install -y ansible
 
Ansible 配置密钥
 
106上生成密钥对
ssh-keygen -t rsa  直接回车即可,不用设置密钥密码
 
把公钥(id_rsa.pub)内容放到对方机器(111)的/root/.ssh/authorized_keys里面
 
111 上 mkdir /root/.ssh
scp .ssh/id_rsa.pub  172.7.15.111:/root/.ssh/authorized_keys
 
本机也要操作
cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
chmod 600 /root/.ssh/authorized_keys
关闭selinux
setenforce 0
 
Ansible更改配置文件
 
vi  /etc/ansible/hosts  //增加
[testhost]
127.0.0.1
172.7.15.111
 
说明: testhost为主机组名字,自定义的。 下面两个ip为组内的机器ip。
Ansible 远程执行命令
ansible testhost -m command -a 'w'
 
这样就可以批量执行命令了。这里的testhost为主机组名,-m后边是模块名字,-a后面是命令。当然我们也可以直接写一个ip,针对某一台机器来执行命令。
 
ansible 127.0.0.1 -m  command -a 'hostname'
 
错误: "msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!"
解决: yum install -y libselinux-python
 
还有一个模块就是shell同样也可以实现  (shell支持管道)
ansible testhost -m shell -a 'w'
 
ansible机器先配通到 目标节点的免密登录

设置 hosts

[k8s]
192.168.1.1 ansible_ssh_user=root ansible_ssh_pass='passwd'
192.168.1.2  ansible_ssh_user=root ansible_ssh_pass='passwd'
192.168.1.3 ansible_ssh_user=root ansible_ssh_pass='passwd'


ansible k8s -m copy -a "src=/root/.ssh/id_rsa.pub dest=/root/.ssh/authorized_keys"

ansible k8s -m shell -a "cat /root/.ssh/authorized_keys"

修改hosts 配置
[k8s]
10.129.72.10 
10.129.72.11 
10.129.72.12 

ansible k8s -m shell -a "hostname"   测试下免密登录配置是否生效

修改主机名
ansible 192.168.1.1  -m shell -a "hostnamectl set-hostname k8s-master-1 "
ansible 192.168.1.1  -m shell -a "hostname"   检查是否修改成功

  

原文地址:https://www.cnblogs.com/weifeng1463/p/7410187.html