ansible tower(awx) Centos7搭建与使用总结(非docker部署)
####一、項目背景
#####由于最近入職新公司,公司中實現運維自動化的架構中主要用到ansible,但是ansible腳本主要部署于服務器指令行中不太直觀。經搜查資料,得知ansible tower(之前叫做awx)是將ansible的指令界面化,簡明直觀,簡單易用,故搭建了一個ansible tower環境,與大家分享學習。
####二、搭建步驟
#####1.部署服務器之間免密。大家都知道,ansible實現批量時也會通過ssh遠程模塊去執行命令,因此服務器之間部署免密較為方便。此處,我測試環境有兩臺服務器(主機名wushengtest-agent1,wushengtest-agent2)實現,使用root(或其他用戶)執行ssh-keygen生成秘鑰對,將id_rsa.pub文件輸出到本地和遠程機器中的~/.ssh/authorized_keys文件中,因第一次實現免密會要求輸入一個yes或no之后才能實現,故公鑰部署好之后手動進行一次遠程登錄。
#####host文件,定義主機名(此處也可跳過)
#####authorized_keys文件(實現免密)
#####2.防火墻設置。此處可以選擇關閉防火墻,或者設置特定的防火墻規則。
######(1)關閉防火墻。
######(2)設置特定防火墻規則。
systemctl enable firewalld systemctl start firewalld firewall-cmd --add-service=http --permanent;firewall-cmd --add-service=https --permanent systemctl restart firewalld#####3.下載epel-release源。
yum install -y epel-release#####4.下載postgreSQL。
yum install -y https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm yum install postgresql96-server -y#####5.下載其他必要組件。
yum install -y rabbitmq-server wget memcached nginx ansible#####6.下載awx。
##發現下面這個yum源已失效,現已更新 ##wget -O /etc/yum.repos.d/awx-rpm.repo https://copr.fedorainfracloud.org/coprs/mrmeee/awx/repo/epel-7/mrmeee-awx-epel-7.repo wget -O /etc/yum.repos.d/awx-rpm.repo https://copr.fedorainfracloud.org/coprs/mrmeee/ansible-awx/repo/epel-7/mrmeee-ansible-awx-epel-7.repo yum install -y awx#####7.初始化數據庫。
/usr/pgsql-9.6/bin/postgresql96-setup initdb Initializing database ... OK#####8.啟動rabbit-mq服務。
systemctl start rabbitmq-server && systemctl enable rabbitmq-server#####9.啟動PostgreSQL服務。
systemctl enable postgresql-9.6 && systemctl start postgresql-9.6#####10.啟動Memcached服務。
systemctl enable memcached && systemctl start memcached#####11.創建PostgreSQL用戶。
sudo -u postgres createuser -S awx ####若發生"could not change directory to "/root": Permission denied"報錯,忽略即可。#####12.創建數據庫。
sudo -u postgres createdb -O awx awx ####若發生"could not change directory to "/root": Permission denied"報錯,忽略即可。#####13.數據導入數據庫。
sudo -u awx /opt/awx/bin/awx-manage migrate#####14.awx 初始化配置。
echo "from django.contrib.auth.models import User; User.objects.create_superuser('admin', 'root@localhost', 'password')" | sudo -u awx /opt/awx/bin/awx-manage shellsudo -u awx /opt/awx/bin/awx-manage create_preload_datasudo -u awx /opt/awx/bin/awx-manage provision_instance --hostname=$(hostname)sudo -u awx /opt/awx/bin/awx-manage register_queue --queuename=tower --hostnames=$(hostname)#####15.nginx文件配置。(若不想通過配置域名訪問,則此步驟可跳過)
cd /etc/nginx/ cp nginx.conf nginx.conf.bkp #備份nginx配置文件 wget -O /etc/nginx/nginx.conf https://raw.githubusercontent.com/sunilsankar/awx-build/master/nginx.conf #替換nginx配置文件systemctl start nginx && systemctl enable nginx #啟動與開機自啟nginx服務#####16.開啟awx服務。
systemctl start awx-cbreceiver systemctl start awx-celery-beat systemctl start awx-celery-worker systemctl start awx-channels-worker systemctl start awx-daphne systemctl start awx-websystemctl enable awx-cbreceiver systemctl enable awx-celery-beat systemctl enable awx-celery-worker systemctl enable awx-channels-worker systemctl enable awx-daphne systemctl enable awx-web#####17.創建免密登錄用戶。
useradd ansible #本地和遠程都執行添加 su - ansible #切換到ansible用戶 ssh-keygen #生成秘鑰 Generating public/private rsa key pair. Enter file in which to save the key (/home/ansible/.ssh/id_rsa): Created directory '/home/ansible/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/ansible/.ssh/id_rsa. Your public key has been saved in /home/ansible/.ssh/id_rsa.pub. The key fingerprint is: SHA256:RW/dhTsxcyGicleRI0LpLm+LyhAVinm0xktapodc8gY ansible@awx.sunil.cc The key's randomart image is: +---[RSA 2048]----+ | . . ..o. +ooo| | = o . +.oo+*.o| | E @ . ..oo.+ o*.| |. # o oo.. o | | = * S . | | o . . . | | . o | | o .o | | o..... | +----[SHA256]-----+visudo #配置ansible超級管理員權限 ansible ALL=(ALL) NOPASSWD: ALL #免密執行所有操作#####18.將ansible用戶秘鑰發送到本地和遠程的~/.ssh/authorized_keys中,并設置authorized_keys權限為600。
###本地 [ansible@wushengtest-agent1 .ssh]$ cat id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDStB8JGsVsSLppwYWdnEPLE4xwFqRDn7xE/d3hjBQ6A0JGm1t+GtHB3GPIEjANFTnxQwHpR+cRttbL3mlQvpIYqCZOMZds9XA7VI5qgs0aSGUU8cNYKjmmrMpJa9sB4WVtj3M4u2fEXt9FKKCtjMMpOfiQxIkEhYZ+2GoAX5sHXan7TPcgwb5r7WW6j43aaPc6g9XWN63nonQz6KeMSFZ/y0o2HJMh1FEkktZw6A1HVfn+JNWoQb1glyqGjO1ync+Sok8yXpqakEEWpXNQSQYs4eBEwfkKql5EuolQMIbF9VYhpEcR9LfbMvYdq/RPKWN3mmRMWfPZ2dTZl515XBdV ansible@awx.sunil.cc [ansible@wushengtest-agent1 .ssh]$ [ansible@wushengtest-agent1 .ssh]$ cat authorized_keys ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDStB8JGsVsSLppwYWdnEPLE4xwFqRDn7xE/d3hjBQ6A0JGm1t+GtHB3GPIEjANFTnxQwHpR+cRttbL3mlQvpIYqCZOMZds9XA7VI5qgs0aSGUU8cNYKjmmrMpJa9sB4WVtj3M4u2fEXt9FKKCtjMMpOfiQxIkEhYZ+2GoAX5sHXan7TPcgwb5r7WW6j43aaPc6g9XWN63nonQz6KeMSFZ/y0o2HJMh1FEkktZw6A1HVfn+JNWoQb1glyqGjO1ync+Sok8yXpqakEEWpXNQSQYs4eBEwfkKql5EuolQMIbF9VYhpEcR9LfbMvYdq/RPKWN3mmRMWfPZ2dTZl515XBdV ansible@awx.sunil.cc [ansible@wushengtest-agent1 .ssh]$ chmod 600 authorized_keys###wushengtest-agent2 [root@wushengtest-agent2 ~]# su - ansible [ansible@wushengtest-agent2 ~]$ mkdir .ssh [ansible@wushengtest-agent2 ~]$ chmod 700 .ssh [ansible@wushengtest-agent2 ~]$ cat .ssh/authorized_keys ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDStB8JGsVsSLppwYWdnEPLE4xwFqRDn7xE/d3hjBQ6A0JGm1t+GtHB3GPIEjANFTnxQwHpR+cRttbL3mlQvpIYqCZOMZds9XA7VI5qgs0aSGUU8cNYKjmmrMpJa9sB4WVtj3M4u2fEXt9FKKCtjMMpOfiQxIkEhYZ+2GoAX5sHXan7TPcgwb5r7WW6j43aaPc6g9XWN63nonQz6KeMSFZ/y0o2HJMh1FEkktZw6A1HVfn+JNWoQb1glyqGjO1ync+Sok8yXpqakEEWpXNQSQYs4eBEwfkKql5EuolQMIbF9VYhpEcR9LfbMvYdq/RPKWN3mmRMWfPZ2dTZl515XBdV ansible@awx.sunil.cc [ansible@wushengtest-agent2 ~]$ chmod 600 .ssh/authorized_keys#####19.驗證免密登錄。
[ansible@wushengtest-agent1 ~]$ ssh wushengtest-agent1 Last login: Sun Mar 11 13:14:06 2018 from 192.168.65.128 [ansible@wushengtest-agent1 ~]$ exit logout Connection to client1 closed. [ansible@wushengtest-agent1 ~]$ ssh wushengtest-agent2 Last login: Sun Mar 11 12:50:14 2018 from 192.168.65.129 [ansible@wushengtest-agent2 ~]$#####20.瀏覽器輸入http://awx服務器ip即可訪問。用戶名/密碼初始化為:admin/password
#####21.創建任務。
######(1)創建項目。
#####21.友情提示:本篇博客根據https://www.howtoforge.com/tutorial/centos-ansible-awx-installation/總結得來。
####三、問題總結。
#####1.ansible tower(aws)安裝,python版本應在3以上。
#####2.ansible文檔指示,下載make和python后,在python文件夾中直接進行 make && make install報錯Segmentation fault。
#####應該是make版本沖突,使用/usr/bin/make 進行編譯就可以。
#####3.Failed to get D-Bus connection: Operation not permitted
#####重啟以后解決。
#####4.yml腳本中自定義hosts報錯,用all就可以。
#####原因為服務器中去執行yml腳本默認從/etc/ansible/hosts中讀取hosts,而awx界面的腳本默認從界面設置的inventory->hosts中讀取。
總結
以上是生活随笔為你收集整理的ansible tower(awx) Centos7搭建与使用总结(非docker部署)的全部內容,希望文章能夠幫你解決所遇到的問題。