ansible-play

#1
- hosts: test
  user: root
  tasks:
  - name: create nginx group
    group: name=nginx system=yes gid=208
  - name: create nginx user
    user: name=nginx system=yes uid=208 group=nginx
    
- hosts: test
  user: root
  tasks:
  - name: copy file to test
    copy: src=/etc/inittab dest=/tmp/inittab.ans
    
    
    
handlers 处理器  由某事件触发执行的操作

#2
- hosts: test
  user: root
  tasks:
  - name: install httpd package
    yum: name=httpd state=present
  - name: install configuation file for httpd
    copy: src=/root/conf/httpd.conf  dest=/etc/httpd/conf/httpd.conf
  - name: start httpd service
    service: enabled=true  name=httpd state=started
    
 
#3 修改文件,才会触发重启
- hosts: test
  user: root
  tasks:
  - name: install httpd
    yum: name=httpd state=present
  - name: install configuation file for httpd
    copy: src=/root/conf/httpd.conf  dest=/etc/httpd/conf/httpd.conf
    notify:
    - restart httpd
  - name: start httpd service
    service: enabled=true name=httpd state=started
  handlers:
  - name: restart httpd
    service: name=httpd state=restarted
    
    
#4 playbook 使用变量
- hosts: test
  user: root
  vars:
  - package: httpd
  - service: httpd
  
  tasks:
  - name: install httpd
    yum: name={{ package }} state=present
  - name: install configuation file for httpd
    copy: src=/root/conf/httpd.conf  dest=/etc/httpd/conf/httpd.conf
    notify:
    - restart httpd
  - name: start httpd service
    service: enabled=true name={{ service }} state=started
  handlers:
  - name: restart httpd
    service: name=httpd state=restarted


#5  when 条件测试
- hosts: test
  user: root
  vars:
  - user: user100
  tasks:
  - name: create {{ user }} user
    user: name={{ user }}
    when: ansible_hostname == "mongo_slave1"  
    


- hosts: test
  user: root
  tasks:
  - name: copy file
    copy: content={{ ansible_os_family }} dest=/tmp/vars.test
    

- hosts: test
  user: root
  tasks:
  - name: copy file
    copy: content='{{ ansible_os_family }},{{ httpd_port  }}' dest=/tmp/vars.test


#迭代循环
- hosts: test
  tasks:
  - name: "install httpd"
    yum : name={{ item }} state=present
    with_items:
    - httpd
    - httpd-devel
  - name: "chkconfig"
    service: name=httpd state=started enabled=yes

    

- hosts: test
  tasks:
  - name: create user
    user: name={{ item }}
    with_items:
    - ysl01
    - ysl02
    - ysl03
    - ysl04
    - ysl05
    - ysl06
    
    
#template 模版
#[test]
#192.168.2.100 httpd_port=100 maxClient=1000
#192.168.2.106 httpd_port=106 maxClient=2000
 
- hosts: test
  user: root
  tasks:
  - name: install http
    yum: name=httpd state=present
  - name: copy file
    template: src=/root/templates/httpd.conf.j2  dest=/etc/httpd/conf/httpd.conf
    notify:
    - restart httpd
  - name: restart httpd
    service: name=httpd state=started
  handlers:
  - name: restart httpd
    service: name=httpd state=restarted
    
    
# tags 在playbook可以为某个任务定义一个标签, 在执行此playbook时,通过为ansible-playbook命令使用--tags选项能实现仅运行指定的tasks     而非所有 
    
- hosts: test
  remote_user: root
  vars:
  - package: httpd
  - service: httpd
  tasks:
  - name: install httpd package
    yum: name={{ package }} state=latest
  - name: install configuation file for httpd
    template: src=/root/templates/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
    tags:
    - conf
    notify:
    - restart httpd
  - name: start httpd service
    service: enabled=true name={{ service }} state=started
  handless:
  - name: restart httpd
    service: name=httpd start=restarted
    
    
  - name: install configuation file for httpd
    template: src=/root/templates/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
    tags:
    - conf

  

原文地址:https://www.cnblogs.com/augustyang/p/9238963.html