1、安裝apache,做初始配置,并啟動服務(wù):
這個是你選擇的主機
- hosts: webservers
這個是變量
vars:
http_port: 80
max_clients: 200
遠端的執(zhí)行權(quán)限
remote_user: root
tasks:
利用yum模塊來操作
-
name: ensure apache is at the latest version
yum: pkg=httpd state=latest
-
name: write the apache config file
template: src=/srv/httpd.j2 dest=/etc/httpd.conf
觸發(fā)重啟服務(wù)器
notify:
- restart apache
-
name: ensure apache is running
service: name=httpd state=started
這里的restart apache 和上面的觸發(fā)是配對的。這就是handlers的作用。相當(dāng)于tag
handlers:
- name: restart apache
service: name=httpd state=restarted
2、可以同時使用10個進程進行,調(diào)用格式為:
ansible-playbook test.yml -f 10
3、對于沒有把握執(zhí)行的任務(wù),需要加上 ignore_errors: True,這樣即使出錯,下一個任務(wù)也會繼續(xù)執(zhí)行
4、ansible-playbook可以根據(jù)上一個任務(wù)的執(zhí)行結(jié)果,來判斷執(zhí)行下一個任務(wù),系統(tǒng)參數(shù)為when:
tasks:
-
shell: /usr/bin/foo
register: foo_result
ignore_errors: True
-
name: "cmd"
action: command touch /root/kkkkk
when: foo_result.rc == 127
5、根據(jù)操作系統(tǒng)類型,執(zhí)行不同安裝:
- hosts: all
remote_user: root
gather_facts:True
tasks: - name: install apache on CentOS
yum: name=httpd state=present
when: ansible_os_family =="CentOS" - name: install apache on Debian
yum: name=apache2 state=present
when: ansible_os_family =="Debian"
6、notify和handlers來執(zhí)行觸發(fā),handlers可多次調(diào)用:
- name: template configuration file
template: src=template.j2 dest=/etc/foo.conf
notify:- restart memcached
- restart apache
handlers: - name: restart memcached
service: name=memcached state=restarted - name: restart apache
service: name=apache state=restarted
7、執(zhí)行多個變量,每個變量為一次任務(wù):
tasks:
- name: panduan
command: echo {{ item }}
with_items: [ 0,2,4,6,8,10 ]
8、特定任務(wù)用特定用戶執(zhí)行:
- hosts: webnodes
remote_user: mageedu
tasks:- name: test connection
ping:
remote_user: mageedu
sudo: yes
- name: test connection
9、捕獲執(zhí)行中的錯誤,并進行補救:
tasks:
- block:
- name: Create {{ maildir_path }}
copy:
src: "{{ maildir }}"
dest: "{{ maildir_path }}"
mode: 0755
register: command_output
rescue: - name: Install mail packages
yum:
name: "{{ item }}"
state: latest
with_items:- "{{ mail_package }}"
- dovecot
always:
- name: start the mail service
service:
name: "{{ mail_service }}"
state: restarted
- name: Create {{ maildir_path }}
10、對任務(wù)做tag標記,可以只執(zhí)行某個tag,執(zhí)行語法為:ansible-playbook -t TAGS_NAME playbook.yaml
- hosts: all #所有遠程主機
remote_user: root #以遠程主機上root用戶執(zhí)行
tasks: #任務(wù)- name: install redis #任務(wù)之安裝
yum: name=redis state=latest #動作調(diào)用yum模塊安裝 - name: copy config file #任務(wù)之復(fù)制同步配置文件到遠程目標主機
copy: src=/root/playbooks/redis.conf dest=/etc/redis.conf owner=redis #動作copy模塊執(zhí)行
notify: restart redis #觸發(fā)的動作
tags: configfile #任務(wù)標記名configfile
- name: install redis #任務(wù)之安裝
11、從外部傳入變量,ansible-playbook的格式為:ansible-playbook tomcat-install.yml --extra-vars "{'host':'192.168.11.111', 'tomcat_home':'/opt/tomcat-test', 'url':'http://www.apache.com/tomcat-7.0.57.tar.gz'}"
-
name: Tomcat install and configuration
hosts: "{{ host }}"
user: root
vars:
tomcat_home: "{{ tomcat_home }}"
tasks:
- name: absent old tomcat
file: path={{ item }} state=absent
with_items:
- "{{ tomcat_home }}"
- /geelyapp/auto_scripts/tomcat.sh
- name: get tomcat_tar_gz
get_url: url={{ url }} dest=/tmp/tomcat.tar.gz
- name: Create the dir
file: path={{ item }} state=directory
with_items:
- /geelyapp/gc_log
- /geelyapp/auto_scripts
- "{{ tomcat_home }}"