Ansible Playbooksѧϰ

Ansible��Playbooks��Ansible�������ã�����Ӧ�õĽṹ�����ԡ�Ansible��ģ��ͺñ�shell�����ôplaybooks�ͺñ�shell�ű����ڽű���ָ����ôʹ����Щ�����ټ���һЩ�ж����ȵȡ�

Playbooksʹ��YAML�ļ�����ʾִ�в��衣



---
- hosts: webservers
  vars:
    http_port: 80
    max_clients: 200
  remote_user: root
  tasks:
  - name: ensure apache is at the latest version
    yum: name=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 (and enable it at boot)
    service: name=httpd state=started enabled=yes
  handlers:
    - name: restart apache
      service: name=httpd state=restarted


Ҳ����д������

---
- hosts: webservers
  vars:
    http_port: 80
    max_clients: 200
  remote_user: root
  tasks:
  - name: ensure apache is at the latest version
    yum:
      name: 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


Playbooks��Ҳ���԰���plays��

---
- hosts: webservers
  remote_user: root

  tasks:
  - name: ensure apache is at the latest version
    yum: name=httpd state=latest
  - name: write the apache config file
    template: src=/srv/httpd.j2 dest=/etc/httpd.conf
    
    
- hosts: databases
  remote_user: root

  tasks:
  - name: ensure postgresql is at the latest version
    yum: name=postgresql state=latest
  - name: ensure that postgresql is started
    service: name=postgresql state=running


hosts ��ָ��ƥ�����������������Զ���","�ָ�

remote_user ָ��Զ��ִ��task������û�

       remote_user��Ansible1.4֮ǰ������user

Ҳ����Ϊÿ��task����ָ��Զ��ִ���û�

---
- hosts: webservers
  remote_user: root
  tasks:
    - name: test connection
      ping:
      remote_user: yourname


ʹ����Ȩ�û�ִ��

---
- hosts: webservers
  remote_user: yourname
  become: yes


Ϊ����taskָ��become

---
- hosts: webservers
  remote_user: yourname
  tasks:
    - service: name=nginx state=started
      become: yes
      become_method: sudo


�������û���¼Ȼ����root������û�ִ��


---
- hosts: webservers
  remote_user: yourname
  become: yes
  become_user: postgres



---
- hosts: webservers
  remote_user: yourname
  become: yes
  become_method: su



�����б� Task lists

tasks:
  - name: make sure apache is running
    service: name=httpd state=running


ÿ��play������һϵ��tasks��command��shellģ�����ֻ��������д��key=value����ʽ

tasks:
  - name: disable selinux
    command: /sbin/setenforce 0


ÿ����Ҫִ�е�task������Ҫ��һ��name���ڱ�ʾִ�в���


command��shellģ���ϵ�����룬���������ִ�гɹ��˳��벻��0����������

tasks:
  - name: run this command and ignore the result
    shell: /usr/bin/somecommand || /bin/true


���ߣ�

tasks:
  - name: run this command and ignore the result
    shell: /usr/bin/somecommand
    ignore_errors: True


���ִ����̫�����Է���д��

tasks:
  - name: Copy ansible inventory file to client
    copy: src=/etc/ansible/hosts dest=/etc/ansible/hosts
            owner=root group=root mode=0644


ִ�п���ʹ�ñ��������趨����һ��vhost�ı�����

tasks:
  - name: create a virtual host file for {{ vhost }}
    template: src=somefile.j2 dest=/etc/httpd/conf.d/{{ vhost }}



Handlers�� Running Operations On Change

- name: template configuration file
  template: src=template.j2 dest=/etc/foo.conf
  notify:
     - restart memcached
     - restart apache


notify���г�������ͽ���handlers

Handlers are lists of tasks,not really any different from regular tasks,that are refercenced by a globaly unique name.Handlers are what notifiers notify.

���û��֪ͨhandler,notify������ִ�С�

handlers:
    - name: restart memcached
      service: name=memcached state=restarted
    - name: restart apache
      service: name=apache state=restarted

Handlers����������������ʹ�����������


ִ��playbook

# ansible-playbook -i ansible_hosts install_apache.yml -f 10


ִ��playbook֮ǰ��ü��������Щ����ᱻִ�У���ֹ�����

# ansible-playbook -i ansible_hosts install_apache.yml -f 10  --list-hosts




ansible-pull

ansibleĬ������push�ķ�ʽ��������ansible��������ִ�����������ڵ㡣Ҳ����ʹ��pull�ķ�ʽ���ø����ڵ��������ȡ����ȻȻ��ִ�С�



Playbooks Roles and Include Statements


������Ҫ�ڶ��plays����playbooks֮������tasks������ʹ��include


---# possibly saved as tasks/foo.yml
- name: placeholder foo
  command: /bin/foo
- name: placeholder bar
  command: /bin/bar


include ָ���������ʹ��

tasks:

  - include: tasks/foo.yml



�����Ը�include���ݲ���

tasks:
  - include: wordpress.yml wp_user=timmy
  - include: wordpress.yml wp_user=alice
  - include: wordpress.yml wp_user=bob


Ҳ��������

tasks:

  - include: wordpress.yml
    vars:
        wp_user: timmy
        ssh_keys:
          - keys/one.txt
          - keys/two.txt


���ݵı��������ڱ�����ļ���ʹ�ã�ʹ�ñ����ķ�ʽ���£�

{{ wp_user }}









�ο��ĵ���

http://docs.ansible.com/ansible/playbooks.html

https://github.com/ansible/ansible-examples/blob/master/windows/deploy-site.yml

http://docs.ansible.com/ansible/intro_patterns.html


���ij��� ��Linux SA John�� ���ͣ�����ر����˳���http://john88wang.blog.51cto.com/2165294/1790656

你可能感兴趣的:(Ansible Playbooksѧϰ)