共計 9781 個字符,預計需要花費 25 分鐘才能閱讀完成。
這篇文章主要介紹“Ansible 怎么安裝使用”,在日常操作中,相信很多人在 Ansible 怎么安裝使用問題上存在疑惑,丸趣 TV 小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Ansible 怎么安裝使用”的疑惑有所幫助!接下來,請跟著丸趣 TV 小編一起來學習吧!
Ansible 可以集中地控制多個節點,批量地執行 ssh 命令。由于其使用 ssh 進行操作,因此遠端服務器除了安裝 openssh-server(一般服務器已經內置)之外,不需要安裝額外的軟件,因此使用非常簡單和方便。
1、快速安裝
包括 Ansible 和 sshpass,其中 sshpass 是用于交互輸入密碼的組件。因為我們要批量處理大量節點,因此節點的密碼設為一樣可以大大簡化配置過程,但這會增加安全性風險,需要設置足夠強度的密碼并妥善保存。
運行命令如下:
sudo apt install -y ansible sshpass
2、創建 Hosts 清單
這是 Ansible 要操作的節點主機名或 IP 地址的清單,可以分組和指定登錄賬號、密碼等參數。該清單有一個系統級的默認存儲位置(參考 /etc/ansible/hosts),但不建議應用使用。可以在自己的目錄下創建一個清單,然后使用環境變量 ANSIBLE_HOSTS 來指示文件位置,或者直接放在當前目錄下,使用 - i 來指定清單的文件名。
創建主機清單
創建一個 hosts 主機清單文件:
echo 127.0.0.1 ~/ansible_hosts
將環境變量加入啟動文件:
# 將 hosts 清單放在 home 目錄,每次系統啟動時自動加載。echo export ANSIBLE_HOSTS=~/ansible_hosts ~/.profile
# 立即使用。source ~/.proflie
更復雜的主機清單
單獨指定主機參數的例子:
[local]
192.168.199.188 ansible_ssh_port=22 ansible_ssh_host=192.168.199.188 ansible_ssh_user=superwork ansible_ssh_pass=SuperMap
192.168.199.249 ansible_ssh_port=22 ansible_ssh_host=192.168.199.249 ansible_ssh_user=supermap ansible_ssh_pass=SuperMap
192.168.199.174 ansible_ssh_port=22 ansible_ssh_host=192.168.199.174 ansible_ssh_user=smt ansible_ssh_pass=SuperMap
更多的主機清單格式:
# ansible 主機清單格式
# This is the default ansible hosts file.
# It should live in /etc/ansible/hosts
# - Comments begin with the # character
# - Blank lines are ignored
# - Groups of hosts are delimited by [header] elements
# - You can enter hostnames or ip addresses
# - A hostname/ip can be a member of multiple groups
# Ex 1: Ungrouped hosts, specify before any group headers.
#green.example.com
#blue.example.com
#192.168.100.1
#192.168.100.10
# Ex 2: A collection of hosts belonging to the webservers group
#[webservers]
#alpha.example.org
#beta.example.org
#192.168.1.100
#192.168.1.110
# If you have multiple hosts following a pattern you can specify
# them like this:
#www[001:006].example.com
# Ex 3: A collection of database servers in the dbservers group
#[dbservers]
#db01.intranet.mydomain.net
#db02.intranet.mydomain.net
#10.25.1.56
#10.25.1.57
# Here s another example of host ranges, this time there are no
# leading 0s:
#db-[99:101]-node.example.com
3、操作多臺主機
ansible 可以自動按照清單在多個主機上通過 ssh 執行命令。
馬上試一下
現在來試一下,ping 清單中所有的機器:
ansible all -m ping
或者提示輸入 ssh 密碼:
ansible all -m ping
--ask-pass
使用 –ask-pass 提示用戶在運行時輸入密碼,避免將密碼保存在配置文件中,增加一定程度上的安全性。
指定清單文件,遠程獲取清單中所有機器的 hostname:
ansible all -m shell -a hostname --ask-pass -i ~/ansible_hosts
獲取 Docker 信息:
ansible all -m shell -a docker info --ask-pass
獲取主機信息:
ansible all -m shell -a uname -a --ask-pass
執行 sudo 操作
下面的命令執行 apt update 操作,遠程更新各個主機的軟件包。
ansible all -m shell -a apt update apt upgrade -y --ask-sudo-pass --become --become-method=sudo
注意上面的 –ask-sudo-pass 和 –become 參數,在 Ubuntu 里遠程使用 sudo 來執行系統級的命令。
4、密鑰登錄設置
上面使用的是密碼登錄 ssh,另外一種方法是使用密鑰進行登錄,安全性更強一些,使用也更為方便。
創建密鑰:
ssh-keygen -t rsa
上傳密鑰到遠程主機:
ansible all -m copy -a src=/home/openthings/.ssh/id_rsa.pub dest=/tmp/id_rsa.pub --ask-pass
把公鑰文件追加到遠程服務器的授權清單里。輸入:
ansible all -m shell -a cat /tmp/id_rsa.pub /root/.ssh/authorized_keys --ask-pass -u root
然后,把 /tmp 中的公鑰文件刪除:
ansible all -m file -a dest=/tmp/id_rsa.pub state=absent -u root
試一下(現在不需要輸入密碼了,也不需使用 –ask-pass 參數):
ansible all -m shell -a hostname -u root
注意:
使用 mass 裝機的節點,可以(設置)自動注入 maas controller 的 ssh 密鑰,不需要再次配置。
5、Playbook 使用
Playbook 將主機清單和命令合成為一個 yaml 文件,使用更為方便。
把上面的 ssh 密鑰分發的過程編寫為一個 playbook 文件,如下:
---
- hosts: SUSEBased
remote_user: mike
sudo: yes
tasks:
- authorized_key: user=root key= {{ lookup( file , /home/openthings/.ssh/id_rsa.pub) }} path=/root/.ssh/authorized_keys manage_dir=no
- hosts: RHELBased
remote_user: mdonlon
sudo: yes
tasks:
- authorized_key: user=root key= {{ lookup( file , /home/openthings/.ssh/id_rsa.pub) }} path=/root/.ssh/authorized_keys manage_dir=no
還是比較簡明的,下面進一步解釋 playbook 的格式。
Playbook 格式
一個簡單的例子:
---
- hosts: showtermClients
remote_user: root
tasks:
- yum: name=rubygems state=latest
- yum: name=ruby-devel state=latest
- yum: name=gcc state=latest
- gem: name=showterm state=latest user_install=no
主要包括 hosts、user 和 tasks 三個主要部分,即主機、用戶和命令。
一個完整的主機配置 playbook 如下:
---
- hosts: showtermServers
remote_user: root
tasks:
- name: ensure packages are installed
yum: name={{item}} state=latest
with_items:
- postgresql
- postgresql-server
- postgresql-devel
- python-psycopg2
- git
- ruby21
- ruby21-passenger
- name: showterm server from github
git: repo=https://github.com/ConradIrwin/showterm.io dest=/root/showterm
- name: Initdb
command: service postgresql initdb
creates=/var/lib/pgsql/data/postgresql.conf
- name: Start PostgreSQL and enable at boot
service: name=postgresql
enabled=yes
state=started
- gem: name=pg state=latest user_install=no
handlers:
- name: restart postgresql
service: name=postgresql state=restarted
- hosts: showtermServers
remote_user: root
sudo: yes
sudo_user: postgres
vars:
dbname: showterm
dbuser: showterm
dbpassword: showtermpassword
tasks:
- name: create db
postgresql_db: name={{dbname}}
- name: create user with ALL priv
postgresql_user: db={{dbname}} name={{dbuser}} password={{dbpassword}} priv=ALL
- hosts: showtermServers
remote_user: root
tasks:
- name: database.yml
template: src=database.yml dest=/root/showterm/config/database.yml
- hosts: showtermServers
remote_user: root
tasks:
- name: run bundle install
shell: bundle install
args:
chdir: /root/showterm
- hosts: showtermServers
remote_user: root
tasks:
- name: run rake db tasks
shell: bundle exec rake db:create db:migrate db:seed
args:
chdir: /root/showterm
- hosts: showtermServers
remote_user: root
tasks:
- name: apache config
template: src=showterm.conf dest=/etc/httpd/conf.d/showterm.conf
Playbook 使用
使用 ansible playbook 的命令是 ansible-playbook,其它參數與 ansible 是基本一致的。
ansible-playbook testPlaybook.yaml -f 10
注意,上面的 -f 參數指的是并行執行的數量。
6、Ansible 命令參考
使用 ansible -h 可以獲取 ansible 的命令詳細列表,如下:
Usage: ansible host-pattern [options]
Define and run a single task playbook against a set of hosts
Options:
-a MODULE_ARGS, --args=MODULE_ARGS
module arguments
--ask-vault-pass ask for vault password
-B SECONDS, --background=SECONDS
run asynchronously, failing after X seconds
異步運行,可以指定超時的時長。 (default=N/A)
-C, --check don t make any changes; instead, try to predict some
of the changes that may occur
-D, --diff when changing (small) files and templates, show the
differences in those files; works great with --check
-e EXTRA_VARS, --extra-vars=EXTRA_VARS
set additional variables as key=value or YAML/JSON, if
filename prepend with @
-f FORKS, --forks=FORKS
specify number of parallel processes to use
并行執行,可指定并發數,缺省為 5。 (default=5)
-h, --help show this help message and exit
-i INVENTORY, --inventory=INVENTORY, --inventory-file=INVENTORY
specify inventory host path or comma separated host
list. --inventory-file is deprecated
指定 host 文件路徑或者分隔的 host 清單。 -l SUBSET, --limit=SUBSET
further limit selected hosts to an additional pattern
--list-hosts outputs a list of matching hosts; does not execute
anything else
列出 hosts 主機清單。 -m MODULE_NAME, --module-name=MODULE_NAME
module name to execute (default=command)
-M MODULE_PATH, --module-path=MODULE_PATH
prepend colon-separated path(s) to module library (def
ault=[u /home/openswitch/.ansible/plugins/modules ,
u /usr/share/ansible/plugins/modules ])
-o, --one-line condense output
--playbook-dir=BASEDIR
Since this tool does not use playbooks, use this as a
subsitute playbook directory.This sets the relative
path for many features including roles/ group_vars/
etc.
指定 playbook 的主目錄。 -P POLL_INTERVAL, --poll=POLL_INTERVAL
set the poll interval if using -B (default=15)
pull 的時間間隔。 --syntax-check perform a syntax check on the playbook, but do not
execute it
-t TREE, --tree=TREE log output to this directory
日志輸出目錄。 --vault-id=VAULT_IDS the vault identity to use
--vault-password-file=VAULT_PASSWORD_FILES
vault password file
-v, --verbose verbose mode (-vvv for more, -vvvv to enable
connection debugging)
--version show program s version number and exit
Connection Options:
control as whom and how to connect to hosts
-k, --ask-pass ask for connection password
詢問密碼。 --private-key=PRIVATE_KEY_FILE, --key-file=PRIVATE_KEY_FILE
use this file to authenticate the connection
-u REMOTE_USER, --user=REMOTE_USER
指定遠端主機上的用戶名,將用該用戶操作。 connect as this user (default=None)
-c CONNECTION, --connection=CONNECTION
connection type to use (default=smart)
-T TIMEOUT, --timeout=TIMEOUT
override the connection timeout in seconds
指定連接超時,缺省為 1
(default=10)
--ssh-common-args=SSH_COMMON_ARGS
specify common arguments to pass to sftp/scp/ssh (e.g.
ProxyCommand)
--sftp-extra-args=SFTP_EXTRA_ARGS
specify extra arguments to pass to sftp only (e.g. -f,
-l)
--scp-extra-args=SCP_EXTRA_ARGS
specify extra arguments to pass to scp only (e.g. -l)
--ssh-extra-args=SSH_EXTRA_ARGS
specify extra arguments to pass to ssh only (e.g. -R)
Privilege Escalation Options:
control how and which user you become as on target hosts
-s, --sudo run operations with sudo (nopasswd) (deprecated, use
become)
指定使用 sudo 操作,已過時,使用 become。 -U SUDO_USER, --sudo-user=SUDO_USER
desired sudo user (default=root) (deprecated, use
become)
已過時,使用 become。 -S, --su run operations with su (deprecated, use become)
已過時,使用 become。 -R SU_USER, --su-user=SU_USER
run operations with su as this user (default=None)
(deprecated, use become)
已過時,使用 become。 -b, --become run operations with become (does not imply password
prompting)
使用 become 操作。 --become-method=BECOME_METHOD
privilege escalation method to use (default=sudo),
valid choices: [ sudo | su | pbrun | pfexec | doas |
dzdo | ksu | runas | pmrun | enable ]
become 操作方法,缺省為 sudo。 --become-user=BECOME_USER
run operations as this user (default=root)
become 操作的用戶名,缺省為 root。 --ask-sudo-pass ask for sudo password (deprecated, use become)
已過時,使用 become。 --ask-su-pass ask for su password (deprecated, use become)
已過時,使用 become。 -K, --ask-become-pass
ask for privilege escalation password
Some modules do not make sense in Ad-Hoc (include, meta, etc)
到此,關于“Ansible 怎么安裝使用”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注丸趣 TV 網站,丸趣 TV 小編會繼續努力為大家帶來更多實用的文章!