配置ansible环境
1.安装软件:
ansible-2.7.8-1.el7.noarch.rpm
libtomcrypt-1.17-25.el7.x86_64.rpm
libtommath-0.42.0-5.el7.x86_64.rpm
python2-crypto-2.6.1-13.el7.x86_64.rpm
python2-jmespath-0.9.0-1.el7.noarch.rpm
python-httplib2-0.9.2-0.1.el7.noarch.rpm
python-keyczar-0.71c-2.el7.noarch.rpm
python-paramiko-2.1.1-0.9.el7.noarch.rpm
sshpass-1.06-1.el7.x86_64.rpm
python版本为2.7,此时缺少epel-release安装包
wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm ##下载epel包
yum install -y epel-release-6-8.noarch.rpm ##安装
rpm -qc ansible ##查看ansible文件
ansible --version
2.创建普通用户devops,用普通用户进行操作
useradd devops
su - devops
mkdir ansible
cd ansible/
vim ansible.cfg
[defaults]
inventory = ./inventory
[privilege_escalation] #默认sudo到root用户,不用加 -b
become=True
become_method=sudo
become_user=root
become_ask_pass=False
vim /home/devops/ansible/inventory
[test] #表示一个用户组
172.25.25.2 #指定用户组中的远程主机,可以是ip或主机名
[prod]
server3
[webserver:children] #
test
prod
在远程主机server2,server3创建普通用户
[root@server2 ~]# useradd devops
[root@server2 ~]# passwd devops
3.ansible管理主机需要与这些主机做好ssh免密
vim /etc/hosts
172.25.25.1 server1
172.25.25.2 server2
172.25.25.3 server3
ssh-keygen ##生成rsa认证密钥
ssh-copy-id server2
ssh-copy-id server3
4.ansible命令
-m 模块名 #调用模块
-u 用户名 #以XX身份执行
-p 用户名 #默认切换到root用户
以devops用户执行ping存活检测
[devops@server1 ansible]$ ansible all -m ping -u devops
以devops用户sudo至root执行ping存活检测
[devops@server1 ansible]$ ansible all -m ping -u devops -b
以devops用户sudo至xixi 执行ping存活检测
[devops@server1 ansible]$ ansible all -m ping -u devops -b --become-user xixi
因为是用devops做的免密,sudo到root便ping不通
解决:
vim /etc/sudoers
devops ALL=(ALL) NOPASSWD:ALL
定义主机和组
中括号表示一个组,也可以表示一个范围
[webserver]
www[1:10].example.com
db-[a:f].example.com
定义主机变量
在playbook中使用时对主机进行个性化定制
[webserver]
web1 http_port=8080 http_host=xxx.xxx.xxx.xxx
定义组嵌套及组变量
[apache]
server2
[nginx]
server3
server4
[webserver:children]
apache
nginx
[webserver:vars]
ntp_server=time1.aliyun.com
组与组之间可以相互调用,并且可以向组中的主机指定变量。不过,这些变量只能在Ansible-playbook中使用,而Ansible不支持。
在inventory主文件中保存所有的变量并不是最佳的方式,还可以保存到独立的文件中:
这些独立文件的格式为YAML
/etc/ansible/group_vars/raleigh
/etc/ansible/group_vars/webservers
/etc/ansible/host_vars/foosball
当变量太多时,分文件定义变量,可以更方便管理和组织
/etc/ansible/group_vars/raleigh/db_settings
/etc/ansible/group_vars/raleigh/cluster_settings
vim /home/devops/ansible/inventory
[test] #表示一个用户组
172.25.25.2 #指定用户组中的远程主机,可以是ip或主机名
[prod]
server3
[webserver:children] #
test
prod
ansible与正则
匹配所有的主机,all或*功能相同。
- ansible all -m ping
- ansible “*” -m ping
- ansible 172.25.0.* -m ping
对多台主机或多个组同时执行,相互之间用冒号分割即可
- ansible “web1:web2” -m ping
在webserver组但不在database组的主机,用感叹号表示
- ansible ‘webserver:!database’ -m ping
在webserver组和database组中同时存在的主机,用&符号表示
- ansible “webserver:&database” -m ping
模糊匹配
- *.example.com
- www*.com:database
[devops@server1 ansible]$ ansible ‘test:!prod’ -m ping
172.25.25.2 | SUCCESS => {
“changed”: false,
“ping”: “pong”
}
[devops@server1 ansible]$ ansible 'webserver' -m ping
server3 | SUCCESS => {
"changed": false,
"ping": "pong"
}
172.25.25.2 | SUCCESS => {
"changed": false,
"ping": "pong"
}
[devops@server1 ansible]$ ansible 'test:&prod' -m ping
[devops@server1 ansible]$ ansible 'ser*' -m ping
server3 | SUCCESS => {
"changed": false,
"ping": "pong"
}
[devops@server1 ansible]$ ansible '172.25.25.*' -m ping
172.25.25.2 | SUCCESS => {
"changed": false,
"ping": "pong"
}
ansible常用模块
ansible有许多模块,默认是command,也就是命令模块,可以通过 -m 选项来指定不同的模块。
copy模块
ansible webserver -m copy -a “src=/etc/hosts dest=/tmp/hosts”
file模块
修改文件权限和属性
ansible webserver -m file -a “dest=/tmp/hosts mode=600 owner=root group=root”
递归创建
ansible webserver -m file -a “dest=/tmp/dir1/dir2 mode=755 owner=root group=root state=directory”
删除文件
ansible webserver -m file -a “dest=/tmp/dir1/dir2 state=absent”
yum模块
ansible webserver -m yum -a “name=httpd state=present”
在线下载
ansible server3 -m yum -a “name=http://172.25.25.250/rhel7.3/packages/XXX.rpm state=present”
本地安装
ansible server2 -m yum -a “name=/mnt/xxx.rpm state=present”
卸载软件
ansible server2 -m yum -a “name=httpd state=absent”
service模块
ansible webserver -m service -a “name=httpd state=started”
ansible webserver -m service -a “name=httpd state=restarted”
ansible webserver -m service -a “name=httpd state=stopped”
user模块
ansible all -m user -a “name=xixi password=<密码>”#明文
密码加密
ansible webserver -m user -a “name=xixi password={{‘haha’|password_hash(‘sha512’)}}”
删除用户
ansible all -m user -a “name=xixi state=absent remove=yes”
创建用户
ansible all -m user -a “name=xixi shell=/bin/bash groups=users,wheel append=yes state=present”
ansible-doc
ansible模块非常多,ansible提供了类似于man功能的help说明工具ansible-doc。
显示所有可用模块
ansible-doc -l
获取yum模块帮助
ansible-doc yum
指定操作test组,拷贝文件
[devops@server1 ansible]$ ansible test -m copy -a "src=/etc/passwd dest=/tmp/passwd"
在test组中的远程主机中查看/tmp,默认调用command模块
[devops@server1 ansible]$ ansible test -a "ls /tmp/"
没有权限操作/mnt
[devops@server1 ansible]$ ansible test -m copy -a "src=/etc/passwd dest=/mnt/passwd"
172.25.25.2 | FAILED! => {
"changed": false,
"checksum": "22dc9c5c3e860a79ca8a2328cd2fb7ab5ebba3d1",
"msg": "Destination /mnt not writable" #server2/mnt 不可写
}
在server2修改devops权限
[root@server2 ansible]# vim /etc/sudoers #server2
devops ALL=(ALL) NOPASSWD:ALL
可以拷贝文件到/mnt
安装软件
present安装;absent 卸载
test组远程主机安装httpd
[devops@server1 ansible]$ ansible test -m yum -a "name=httpd state=present"
test组查看
[devops@server1 ansible]$ ansible test -a "rpm -qa httpd"
test组command方式开启httpd服务
[devops@server1 ansible]$ ansible test -a "systemctl start httpd"
[root@server2 ~]# netstat -ntlp
test组远程主机卸载httpd
[devops@server1 ansible]$ ansible test -m yum -a "name=httpd state=absent"
调用service模块开启httpd
[devops@server1 ansible]$ ansible test -m service -a "name=httpd state=started"
远程主机安装mariadb
[devops@server1 ansible]$ ansible test -m yum -a "name=mariadb-server state=present"
[devops@server1 ansible]$ ansible test -m yum -a "name=MySQL-python state=present"
#开启数据库
[devops@server1 ansible]$ ansible test -m service -a "name=mariadb state=started"
在数据库中创建用户xixi,设置密码,授权所有表的select
[devops@server1 ansible]$ ansible test -m mysql_user -a "name=xixi password=haha priv=*.*:select host='%' state=present"
test组远程主机创建用户xixi,密码加密
[devops@server1 ansible]$ ansible test -m user -a "name=xixi password={{'haha'|password_hash('sha512')}}"
Ansible的并发特性
ansible和ansible-playbook默认会fork5个线程并发执行命令,如果同时操作的主机数比较多的话,可以调整到一个更大的值。
-f 指定线程数
- ansible webserver -m ping -f 3