//首先安装 :
yum install -y epel-release ##首先安装 epel源
yum install ansible -y
//查看版本(检查安装是否成功):
[root@localhost ~]# ansible --version
//主要配置文件:
[root@localhost ~]# tree /etc/ansible/ ##树状结构展示文件夹
/etc/ansible/
├── ansible.cfg ##ansible的配置文件
├── hosts ##ansible的主仓库,用于存储需要管理的远程主机的相关信息
└── roles ##角色
—管理端添加服务端:
[root@localhost ~]# cd /etc/ansible
[root@localhost ansible]# vim hosts
添加组名和对应的ip地址:
[组名]
ip
。。。
ip
1)关闭第一次使用ansible连接客户端时输入命令提示:
sed -i "s@\#host_key_checking = False@host_key_checking = False@g" /etc/ansible/ansible.cfg
指定日志路径:
sed -i "s@\#log_path = \/var\/log\/ansible.log@log_path = \/var\/log\/ansible.log@g" /etc/ansible/ansible.cfg
//配置密钥对验证:
ssh-keygen -t rsa #master生成公钥
vim /etc/ansible/hosts #添加被操控的主机ip
。。。
ansible all -m authorized_key -a “user=root key=‘{undefined{ lookup(‘file’, ‘/root/.ssh/id_rsa.pub’) }}’ path=/root/.ssh/authorized_keys manage_dir=no” --ask-pass -c paramiko #往host里面配置所有主机发送公钥
[root@localhost ansible]# ansible 组名 -m ping #验证通信
10.0.0.32 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
#########成功------分割符############
ansible常用模块
Blockinfile模块:
```bash
在文件末尾插入两行
`[root@tiandong ~]# ansible all -m blockinfile -a "path=/tmp/rc.local block='service restart sshd\nservice restart httpd'`"
报错:
1.No module named ‘paramiko’ (centos20.04)
apt install python3-pip -y
pip install paramiko
ansible剧本技巧:地址
ansible异常:
1.check mode and async cannot be used on same task