主機規劃
主機名稱 | 操作系統版本 | 內網IP | 外網IP(模擬) | 安裝軟件 |
---|---|---|---|---|
ansi-manager | CentOS7.5 | 172.16.1.180 | 10.0.0.180 | ansible |
ansi-haproxy01 | CentOS7.5 | 172.16.1.181 | 10.0.0.181 | |
ansi-haproxy02 | CentOS7.5 | 172.16.1.182 | 10.0.0.182 | |
ansi-web01 | CentOS7.5 | 172.16.1.183 | 10.0.0.183 | |
ansi-web02 | CentOS7.5 | 172.16.1.184 | 10.0.0.184 | |
ansi-web03 | CentOS7.5 | 172.16.1.185 | 10.0.0.185 |
在實際使用中並不需要對ansible配置進行修改,或者說只有需要的時候才修改ansible配置。
添加用戶賬號
說明:
1、 運維人員使用的登錄賬號;
2、 所有的業務都放在 /app/ 下「yun用戶的家目錄」,避免業務數據亂放;
3、 該用戶也被 ansible 使用,因爲幾乎所有的生產環境都是禁止 root 遠程登錄的(因此該 yun 用戶也進行了 sudo 提權)。
# 使用一個專門的用戶,避免直接使用root用戶
# 添加用戶、指定家目錄並指定用戶密碼
# sudo提權
# 讓其它普通用戶可以進入該目錄查看信息
useradd -u 1050 -d /app yun && echo '123456' | /usr/bin/passwd --stdin yun
echo "yun ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
chmod 755 /app/
Ansible 部署流程
添加 epel 源「如果沒有的話」
添加阿里雲 epel 源
https://opsx.alibaba.com/mirror
Ansible 安裝與版本信息查看
[root@ansi-manager ~]# yum install -y ansible
[root@ansi-manager ~]# whereis ansible # ansible 位置信息
ansible: /usr/bin/ansible /etc/ansible /usr/share/ansible /usr/share/man/man1/ansible.1.gz
[root@ansi-manager ~]# ansible --version # 版本信息查看
ansible 2.8.1 # ansible 版本
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 # ansible Python 模塊位置,使用 Python 2.7
executable location = /bin/ansible # ansible 執行文件的位置
python version = 2.7.5 (default, Apr 11 2018, 07:36:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] # Python 版本信息
[yun@ansi-manager ~]$ ll /usr/bin/ansible /bin/ansible # ansible 命令位置
lrwxrwxrwx 1 root root 20 Jun 24 14:14 /bin/ansible -> /usr/bin/ansible-2.7
lrwxrwxrwx 1 root root 20 Jun 24 14:14 /usr/bin/ansible -> /usr/bin/ansible-2.7
Ansible 配置文件講解
Ansible配置文件查找順序
ansible 將從多個地方查找配置文件,順序如下:
1、從環境變量 ANSIBLE_CONFIG 中查找,如果該環境變量有值的話;
2、當前目錄的 ansible.cfg 文件;「每個項目都可以有一個該配置文件,這樣可以更好的管理項目,移植時也更方便。」
3、當前用戶家目錄的 .ansible.cfg 文件;
4、/etc/ansible/ansible.cfg 文件。
可以使用 ansible --version 命令查看使用的配置文件。
在 /etc/ansible/ansible.cfg 配置文件中有該說明
Ansible 部分配置文件講解
實際生產中可以無需做任何修改。
[yun@ansi-manager ansible]$ pwd
/etc/ansible
[yun@ansi-manager ansible]$ vim ansible.cfg
#inventory = /etc/ansible/hosts # 受控端主機資源清單
#library = /usr/share/my_modules/ # 所需依賴庫路徑
#remote_tmp = ~/.ansible/tmp # 遠端機器,臨時文件存放位置
#local_tmp = ~/.ansible/tmp # 本機臨時文件存放位置
#forks = 5 # 默認併發數
#poll_interval = 15 # 默認輪詢時間間隔(單位秒)
#sudo_user = root # 默認sudo後的用戶
#ask_sudo_pass = True # 使用sudo,是否需要輸入密碼
#ask_pass = True # 是否需要輸入密碼
#transport = smart # 傳輸方式
#remote_port = 22 # 默認遠程主機的端口號
#module_lang = C # 模塊和系統之間通信的語言
#module_set_locale = False
………………
# uncomment this to disable SSH key host checking 取消註釋以禁用SSH key主機檢查 【默認是註釋掉的,要檢查主機指紋】
host_key_checking = False # 解開註釋,即跳過檢查主機指紋 【只有 root 用戶執行時纔有該取消指紋檢測的權限】
………………
# logging is off by default unless this path is defined
# if so defined, consider logrotate
#log_path = /var/log/ansible.log # 開啓ansible日誌
………………
[privilege_escalation] # 普通用戶提權配置「使用地方:普通用戶遠程提權使用」
#become=True
#become_method=sudo
#become_user=root
#become_ask_pass=False
上述的 [privilege_escalation] 配置,可在 ansible -h 中查看如何使用。如下:
[yun@ansi-manager ~]$ ansible -h
………………
Privilege Escalation Options: # 權限提升選項
control how and which user you become as on target hosts
-b, --become run operations with become (does not imply password
prompting)
--become-method=BECOME_METHOD
privilege escalation method to use (default=sudo), use
`ansible-doc -t become -l` to list valid choices.
--become-user=BECOME_USER
run operations as this user (default=root)
-K, --ask-become-pass
ask for privilege escalation password
………………