001-Ansible-参考http://www.ansible.com.cn/docs/playbooks_intro.html#about-playbooks
生活随笔
收集整理的這篇文章主要介紹了
001-Ansible-参考http://www.ansible.com.cn/docs/playbooks_intro.html#about-playbooks
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1. Patterns
在Ansible中,Patterns 是指我們?cè)鯓哟_定由哪一臺(tái)主機(jī)來(lái)管理. 意思就是與哪臺(tái)主機(jī)進(jìn)行交互.
ansible <pattern_goes_here> -m <module_name> -a <arguments> ansible webservers -m service -a "name=httpd state=restarted"同時(shí)讓我們提前了解一些技能,除了如上,你也可以通過(guò) --limit 標(biāo)記來(lái)添加排除條件,/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
這個(gè) playbook 為一個(gè)角色 ‘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/ 中的文件,不需要指明文件的路徑。
角色默認(rèn)變量允許你為 included roles 或者 dependent roles(見(jiàn)下) 設(shè)置默認(rèn)變量。要?jiǎng)?chuàng)建默認(rèn)變量,只需在 roles 目錄下添加 defaults/main.yml 文件。這些變量在所有可用變量中擁有最低優(yōu)先級(jí),可能被其他地方定義的變量(包括 inventory 中的變量)所覆蓋。
wheel 角色的 meta/main.yml 文件包含如下內(nèi)容:
--- allow_duplicates: yes dependencies: - { role: tire } - { role: brake }最終的執(zhí)行順序是這樣的:
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_envntp_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來(lái)控制是否收集遠(yuǎn)程系統(tǒng)的信息.如果不收集系統(tǒng)信息,那么上面的變量就不能在該playybook中使用了.
- hosts: whatever gather_facts: no如果收集:
使用復(fù)雜facts變量
一般在系統(tǒng)中收集到如下的信息,復(fù)雜的、多層級(jí)的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" }, ...那么可以通過(guò)下面的兩種方式訪問(wèn)復(fù)雜的變量中的子屬性:
中括號(hào):
{{ ansible_ens3["ipv4"]["address"] }}點(diǎn)號(hào):
{{ ansible_ens3.ipv4.address }}轉(zhuǎn)載于:https://www.cnblogs.com/XilingCai/p/6544948.html
總結(jié)
以上是生活随笔為你收集整理的001-Ansible-参考http://www.ansible.com.cn/docs/playbooks_intro.html#about-playbooks的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 功耗最高下降45%:OPPO Watch
- 下一篇: JMS-activMq与spring进行