一、检查环境
系统版本
[root@ansible-master ~]#cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
Ansible版本
[root@ansible-master ~]# ansible --version ansible 2.7.10
config file = /etc/ansible/ansible.cfg
configured module search path = [u’/root/.ansible/plugins/modules’, u’/usr/share/ansible/plugins/modules’]
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Nov 20 2015, 02:00:19) [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)]
检查环境是否安装python
[root@ansible-master ~]#python -V
Python 2.7.5
配置yum源
yum install -y epel-release
在需要的主机上做免密登录!
二、安装Ansible
使用yum安装ansible
yum install -y ansible
进行测试
ansible -C -i /etc/ansible/hosts 192.168.1.1. -m shell -a "ls -la /"
如果可以看到目的主机根目录下的文件夹,表示ansible已经可以正常使用。
三、Ansible简单命令介绍
检查主机的连通性
ansible all -m ping
重启主机
ansible -i /etc/ansible/hosts hostname -a "/sbin/reboot"
传输文件
ansible all -m copy -a "src=my file dest=host flie"
yum包管理
ansible all -m yum -a "yum命令"
基本命令都可以通过模板shell来完成,
ansible all - m shell -a "shell command"
四:使用Playbook批量操作添加多主机
添加主机和主机相关变量
cat test.ini
[test-client]
192.168.1.1
192.168.2.2.
[test-client:vars]
Variable = test_variable
(test-client的主机的变量)
[all:vars]
ansible-user=root
ansible-ssh-pass=123456789
编写用户具体操作步骤
cat test-playbook.yml
- hosts : test-client
gather_facts: false
vars :
touch_file : devops.file (变量名 : 值)
tasks :
- name : touch file (名字)
shell: “touch /tmp/{{touch_file}}”(模板 :命令)
使用playbook的-i 参数调用ini文件
ansible-playboos -i test.ini test-playbook.yml
如有错误烦请各位大牛多多指正,感谢各位!