ansible-playbook使用示例参考
ansible-playbook参考(1)基础示例
1
2
3
4
5
6
7
8
~]# vim base.yaml
- hosts: 192.168.1.114
remote_user: root
tasks:
- name: install httpd server
yum: name=httpd state=present
- name: start httpd server
service: name=httpd state=started
(2)handlers示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
~]# vim handlers.yaml
- hosts: 192.168.1.114
remote_user: root
tasks:
- name: install httpd
yum: name=httpd state=present
- name: install configure file
copy: src=file/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify:
- restart httpd server
handlers:
- name: restart httpd server
service: name=httpd state=restarted
~]# vim file/httpd.conf
修改Listen 80为Linsten 8080
~]# ansible-playbook --check handlers.yaml
(3)tags示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
~]# vim tags.yaml
- hosts: 192.168.1.114
remote_user: root
tasks:
- name: install httpd
yum: name=httpd state=present
- name: install configure file
copy: src=file/httpd.conf dest=/etc/httpd/conf/httpd.conf
tags: instconf
notify:
- restart httpd server
handlers:
- name: restart httpd server
service: name=httpd state=restarted
~]# ansible-playbook tags.yaml -t instconf
(4)variables示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
~]# vim variables.yaml
- hosts: 192.168.1.114
remote_user: root
tasks:
- name: install {{ package }}
yum: name={{ package }} state=present
a.
直接通过fact调用,使用setup模块可以获取
b.
~]# ansible-playbook variables.yaml -e package=httpd
c.
~]# vim /etc/ansible/hosts
192.168.1.114 package=httpd
~]# ansible-playbook variables.yaml
页:
[1]