示例1:创建用户
---
- name: create user
hosts: all
gather_facts: false
tasks:
- name: create {{ item }}
user:
name: "{{ item }}"
groups: root
password: "$6$Okahzlzi8PqXN30d$Zd.jyW0A9jO3k09OMpEujhSn5EMpjHECfOIQGC2cS6HQBfAhncrVL/iaUmkuCi4LFWqr7KARyoVMJzKtBrBoZ/"
loop:
- alex
- jack
示例2:复制文件
---
- name: copy file
hosts: all
tasks:
- name: copy ansible.cfg file
copy:
src: /etc/ansible/ansible.cfg
dest: /mnt/
mode: '755'
示例3:编辑playbook,使得浏览器中输入相应域名得到对应结果
create web vhost
www.westos.com 80 ------ > /var/www/html ------> www.westos.com
linux.westos.com 80 ------> /var/www/virtual/westos.com/linux ----->linux.westos.com
vim webs.yml
---
webs:
- webname: www.westos.com
doc: /var/www/html
- webname: linux.westos.com
doc: /var/www/virtual/westos.com/linux
vim http.yml
---
- name: httpd
hosts: 172.25.73.10
vars_files: ./webs.yml
tasks:
- name: dnf install httpd
dnf:
name: httpd
state: present
- name: mkdir
file:
path: "{{item.doc}}"
state: directory
loop:
"{{webs}}"
- name: touch index.html
copy:
dest: "{{item.doc}}/index.html"
content: "{{item.webname}}"
loop:
"{{webs}}"
- name: clear config
file:
name: /etc/httpd/conf.d/vhosts.conf
state: absent
- name: configure vhost
lineinfile:
path: /etc/httpd/conf.d/vhosts.conf
create: yes
line: |+
<VirtualHost *:80>
DocumentRoot "{{item.doc}}"
ServerName "{{item.webname}}"
</VirtualHost>
loop:
"{{webs}}"
- lineinfile:
path: /etc/httpd/conf.d/vhosts.conf
line: |+
<VirtualHost _default_:80>
DocumentRoot /var/www/html
</VirtualHost>
insertbefore: BOF
- name: start httpd
service:
name: httpd
state: restarted
enabled: yes
- name: firewalld
firewalld:
service: http
permanent: yes
state: enabled
immediate: yes
未完待续…
|