Ansible是一個python編寫模型驅動的配置管理器,支持多節點發布、遠程任務執行。默認使用 SSH 進行遠程鏈接。無需在被管理節點上安裝附加軟件,可以使用各類編程語言進行擴展。本文基於ansible 2.3.2.0版本python
工做原理
使用Ansible或Ansible-playbooks時,在服務器終端輸入Ansible的Ad-Hoc命令集或palybook後,Ansible會遵循預先編排的規則將Playbooks逐條拆解爲Play,再將paly組織成Ansible可識別的任務(Task),隨後調用任務涉及的全部模塊(modules)和插件(plugins),根據Inventory中定義的主機列表經過SSH將任務集以臨時文件或命令的形式傳輸到遠程客戶端執行並返回執行結果,若是是臨時文件,則執行完畢後自動刪除。mysql
配置文件
ansible配置文件爲ansible.cfg,通常在/etc/ansible/目錄下,默認的獲取順序爲
1. ANSIBLE_CONFIG (一個環境變量)
2. ansible.cfg (位於當前目錄中)
3. ansible.cfg (位於家目錄中)
4. /etc/ansible/ansible.cfglinux
簡易配置nginx
hostfile = /etc/ansible/hosts //hosts文件的位置 library = /usr/share/ansible //ansible默認搜尋模塊的位置 pattern = * //若是沒有提供hosts節點,這是playbook要通訊的默認主機組.默認值是對全部主機通訊 remote_tmp = $HOME/.ansible/tmp //Ansible經過遠程傳輸模塊到遠程主機,而後遠程執行,執行後在清理現場.在有些場景下,你也許想使用默認路徑但願像更換補丁同樣使用 forks = 5 //在與主機通訊時的默認並行進程數 ,默認是5d poll_interval = 15 //當具體的poll interval 沒有定義時,多少時間回查一下這些任務的狀態, 默認值是5秒 sudo_user = root //sudo使用的默認用戶 ,默認是root #ask_sudo_pass = True //用來控制Ansible playbook 在執行sudo以前是否詢問sudo密碼.默認爲no #ask_pass = True //控制Ansible playbook 是否會自動默認彈出密碼 transport = smart //通訊機制.默認 值爲’smart’。若是本地系統支持 ControlPersist技術的話,將會使用(基於OpenSSH)‘ssh’,若是不支持將使用‘paramiko’.其餘傳輸選項‘local’,‘chroot’,’jail’等等 #remote_port = 22 //遠程SSH端口。 默認是22 module_lang = C //模塊和系統之間通訊的計算機語言,默認是C語言 #host_key_checking = False //檢查主機密鑰 timeout = 10 //SSH超時時間 #log_path = /var/log/ansible.log //日誌文件存放路徑 #module_name = command //ansible命令執行默認的模塊 #private_key_file = /path/to/file //私鑰文件存儲位置
除以上參數外,ssh鏈接相關的參數(如host_key_checking)也在該文件中修改。詳細配置信息可查看官方示例git
ansible經常使用命令
ansible
ansible是指令核心部分,其主要用於執行ad-hoc命令,即單條命令。默認後面須要跟主機和選項部分,默認不指定模塊時,使用的是command模塊。github
選項說明web
option 說明sql
-vdocker |
詳細信息輸出shell |
-i |
指定inventory的目錄,缺省會使用/etc/ansible/hosts |
-f |
fork的進程個數,默認是5 |
–private-key=xxx |
指定ssh鏈接用的文件 |
-m |
指定module |
–module-name |
–module-name 指定module名稱 |
–module-path |
–module-path 指定module的path 默認是/usr/share/ansible |
-a |
指定module的參數 |
-k |
提示輸入password |
-K |
提示輸入sudo密碼 與–sudo一塊兒使用 |
-T |
設定鏈接超時時長 |
-B |
設定後臺運行並設定超時時長 |
-c |
設定鏈接類型 有ssh或者local等。 |
-b |
su的方式,能夠指定用戶 |
-C |
only for check |
-t |
將輸出結果以主機名的形式保存在指定目錄, -t /tmp |
實例
ansible test -m ping
該指令用於查看模塊信息,經常使用參數有兩個-l 和 -s
ansible-doc -l # 列出全部已安裝的模塊 ansible-doc -s command # 查看具體某模塊的用法,這裏如查看command模塊
ansible-galaxy 指令用於方便的從https://galaxy.ansible.com/ 站點下載第三方擴展模塊,咱們能夠形象的理解其相似於centos下的yum、python下的pip或easy_install
ansible-galaxy install aeriscloud.docker downloading role ‘docker’, owned by aeriscloud downloading role from https://github.com/AerisCloud/ansible-docker/archive/v1.0.0.tar.gz extracting aeriscloud.docker to /etc/ansible/roles/aeriscloud.docker aeriscloud.docker was installed successfully
ansible-lint
ansible-lint是對playbook的語法進行檢查的一個工具。用法是ansible-lint playbook.yml 。
ansible-playbook
該指令是使用最多的指令,其經過讀取playbook 文件後,執行相應的動做,詳見ansible-playbook部分
ansible-pull
該指令使用須要談到ansible的另外一種模式–pull 模式,適用於大規模機器場景
ansible-vault
ansible-vault主要應用於配置文件中含有敏感信息,主要對於playbooks裏好比涉及到配置密碼或其餘變量時,這種playbook文件在執行時,須要加上 –ask-vault-pass參數,須要輸入密碼後才能正常執行
Inventory和變量說明
Inventory
Ansible 可同時操做屬於一個組的多臺主機,組和主機之間的關係經過 inventory 文件配置. 默認的文件路徑爲 /etc/ansible/hosts
hosts文件示例
[webservers] foo.example.com bar.example.com [dbservers] one.example.com two.example.com three.example.com
主機變量
[atlanta] host1 http_port=80 maxRequestsPerChild=808 host2 http_port=303 maxRequestsPerChild=909
組變量
[atlanta] host1 host2 [atlanta:vars] ntp_server=ntp.atlanta.example.com proxy=proxy.atlanta.example.com
經常使用主機變量
ansible_user # ssh用戶 ansible_ssh_pass # 密碼 ansible_port # ssh端口 ansible_become # true or false,是否切換用戶 ansible_become_method # 切換用戶使用的方法,如sudo、su ansible_become_user # 要切換的用戶 ansible_become_pass # 切換用戶是須要的密碼
ansible全局變量在group_vars/all文件中,在該文件中設置的變量,在任何地方均可以使用
示例:
listen_port: "80" # 可以使用列表或字典 packages_base: - MySQL-server-5.6.21-1.el6.x86_64.rpm - MySQL-client-5.6.21-1.el6.x86_64.rpm mysql_databases: - name: 'test1' - name: 'test2'
role_path # role的路徑
編排定義Ansible任務集的配置文件,由Ansible順序依次執行,一般是json格式的YML文件。示例:
- hosts: 'test' roles: - nginx
ansible的roles用於層次性、結構化地組織palybook。roles可以根據層次型結構自動裝載變量文件、tasks及handlers等。要使用roles只須要playbook中使用include指令
roles組成
└── nginx \\角色名稱 ├── default \\爲當前角色設定默認變量時使用此目錄,應當包含一個main.yml文件; ├── files \\存放有copy或script等模塊調用的文件 ├── handlers \\此目錄總應當包含一個main.yml文件,用於定義各角色用到的各handler ├── meta \\應當包含一個main.yml,用於定義角色的特殊設定及其依賴關係;1.3及之後版本支持 ├── tasks \\至少包含一個名爲main.yml的文件,定義了此角色的任務列表,可以使用include指令 ├── templates \\template模塊會自動在此目錄中尋找Jinja2模板文件 └── vars \\應當包含一個main.yml文件,用於定義此角色用到的變量
示例
nginx/ ├── default ├── files │ └── nginx.conf.bak ├── handlers │ └── main.yml ├── meta ├── tasks │ ├── config.yml │ ├── install.yml │ ├── main.yml │ └── stop.yml ├── templates │ └── nginx.conf.j2 └── vars └── main.yml
nginx.yml
- hosts: test gather_facts: no roles: - nginx
main.yml
- include: install.yml - include: config.yml - include: stop.yml
- hosts: test tasks: - name: create file file: dest=/tmp/{{ item }} state=touch with_items: - 1.txt - 2.txt
- hosts: test gather_facts: yes tasks: - name: use when shell: touch /tmp/when.txt when: ansible_distribution == "CentOS"
handlers 的目的是在執行完成 tasks 以後,還須要進行其餘的操做時,使用 handlers。只有在 tasks 執行成功以後,handlers 纔會生效
- hosts: test tasks: - name: test copy copy: src=/tmp/1.txt dest=/root/1.txt notify: test handlers handlers: - name: test handlers shell: echo "handles" >> /root/1.txt
能夠將一個任務的輸出註冊爲變量,在後面的任務中使用
- hosts: test gather_facts: no tasks: - name: echo date command: date register: date_output - name: echo date_output debug: msg="{{ date_output }}"
若是須要在不是該組的主機執行命令,能夠經過該命令委託任務到其它機器上執行
- hosts: test gather_facts: no tasks: - name: touch 1.txt shell: touch /tmp/1.txt ignore_errors: yes # 忽視錯誤,即若是該任務出現錯誤,則跳過該任務,繼續執行下面的任務。默認爲no,出現錯誤時中止運行 - name: echo shell: 'echo "10.139.0.244" >> /tmp/1.txt' delegate_to: 10.139.0.244
經過 –check 參數來運行 ansible-playbook 時,將不會對遠程的系統做出任何修改。
能夠只執行特定部分的配置而無須要運行整個playbook
- hosts: test gather_facts: no tasks: - name: touch tag1.txt shell: touch /tmp/tag1.txt tags: tag1 - name: touch tag2.txt shell: touch /tmp/tag2.txt tags: tag2
roles中使用:
roles: - { role: webserver, port: 5000, tags: [ 'web', 'foo' ] }
注意:
須要安裝sshpass和libselinux-python用於執行命令和操做文件
遠程執行命令
#ad-hoc ansible test -m shell -a 'hostname' #playbooks - hosts: "test" gather_facts: no tasks: - name: shell shell: "hostname" register: result - name: debug debug: var=result.stdout verbosity=0
注意:
playbooks中執行命令,不會輸出命令執行結果。若是須要查看執行結果,須要將執行結果註冊爲變量,經過debug模塊輸出
command模塊是ansible默認執行模塊,與shell模塊的區別是不支持 shell 變量和管道
#ad-hoc ansible test -a 'hostname' #playbooks - hosts: "test" gather_facts: no tasks: - name: command command: "hostname" register: result - name: debug debug: var=result.stdout verbosity=0
遠程執行命令
#ad-hoc ansible test -m raw -a 'hostname' #playbooks - hosts: "test" gather_facts: no tasks: - name: raw raw: "hostname" register: result - name: debug debug: var=result.stdout verbosity=0
shell、command、raw模塊的區別
command通常用於執行簡單的命令,不支持shell變量和管道。shell能夠執行復雜的命令和目標主機上的腳本文件,和在目標主機上直接使用shell相似。raw和shell相似,而且能夠用於Windows服務器。raw和shell、command的另外一個區別是:在默認開啓ssh日誌時,raw會輸出ssh鏈接出現的錯誤,而shell、command不會
實現主控端向目標主機拷貝文件,相似於scp的功能
#ad-hoc ansible test -m copy -a "src=/etc/hosts dest=/tmp/hosts" #playbooks - hosts: "test" gather_facts: no tasks: - name: copy copy: src=/etc/hosts dest=/tmp/hosts
注意:
文件沒有變化時(md5沒有改變),ansible將不會進行文件複製操做
能夠加backup參數用於備份文件
ansible test -m copy -a 'src=/etc/hosts dest=/tmp/hosts backup=yes'
用於操做文件,若是建立、刪除、修改文件所屬人和權限、建立軟硬連接
建立文件 ansible test -m file -a "dest=/tmp/file.txt state=touch" 更改文件的用戶及權限 ansible test -m file -a "dest=/tmp/file.txt mode=600 owner=test group=root" 建立目錄 ansible test -m file -a "dest=/tmp/file mode=755 owner=test group=root state=directory" 刪除文件或者目錄 ansible test -m file -a "dest=/tmp/file state=absent" #playbooks - hosts: "test" gather_facts: no tasks: - name: file file: dest=/tmp/file.txt state=touch
template使用了Jinjia2格式做爲文件模板,進行文檔內變量的替換的模塊,它的每次使用都會被ansible標記爲changed狀態。
- hosts: "test" gather_facts: no vars: my_name: test tasks: - name: template template: src=hello.j2 dest=/tmp/hello.txt
獲取遠程文件狀態信息,包含atime、ctime、mtime、md五、uid、gid等
ansible test -m stat -a 'path=/tmp/hello.txt'
管理軟件
安裝 ansible test -m yum -a "name=httpd enablerepo=base state=latest" - hosts: "test" gather_facts: no tasks: - name: yum yum: name=httpd enablerepo=base state=latest 卸載 ansible test -m yum -a "name=httpd state=absent"
建立、刪除用戶
建立 ansible test -m user -a "name=test password= $6$YyF5qLN8$edF1l.d/xcd9kv4ZQD/VVq5g2Uavlwoo/l.W4YVIQgsNghN4CbJKSEdZ5ihxztkYJ.bZV2PCP6MnGOioSLqUK." 刪除 ansible test -m user -a "name=test state=absent"
管理服務
啓動 ansible test -m service -a "name=httpd state=started" 重啓 ansible test -m service -a "name=httpd state=restarted" 中止 ansible test -m service -a "name=httpd state=stopped"
管理定時任務
建立 ansible test -m cron -a 'name="job" minute=*/3 hour=* day=* month=* weekday=* job="/usr/sbin/ntpdate time4.aliyun.com"' 刪除 ansible test -m cron -a 'name="job" minute=*/3 hour=* day=* month=* weekday=* job="/usr/sbin/ntpdate time4.aliyun.com" state=absent'
管理用戶組
ansible test -m group -a 'gid=2017 name=grouptest'
遠程執行腳本,腳本文件在控制節點上
ansible test -m script -a '/root/a.sh' - hosts: "test" gather_facts: no tasks: - name: script script: "/root/a.sh"
下載,可以使用代理
ansible test -m get_url -a "url=http://www.baidu.com dest=/tmp"
同步控制節點和目標節點
將主控方目錄推送到指定節點目錄下 ansible test -m synchronize -a 'src=/root/push dest=/tmp/ compress=yes' # delete=yes 使兩邊的內容同樣(即以推送方爲主) # compress=yes 開啓壓縮,默認爲開啓 # –exclude=.Git 忽略同步.git結尾的文件 將指定節點的目錄拉取到控制結點下 ansible test -m synchronize -a 'mode=pull src=/tmp/pull dest=/root/'
複製遠程文件,傳輸至本機
ansible test -m fetch -a 'src=/tmp/fetch.txt dest=/root/ backup=yes'
修改文件內容
- hosts: "test" gather_facts: no tasks: - name: lineinfile lineinfile: dest=/tmp/test.txt regexp='^aa' line='aa=change'
刪除某一行
- hosts: "test" gather_facts: no tasks: - name: lineinfile lineinfile: dest=/tmp/test.txt regexp='^aa' state=absent
獲取目標主機的信息,如內存大小、操做系統等
ansible test -m setup -a 'filter=*distribut*' playbooks中,gather_fact爲yes時,將默認執行setup操做
ansible 2.0版本後,api的使用有較大的變化。如下提供兩個示例。
1. ad-hoc,直接執行命令
# coding=utf-8 from collections import namedtuple from ansible.parsing.dataloader import DataLoader from ansible.vars import VariableManager from ansible.inventory import Inventory from ansible.playbook.play import Play from ansible.executor.task_queue_manager import TaskQueueManager from ansible.plugins.callback import CallbackBase from tempfile import NamedTemporaryFile import os # 用於處理執行結果的回調 class ResultsCollector(CallbackBase): def __init__(self, *args, **kwargs): super(ResultsCollector, self).__init__(*args, **kwargs) self.host_ok = {} self.host_unreachable = {} self.host_failed = {} def v2_runner_on_unreachable(self, result): self.host_unreachable[result._host.get_name()] = result def v2_runner_on_ok(self, result, *args, **kwargs): self.host_ok[result._host.get_name()] = result def v2_runner_on_failed(self, result, *args, **kwargs): self.host_failed[result._host.get_name()] = result class MyRunner(object): """ This is a General object for parallel execute modules. """ def __init__(self, ): self.results_raw = {} def run(self, host_list, module_name, module_args=None, username=None, password=None, connection='ssh', forks=1, timeout=5,gather_facts='no'): """ run module from andible ad-hoc. module_name: ansible module_name module_args: ansible module args """ self.results_raw = {'contacted': {}, 'failed': {}, 'unreachable': {}} Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'timeout', 'remote_user', 'ask_pass', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'ask_value_pass', 'verbosity', 'check']) # 經過NamedTemporaryFile建立一個臨時文件,做爲hosts hosts_file = NamedTemporaryFile(delete=False) hoststr = '[%s]\n' % module_name for i in host_list: hoststr += i + '\n' hosts_file.write(hoststr) hosts_file.close() # initialize needed objects variable_manager = VariableManager() # 經過 variable_manager 增長額外的變量 # variable_manager.extra_vars = {'ansible_winrm_server_cert_validation': 'ignore'} loader = DataLoader() options = Options(connection=connection, module_path='/usr/share/ansible', forks=forks, timeout=timeout, remote_user=username, ask_pass=False, private_key_file=None, ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=None, become_method=None, become_user=None, ask_value_pass=True, verbosity=None, check=False) passwords = dict(conn_pass=password) # create inventory and pass to var manager inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=hosts_file.name) variable_manager.set_inventory(inventory) # create play with tasks if module_args is None: tasks = [dict(action=dict(module=module_name))] else: tasks = [dict(action=dict(module=module_name, args=module_args))] play_source = dict( name="Ansible Play", hosts=module_name, gather_facts=gather_facts, tasks=tasks ) play = Play().load(play_source, variable_manager=variable_manager, loader=loader) # actually run it tqm = None callback = ResultsCollector() try: tqm = TaskQueueManager( inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords, ) tqm._stdout_callback = callback result = tqm.run(play) finally: if tqm is not None: tqm.cleanup() os.remove(hosts_file.name) # 獲取執行結果 try: for host, result in callback.host_ok.items(): self.results_raw['contacted'][host] = result._result for host, result in callback.host_failed.items(): self.results_raw['failed'][host] = result._result['msg'] for host, result in callback.host_unreachable.items(): self.results_raw['unreachable'][host] = result._result['msg'] except Exception as e: print(str(e)) return self.results_raw if __name__ == '__main__': runner = MyRunner() result = runner.run( host_list=[''], module_name='setup', username='root', password='your_password' )
2.playbooks,用於執行playbooks
# coding=utf-8 import logging from ansible.parsing.dataloader import DataLoader from ansible.vars import VariableManager from ansible.inventory import Inventory from ansible.executor.playbook_executor import PlaybookExecutor from ansible.plugins.callback import CallbackBase class ResultsCollector(CallbackBase): def __init__(self, *args, **kwargs): super(ResultsCollector, self).__init__(*args, **kwargs) self.hosts = [] self.task_result = {} def v2_runner_on_ok(self, result, *args, **kwargs): hostname = result._host.get_name() logging.info("%s %s" % (hostname, "ok")) def v2_runner_on_skipped(self, result, *args, **kwargs): hostname = result._host.get_name() logging.info("%s %s" % (hostname, "skipped")) def v2_runner_on_failed(self, result, *args, **kwargs): hostname = result._host.get_name() logging.error("%s %s %s" % (hostname, "failed", result._result['msg'])) def v2_runner_on_unreachable(self, result, *args, **kwargs): hostname = result._host.get_name() logging.error("%s %s %s" % (hostname, "unreachable", result._result['msg'])) def v2_playbook_on_task_start(self, task, is_conditional): logging.info("play task %s" % (task.get_name())) def v2_playbook_on_stats(self, stats): for host in self.hosts: self.task_result[host.get_name()] = stats.summarize(host.get_name()) class Options(object): def __init__(self): self.connection = "ssh" self.forks = 1 self.check = False self.remote_user = 'root' self.tags = dict() self.skip_tags = dict() def __getattr__(self, name): return None def deploy(host_list, playbook,passwd=None,mirror=False): passwd = passwd loader = DataLoader() # 用來加載解析yaml文件或JSON內容,而且支持vault的解密 variable_manager = VariableManager() # 管理變量的類,包括主機,組,擴展等變量,以前版本是在 inventory 中的 inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=host_list) # variable_manager.set_inventory(inventory) # 根據 inventory 加載對應變量 playbooks = [playbook] options = Options() pb = PlaybookExecutor(playbooks=playbooks, inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwd) callback = ResultsCollector() # 經過mirror參數控制是否將執行結果返回到回調中 if not mirror: callback.hosts = inventory.get_hosts() pb._tqm._stdout_callback = callback result_no_callback = pb.run() if not mirror: return callback.task_result else: return result_no_callback if __name__ == '__main__': result = deploy('/etc/ansible/hosts','test.yml') print(result)
檢測工具
ansible-profile插件能夠顯示ansible-playbook 執行每個任務所花費的時間,地址:https://github.com/jlafon/ansible-profile
安裝:
cd /etc/ansible mkdir callback_plugins cd callback_plugins wget https://raw.githubusercontent.com/jlafon/ansible-profile/master/callback_plugins/profile_tasks.py
優化配置
1. 關閉 gathering facts
2. SSH PIPElinING。SSH pipelining 是一個加速 Ansible 執行速度的簡單方法。ssh pipelining 默認是關閉,之因此默認關閉是爲了兼容不一樣的 sudo 配置,主要是 requiretty 選項。若是不使用 sudo,建議開啓。打開此選項能夠減小 ansible 執行沒有傳輸時 ssh 在被控機器上執行任務的鏈接數。不過,若是使用 sudo,必須關閉 requiretty 選項。修改 /etc/ansible/ansible.cfg 文件能夠開啓
pipelining。pipelining修改成True
3. ControlPersist。ControlPersist 特性須要高版本的 SSH 才支持,CentOS 6 默認是不支持的,若是須要使用,須要自行升級 openssh。ControlPersist 即持久化 socket,一次驗證,屢次通訊。ControlPersist 設置的辦法
cat ~/.ssh/config Host * Compression yes ServerAliveInterval 60 ServerAliveCountMax 5 ControlMaster auto ControlPath ~/.ssh/sockets/%r@%h-%p ControlPersist 4h
使用async和poll能夠並行運行一個任務. async觸發ansible並行運做任務,是ansible等待運行這個任務的最大超時值;poll爲檢查這個任務是否完成的頻率時間。poll爲0時,不等待任務執行完畢直接結束
- hosts: test gather_facts: yes tasks: - name: poll shell: sleep 10 async: 4 poll: 2
- hosts: test gather_facts: yes tasks: - name: poll shell: sleep 10 async: 40 poll: 2
Windows
ansible不能部署在Windows服務器上用於控制主機,但能夠操做Windows主機。
ansible操做Windows主機的依賴:
1. Windows系統版本爲windows server 2008 r2以上(windows server 2012最佳)
2. powershell版本3.0以上
3. 開啓WINRM服務,並設置powershell執行選項。可用此腳本開啓
4. 詳細信息可見官方文檔
hosts示例,須要增長ansible_connection=winrm和ansible_winrm_server_cert_validation參數
[windows] windows_host ansible_user=Administrator ansible_ssh_pass=your_pass ansible_connection=winrm ansible_winrm_server_cert_validation=ignore
一些在Linux上使用的模塊,沒法在Windows上使用,Windows可以使用的模塊可參考官方文檔