ansible 安装
python
源码安装git
git clone git://github.com/ansible/ansible.git --recursivegithub
cd ./ansibleshell
source ./hacking/env-setupcentos
1)若是须要安装pip, 先安装对应python版本的pip:ssh
easy_install pipide
2)安装控制主机须要的Python模块测试
pip install paramiko PyYAML Jinja2 httplib2 sixcentos7
3)更新ansible版本日志
git pull --rebase
git submodule update --init --recursive
yum 安装
1)yum源
centos6:rpm -Uvh http://mirrors.zju.edu.cn/epel/6/x86_64/epel-release-6-8.noarch.rpm
centos7:rpm -Uvh http://mirrors.zju.edu.cn/epel/7/x86_64/e/epel_release-7-5.noarch.rpm
yum install ansible
3.本次为yum 安装:/etc/ansible
配置运行环境
ansible.cfg 配置文件
1)环境变量:export ANSIBLE_SUDO_USER=root
2)配置参数
inventory 资源清单,被管理的主题资源列表配置文件位置
library 代码模块目录,多个目录能够用:隔开
forks 设置ansible最多能有多少个进程同时工做
sudo_user 默认指向命令用户
remote_port 指定连接被管理节点端口,默认22
host_key_checking 默认设置为False;相似第一次ssh时须要进行yes确认初始化known_host
timeout ssh连接的超时间隔
log_path 日志文件配置
Linux主机ssh无密码访问
测试:
$ansible --version
测试主机连通性/etc/ansible/host定义列表
192.168.1.1 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.1.2 | SUCCESS => {
"changed": false,
"ping": "pong"
}
4.在被管理节点批量执行命令
能够新建一个新主机清单,执行一些命令
cat new.cfg
[test]
192.168.1.1
192.168.1.2
$ansible test -m shell -a '/bin/echo hello ansible!' -i inventory.cfg
$ansible test -m command -a "/bin/echo hello ansible" -i new.cfg
192.168.1.1 | SUCCESS | rc=0 >>
hello ansible!
192.168.1.2 | SUCCESS | rc=0 >>
hello ansible!
5.ansible获取帮助信息
ansible-doc -h 帮助信息
ansible-doc -l 列出支持的模块
ansible-doc -s+ 直接加模块命令,显示模块描述和使用示例:ansible-doc -s yum
执行ansible命令时在命令后加-vvv能够得到详细输出结果
警告:
[WARNING]: log file at /var/log/ansible.log is not writeable and we cannot create it, aborting
将/var/log/ansible.log 加上写权限就行
chmod +666 /var/log/ansible.log