ansible-playbook使用例リファレンス

1521 ワード

ansible-playbookリファレンス
(1)基本例
~]# 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例
~]# 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例
~]# 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例
~]# 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
[websrvs]
192.168.1.114 package=httpd
~]# ansible-playbook variables.yaml