本文共 3011 字,大约阅读时间需要 10 分钟。
1、安装apache,做初始配置,并启动服务:
vars:
http_port: 80max_clients: 200
remote_user: root
tasks:
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
notify:- restart apache
name: ensure apache is running
service: name=httpd state=started
handlers:
- name: restart apache service: name=httpd state=restarted
2、可以同时使用10个进程进行,调用格式为:
ansible-playbook test.yml -f 103、对于没有把握执行的任务,需要加上 ignore_errors: True,这样即使出错,下一个任务也会继续执行
4、ansible-playbook可以根据上一个任务的执行结果,来判断执行下一个任务,系统参数为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、根据操作系统类型,执行不同安装:
6、notify和handlers来执行触发,handlers可多次调用:
7、执行多个变量,每个变量为一次任务:
tasks:8、特定任务用特定用户执行:
9、捕获执行中的错误,并进行补救:
tasks:10、对任务做tag标记,可以只执行某个tag,执行语法为:ansible-playbook -t TAGS_NAME playbook.yaml
11、从外部传入变量,ansible-playbook的格式为:ansible-playbook tomcat-install.yml --extra-vars "{'host':'192.168.11.111', 'tomcat_home':'/opt/tomcat-test', 'url':'}"
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 }}"
转载地址:http://xiill.baihongyu.com/