1. Patterns
在Ansible中,Patterns 是指我們怎樣確定由哪一臺主機來管理. 意思就是與哪臺主機進行交互.
ansible <pattern_goes_here> -m <module_name> -a <arguments>
ansible webservers -m service -a "name=httpd state=restarted"
同時讓我們提前了解一些技能,除了如上,你也可以通過--limit
標記來添加排除條件,/usr/bin/ansible or /usr/bin/ansible-playbook都支持:
ansible-playbook site.yml --limit datacenter2
如果你想從文件讀取hosts,文件名以@為前綴即可.從Ansible 1.2開始支持該功能:
ansible-playbook site.yml --limit @retry_hosts.txt
tasks:- name: make sure apache is runningservice: name=httpd state=running
tasks:- name: disable selinuxcommand: /sbin/setenforce 0
tasks:- name: run this command and ignore the resultshell: /usr/bin/somecommand || /bin/true
tasks:- name: run this command and ignore the resultshell: /usr/bin/somecommandignore_errors: True
tasks:- name: Copy ansible inventory file to clientcopy: src=/etc/ansible/hosts dest=/etc/ansible/hostsowner=root group=root mode=0644
tasks:- name: create a virtual host file for {{ vhost }}template: src=somefile.j2 dest=/etc/httpd/conf.d/{{ vhost }}
template: src=templates/foo.j2 dest=/etc/foo.conf
- name: template configuration filetemplate: src=template.j2 dest=/etc/foo.confnotify:- restart memcached- restart apache
handlers:- name: restart memcachedservice: name=memcached state=restarted- name: restart apacheservice: name=apache state=restarted
tasks:- shell: some tasks go here- meta: flush_handlers- shell: some other tasks
ansible-playbook playbook.yml -f 10
ansible-playbook playbook.yml --list-hoststasks: - include: wordpress.yml wp_user=timmy - include: wordpress.yml wp_user=alice - include: wordpress.yml wp_user=bob
- name: this is a play at the top level of a filehosts: allremote_user: roottasks:- name: say hitags: fooshell: echo "hi..."- include: load_balancers.yml - include: webservers.yml - include: dbservers.yml
這個 playbook 為一個角色 ‘x’ 指定了如下的行為:
- 如果 roles/x/tasks/main.yml 存在, 其中列出的 tasks 將被添加到 play 中
- 如果 roles/x/handlers/main.yml 存在, 其中列出的 handlers 將被添加到 play 中
- 如果 roles/x/vars/main.yml 存在, 其中列出的 variables 將被添加到 play 中
- 如果 roles/x/meta/main.yml 存在, 其中列出的 “角色依賴” 將被添加到 roles 列表中 (1.3 and later)
- 所有 copy tasks 可以引用 roles/x/files/ 中的文件,不需要指明文件的路徑。
- 所有 script tasks 可以引用 roles/x/files/ 中的腳本,不需要指明文件的路徑。
- 所有 template tasks 可以引用 roles/x/templates/ 中的文件,不需要指明文件的路徑。
- 所有 include tasks 可以引用 roles/x/tasks/ 中的文件,不需要指明文件的路徑。
site.yml webservers.yml fooservers.yml roles/common/files/templates/tasks/handlers/vars/defaults/meta/webservers/files/templates/tasks/handlers/vars/defaults/meta/
角色默認變量允許你為 included roles 或者 dependent roles(見下) 設置默認變量。要創建默認變量,只需在 roles 目錄下添加 defaults/main.yml 文件。這些變量在所有可用變量中擁有最低優先級,可能被其他地方定義的變量(包括 inventory 中的變量)所覆蓋。
wheel 角色的 meta/main.yml 文件包含如下內容:
---
allow_duplicates: yes
dependencies:
- { role: tire }
- { role: brake }
最終的執行順序是這樣的:
tire(n=1) brake(n=1) wheel(n=1) tire(n=2) brake(n=2) wheel(n=2) ... car
?- hosts: webservers
?? vars:?
????? http_port: 80
template: src=foo.cfg.j2 dest={{ remote_install_path }}/foo.cfg
- hosts: app_serversvars:app_path: "{{ base_path }}/22"
- hosts: allremote_user: root# here we make a variable named "proxy_env" that is a dictionaryvars:proxy_env:http_proxy: http://proxy.example.com:8080tasks:- apt: name=cobbler state=installedenvironment: proxy_env
ntp_server: ntp.bos.example.com backup: bak.bos.example.com proxy_env:http_proxy: http://proxy.bos.example.com:8080https_proxy: http://proxy.bos.example.com:8080
yum install redis service redis start pip install redis
ansible-playbook release.yml --extra-vars "hosts=vipers user=starbuck"
ansible-playbook playbook.yml --start-at="install packages"
ansible-playbook playbook.yml --step
在Playbook中,如果寫gather_facts來控制是否收集遠程系統的信息.如果不收集系統信息,那么上面的變量就不能在該playybook中使用了.
- hosts: whatever
gather_facts: no
如果收集:
使用復雜facts變量
一般在系統中收集到如下的信息,復雜的、多層級的facts變量如何使用呢?
..."ansible_ens3": {"active": true, "device": "ens3", "ipv4": { "address": "10.66.192.234", "netmask": "255.255.254.0", "network": "10.66.192.0" }, "ipv6": [ { "address": "2620:52:0:42c0:5054:ff:fef2:e2a3", "prefix": "64", "scope": "global" }, { "address": "fe80::5054:ff:fef2:e2a3", "prefix": "64", "scope": "link" } ], "macaddress": "52:54:00:f2:e2:a3", "module": "8139cp", "mtu": 1500, "promisc": false, "type": "ether" }, ...
那么可以通過下面的兩種方式訪問復雜的變量中的子屬性:
中括號:
{{ ansible_ens3["ipv4"]["address"] }}
點號:
{{ ansible_ens3.ipv4.address }}