---
# Requires ansible 1.8+ - name: 'YUM - fire and forget task' yum: name=docker-io state=installed async: 1000 poll: 0 register: yum_sleeper - name: 'YUM - check on fire and forget task' async_status: jid={{ yum_sleeper.ansible_job_id }} register: job_result until: job_result.finished retries: 30
在檢查模式狀態下,是不會在遠程系統上作出任何改變的。javascript
ansible-playbook foo.yml --check
ignore_errors 模塊能夠在任務執行錯誤時,忽略錯誤並繼續執行任務。php
tasks:
- command: /bin/false ignore_errors: true - debug: msg="false"
- name: test play hosts: webservers serial: 3
在上面的例子中,若是咱們有100個主機,組「webservers」中的3個主機將完成playbook,而後再移動到接下來的3個主機。css
還能夠使用百分比java
serial: "30%"
默認狀況下,只要組中有還沒有失敗的主機,Ansible將繼續執行操做。 在一些狀況下,例如利用上述滾動更新,可能但願在達到失敗的特定閾值時停止任務。node
- hosts: webservers max_fail_percentage: 30 serial: 10
- hosts: app_servers
tasks:
- name: gather facts from db servers setup: delegate_to: "{{item}}" delegate_facts: True with_items: "{{groups['dbservers']}}"
以上將爲dbservers組中的機器收集facts,並將facts分配給這些機器,而不是app_servers。 這樣您能夠查找hostvars ['dbhost1'] ['default_ipv4_addresses'] [0],即便dbserver不是play的一部分,或者使用-limit省略。python
---
tasks:
- command: /opt/application/upgrade_db.py run_once: true
---
- hosts: web
any_errors_fatal: True
tasks:
- apt: name=cobbler state=installed
environment:
http_proxy: http://proxy.example.com:8080 PATH: /var/local/nvm/versions/node/v4.2.1/bin:{{ ansible_env.PATH }}
也能夠在play級別使用nginx
- hosts: testhost
roles:
- php
- nginx
environment:
http_proxy: http://proxy.example.com:8080
如下3中方法都可使用
• 命令行加上 --force-handlers 參數
• 配置文件加上 force_handlers = True
• playbook裏設置 force_handlers: Trueweb
- hosts: test
gather_facts: no
vars_prompt:
- name: "name" prompt: "what is your name?" default: "user" private: yes confirm: yes tasks: - debug: msg={{ name }}
依賴Passlib包docker
- hosts: test gather_facts: no vars_prompt: - name: "pass" prompt: "Enter password" private: yes encrypt: "sha512_crypt" confirm: yes salt_size: 7 tasks: - debug: msg={{ pass }}
可支持如下加密類型
• des_crypt - DES Crypt
• bsdi_crypt - BSDi Crypt
• bigcrypt - BigCrypt
• crypt16 - Crypt16
• md5_crypt - MD5 Crypt
• bcrypt - BCrypt
• sha1_crypt - SHA-1 Crypt
• sun_md5_crypt - Sun MD5 Crypt
• sha256_crypt - SHA-256 Crypt
• sha512_crypt - SHA-512 Crypt
• apr_md5_crypt - Apache’s MD5-Crypt variant
• phpass - PHPass’ Portable Hash
• pbkdf2_digest - Generic PBKDF2 Hashes
• cta_pbkdf2_sha1 - Cryptacular’s PBKDF2 hash
• dlitz_pbkdf2_sha1 - Dwayne Litzenberger’s PBKDF2 hash
• scram - SCRAM Hash
• bsd_nthash - FreeBSD’s MCF-compatible nthash encodingbash
標記一個任務
tasks:
- yum: name={{ item }} state=installed
with_items:
- httpd
- memcached
tags:
- packages
- template: src=templates/src.j2 dest=/etc/foo.conf tags: - configuration
標記playbook
- hosts: all
tags:
- bar
tasks:
...
- hosts: all
tags: ['foo'] tasks: ...
標記roles
roles: - { role: webserver, port: 5000, tags: [ 'web', 'foo' ] }
標記包含
- include: foo.yml tags: [web,foo]
始終運行標記的任務
tasks:
- debug: msg="Always runs" tags: - always
還有另外3個特殊關鍵字用於標籤,'tagged','untagged'和'all',它們分別是僅運行已標記,只有未標記和全部任務。
默認狀況下ansible運行就像指定了'-tags all
'。運行playbook中的未標記任務 -tags untagged
顯示playbook中的全部標記任務
ansible-playbook example.yml --list-tags
執行全部標記名稱爲packages的任務
ansible-playbook example.yml --tags packages
跳過全部標記名稱爲notification的任務
ansible-playbook example.yml --skip-tags "notification"
等待端口可用,才能執行任務
- wait_for: port=8000 delay=10
等待直到鎖定文件被刪除
wait_for: path=/var/lock/file.lock state=absent