ansible之playbook

 

1.playbook的语法格式如下:  - hosts:  webservers 是定义的主机组 也就是playbook中的 play 角色的意思 注意":" 冒号后面也必须价格空格不然就报错

  tasks: 注意 他前面是有2个空格的, ansible 是用空格来区分规格的, 记住不能使用tab按键

[root@m01 ~]# mkdir project1
[root@m01 ~]# cd project1/
[root@m01 project1]# cat p1.yml 
---
#play
- hosts: webservers

  tasks:
    - name: Installed Httpd Server
      yum:
        name: httpd
        state: present

    - name: Start Httpd Server
      systemd:
        name: httpd
        state: started
        enabled: yes

 

2.检查语法,只检查是否是yaml语法格式。并不做逻辑校验。  (记住这个要经常使用,他是判断语法是否正确!!!)

[root@m01 project1]# ansible-playbook --syntax-check p1.yml 

 

3.模拟执行(不是真的执行)

[root@m01 project1]# ansible-playbook -C  p1.yml 

 

4.真实的描述状态(被控端的状态必须与控制端描述的状态一致)

[root@m01 project1]# ansible-playbook p1.yml
[root@m01 project1]# cat p1.yml 
---
#play
- hosts: webservers

  tasks:
    - name: Installed Httpd Server
      yum: name=httpd state=present

    - name: Start Httpd Server
      systemd: name=httpd state=started enabled=yes

    - name: Start Firewalld Server
      systemd: name=firewalld state=started enabled=yes

    - name: Configure Firewalld Server
      firewalld: service=http immediate=yes permanent=yes state=enabled

- hosts: web01
  tasks:
    - name: Configure web01 Website
      copy: content='This is Web01' dest=/var/www/html/index.html

- hosts: web02
  tasks:
    - name: Cofnigure webi-2 weisite
      copy: content='This is Web02' dest=/var/www/html/index.html
多paly语法示例

相关文章: