roles做爲ansible重要的企業實戰解決方案。nginx
--- - hosts: websrvs remote_user: root tasks: - name: create some files file: name=/data/{{ item }} state=touch with_items: - file1 - file2 when: ansible_distribution_major_version == "7"
--- - hosts: websrvs remote_user: root tasks: - name: create some groups group: name={{item}} when: ansible_distribution_major_version == "7" with_items: - g1 - g2 - g3 - name: create some users user: name={{item.name}} group={{item.group}} when: ansible_distribution_major_version == "7" with_items: - { name: 'user1',group: 'g1'} - { name: 'user1',group: 'g2'} - { name: 'user1',group: 'g3'}
--- - hosts websrvs remote_user: root vars: #定義一個列表類型的變量,名稱爲ports ports: - 81 - 82 - 83 tasks: - name: copy conf file template: src=for1.conf.j2 dest=/data/for1.conf
touch for1.conf.j2
循環建立配置,而且能夠使用if條件判斷web
{% for port in ports %} server{ listem {{ port }} } {% endfor %}
- hosts: websrvs remote_user: root roles: - role: httpd - role: nginx
main.ymlcode
- include: temp.yml - include: roles/htpd/tasks/copyfile.yml
- hosts: websrvs remote_user: root roles: - { role: httpd, tasg: ['web',httpd'] } - { role: nginx, tags: ['web','nginx'],when: ansible_distribution_major_version == "7" }
ansible-playbook -t web some_role.yml