ansible---基础

ansible
特点:
不需要安装客户端,通过sshd去通信
基于模块工作,模块可以由任何语言开发
不仅支持命令行使用模块,也支持编写yaml格式的playbook
支持sudo
安装:
yum install -y epel-release
yum install -y ansible

配置文件:
vi /etc/ansible/hosts //增加
[test]
127.0.0.1 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass=123456
zhan-02 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass=123456

ansible执行远程命令:
cd /etc/ansible
ansible test -m command -a 'who'
[root@zhan-01 ansible]# ansible test -m command -a 'who'
zhan-02 | SUCCESS | rc=0 >>
root pts/0 2018-02-03 16:00 (192.168.88.123)
root pts/1 2018-02-03 16:19 (192.168.88.123)
root pts/2 2018-02-03 17:45 (zhan-01)

127.0.0.1 | SUCCESS | rc=0 >>
root pts/0 2018-02-03 16:00 (192.168.88.123)
root pts/1 2018-02-03 08:19 (192.168.88.123)
root pts/4 2018-02-03 09:45 (localhost)

test为主机组名,-m后边是模块名字,-a后面是命令。当然我们也可以直接写一个ip,针对某一台机器来执行命令。

执行单个主机命令:
ansible 127.0.0.1 -m command -a 'hostname'
127.0.0.1 | SUCCESS | rc=0 >>
zhan-01

ansible 取消交互:
去除ssh无交互添加known_hosts配置文件
在/etc/ansible/ansible.cfg打开下面注释:
host_key_checking = False

取消ssh的yes和no的交互
修改/etc/ssh/ssh_config文件(或$HOME/.ssh/config)中的配置,添加如下两行配置:
一般为:StrictHostKeyChecking ask
可改为:StrictHostKeyChecking no
UserKnownHostsFile /dev/null
修改好配置后,重新启动sshd服务即可,命令为:/etc/init.d/sshd restart (或 service sshd restart )

原文地址:https://www.cnblogs.com/pythonlx/p/8408680.html