公司中實現運維自動化的架構中主要用到ansible,ansible腳本在部署服務器指令行中顯得不太直觀。Ansible-Tower(之前叫做awx)是將ansible的指令界面化,簡明直觀,簡單易用。Ansibke-tower其實就是一個圖形化的任務調度,復雜服務部署,IT自動化的一個管理平台,屬於發布配置管理系統,支持Api及界面操作,Django編寫。Ansible-tower可以通過界面從github拉取最新playbook實施服務部署,提高生產效率。當然它也提供一個RESET API和命令行的CLI以供python腳本調用。下面是Ansible-Tower的搭建記錄,在此分享下:
Ansible-Tower目前支持7.4+的版本,可以使用yum update -y命令更新; 1. 安裝Ansible的epel源 [root@ansible ~]# cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core) [root@ansible ~]# python -V Python 2.7.5 關閉selinux [root@ansible ansible-tower]# vim /etc/sysconfig/selinux ......... SELINUX=disabled [root@ansible ansible-tower]# setenforce 0 setenforce: SELinux is disabled [root@ansible ansible-tower]# getenforce Disabled 關閉防火牆 [root@ansible ansible-tower]# systemctl stop firewalld [root@ansible ansible-tower]# systemctl disable firewalld [root@ansible ansible-tower]# firewall-cmd --state not running ======================================================================================================== 需要注意:如果開啟了防火牆,需要開放對應訪問策略(這里是測試環境,就關閉了防火牆) [root@ansible ansible-tower]# firewall-cmd --permanent --zone=public --add-port=80/tcp [root@ansible ansible-tower]# systemctl restart firewalld.service ======================================================================================================== 2. 安裝Ansible [root@ansible ~]# yum install -y ansible [root@ansible ~]# ansible --version ansible 2.8.2 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, Jun 20 2019, 20:27:34) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] 3. 按照ansible-tower(官網下載地址: https://releases.ansible.com/ansible-tower/setup/) 下載地址:https://pan.baidu.com/s/1Uz-BFZXkjOr4FLg-lFF4fQ 提取密碼:3e97 [root@ansible ~]# cd /usr/local/src/ [root@ansible src]# wget https://releases.ansible.com/ansible-tower/setup-bundle/ansible-tower-setup-bundle-3.2.6-1.el7.tar.gz [root@ansible src]# tar -zvxf ansible-tower-setup-bundle-3.2.6-1.el7.tar.gz [root@ansible src]# mv ansible-tower-setup-bundle-3.2.6-1.el7 /usr/local/ansible-tower [root@ansible src]# cd /usr/local/ansible-tower [root@ansible ansible-tower]# ls backup.yml bundle group_vars install.yml inventory licenses README.md restore.yml roles setup.sh 配置inventory文件 (注意:admin_password處填寫的就是ansible-tower登陸密碼,密碼可以自行設定) [root@ansible ansible-tower]# sed -i "s#password=''#password='tower@123'#g" inventory [root@ansible ansible-tower]# sed -i "s#host=''#host='127.0.0.1'#g" inventory [root@ansible ansible-tower]# sed -i "s#port=''#port='5432'#g" inventory 安裝前先創建/var/log/tower的日志目錄,不然會報錯 [root@ansible ansible-tower]# mkdir -p /var/log/tower 接着執行ansible-tower的安裝腳本,如果網絡沒有問題的話耐心等待安裝完成即可. [root@ansible ansible-tower]# ./setup.sh
安裝完成沒報錯的話即可訪問web頁面,這里測試機地址為172.16.60.244,則訪問ansible-tower地址就是https://172.16.60.244, 默認初始頁面如下:


默認用戶為admin,密碼為inventory文件admin_password字段配置的密碼(如上設置的密碼為"tower@123")。接着會提示讓選擇license文件,導入license,沒有的話,點擊REQUEST LICENSE,去官方 (https://www.ansible.com/license) 申請免費試用,填寫個人信息后 (郵箱要填寫正確,其他信息可隨便填寫) 會把license發到填寫的郵箱。這里分享一個已經申請下來的license文件 (提取密碼為: krwe)。提交license並登錄成功后默認初始頁面如下:


申請的免費版license最多只能添加10個主機, 且有時間限制。 下面記錄下破解方法:
[root@k8s-node01 ansible-tower]# cd /var/lib/awx/venv/awx/lib/python2.7/site-packages/tower_license
[root@k8s-node01 tower_license]# ll
total 28
-rw-r--r-- 1 root root 10417 Aug 11 2018 __init__.py
-rw-r--r-- 1 root root 6352 Aug 11 2018 __init__.pyc
-rw-r--r-- 1 root root 6352 Aug 11 2018 __init__.pyo
修改__init__.py文件
將119行和120行修改為如下內容,特別需要注意格式.
也就是在原文119和120行之間添加了一行"return True"內容,格式對齊即可 (即加入的"return True" 跟 后面的if語句保持對齊,不然后面重新編譯會報錯)。
[root@k8s-node01 tower_license]# vim __init__.py
.........
119 def _check_cloudforms_subscription(self):
120 return True
121 if os.path.exists('/var/lib/awx/i18n.db'):
122 return True
123 if os.path.isdir("/opt/rh/cfme-appliance") and os.path.isdir("/opt/rh/cfme-gemset"):
124 try:
.........
修改完重新編譯一下:
[root@k8s-node01 tower_license]# python -m py_compile __init__.py
[root@k8s-node01 tower_license]# python -O -m py_compile __init__.py
[root@k8s-node01 tower_license]#
重啟服務:
[root@k8s-node01 tower_license]# ansible-tower-service restart
Restarting Tower
Redirecting to /bin/systemctl stop postgresql-9.6.service
Redirecting to /bin/systemctl stop rabbitmq-server.service
Redirecting to /bin/systemctl stop nginx.service
Redirecting to /bin/systemctl stop supervisord.service
Redirecting to /bin/systemctl start postgresql-9.6.service
Redirecting to /bin/systemctl start rabbitmq-server.service
Redirecting to /bin/systemctl start nginx.service
Redirecting to /bin/systemctl start supervisord.service
[root@k8s-node01 tower_license]#
重新打開ansible-tower界面的"settings"–>"VIEW YOUR LICENSE",發現"Hosts Available"變成了9999999台,說明破解成功,如下:

需要注意:發現最新版本或者高版本的ansible-tower沒有__init__.py文件,需要對__init__.pyc進行反編譯,然后進行HOSTS限制破解操作:
比如下載ansible-tower-setup-latest.tar.gz最新的包,按照上面的按照部署,將ansible-tower部署到/usr/local目錄下
[root@ansible ~]# python --version
Python 2.7.5
[root@ansible ~]# cd /var/lib/awx/venv/awx/lib/python3.6/site-packages/tower_license
[root@ansible tower_license]# ll
total 8
-rw-r--r-- 1 root root 5055 Aug 12 21:13 __init__.pyc
drwxr-xr-x 2 root root 37 Aug 22 15:19 __pycache__
1)接下來進行反匯編init.pyc
[root@ansible tower_license]# yum install python-pip
[root@ansible tower_license]# pip -V
pip 8.1.2 from /usr/lib/python2.7/site-packages (python 2.7)
[root@ansible tower_license]# pip install uncompyle6
[root@ansible tower_license]# uncompyle6 --version
uncompyle6 3.3.5
[root@ansible tower_license]# uncompyle6 __init__.pyc >__init__.py
[root@ansible tower_license]# ll
total 16
-rw-r--r-- 1 root root 7301 Aug 22 15:42 __init__.py
-rw-r--r-- 1 root root 5055 Aug 12 21:13 __init__.pyc
drwxr-xr-x 2 root root 37 Aug 22 15:19 __pycache__
2)修改__init__.py文件
[root@ansible tower_license]# vim __init__.py
........
# _check_cloudforms_subscription方法修改如下內容,特別需要注意格式。
def _check_cloudforms_subscription(self):
# 只需要添加下面一行直接返回 True即可。注意格式要跟if對對齊。
return True
if os.path.exists('/var/lib/awx/i18n.db'):
return True
else:
if os.path.isdir('/opt/rh/cfme-appliance'):
if os.path.isdir('/opt/rh/cfme-gemset'):
pass
try:
........
#修改"license_date=253370764800L" 為 "license_date=253370764800"
........
def _generate_cloudforms_subscription(self):
self._attrs.update(dict(company_name='Red Hat CloudForms License', instance_count=9999999,
license_date=253370764800, # 只需要修改這一行
license_key='xxxx',
license_type='enterprise',
subscription_name='Red Hat CloudForms License'))
........
3)修改完重新編譯一下
[root@ansible tower_license]# python -m py_compile __init__.py
[root@ansible tower_license]# python -O -m py_compile __init__.py
[root@ansible tower_license]#
4)重啟服務
[root@ansible tower_license]# ansible-tower-service restart
5)最后打開url (https://your_ip/#/license) ,發現"Hosts Available"變成了"9999999"台, 到期時間變成了"01/01/9999", 說明破解成功了。
