playbook

编写yml文件
yum文件严格遵守两个空格缩进的格式。

  • 发布httpd并启动

    [devops@server1 ansible]$ cat playbook.yml 
    ---
    - hosts: prod
      tasks:
        - name: install httpd
          yum:
            name: httpd
            state: present
    
        - name: start httpd
          service:
            name: httpd
            state: started

[devops@server1 ansible]$ ansible-playbook playbook.yml --syntax-check##检测文件语法正确性
在这里插入图片描述

[devops@server1 ansible]$ ansible-playbook playbook.yml --list-task##检测任务
在这里插入图片描述
[devops@server1 ansible]$ ansible-playbook playbook.yml --list-hosts##检测生效主机
在这里插入图片描述

[devops@server1 ansible]$ ansible-playbook playbook.yml ##执行文件
在这里插入图片描述

server3查看结果
在这里插入图片描述

  • 添加配置文件
[devops@server1 ansible]$ cat playbook.yml
---
- hosts: prod
  tasks:
    - name: install httpd
      yum:
        name: httpd
        state: present

    - name: configure httpd
      copy:
        src: files/httpd.conf
        dest: /etc/httpd/conf/httpd.conf
        owner: root
        group: root
        mode: 644	
      notify: restart httpd

    - name: start httpd
      service:
        name: httpd
        state: started

  handlers:
    - name: restart httpd
      service:
        name: httpd
        state: restarted

[devops@server1 ansible]$ mkdir files
[devops@server1 ansible]$ cd files
[devops@server1 files]$ scp root@172.25.32.3:/etc/httpd/conf/httpd.conf .
httpd.conf 100% 11KB 11.5KB/s 00:00
[devops@server1 files]$ ls
httpd.conf
在这里插入图片描述
[devops@server1 ansible]$ vi files/httpd.conf
在这里插入图片描述
在这里插入图片描述
查看结果
在这里插入图片描述
还原配置文件后再次运行yml文件
在这里插入图片描述

  • 添加火墙与默认发布文件
---
- hosts: prod
  tasks:
    - name: install httpd
      yum:
        name: httpd
        state: present
        
    - name: copy index.html
      copy:
        src: files/index.html
        dest: /var/www/html/index.html


    - name: configure httpd
      copy:
        src: files/httpd.conf
        dest: /etc/httpd/conf/httpd.conf
        owner: root
        group: root
        mode: 644
      notify: restart httpd

    - name: start httpd and firewalld
      service:
        name: "{{ item }}"
        state: started
      loop:
        - httpd
        - firewalld

    - name: configure firewalld
      firewalld:
        service: http
        permanent: yes
        immediate: yes
        state: enabled

  handlers:
    - name: restart httpd
      service:
        name: httpd
        state: restarted

- hosts: localhost
  become: no
  tasks:
    - name: test http
      uri:
        url: http://172.25.32.3
        status_code: 200



在这里插入图片描述
访问测试:
在这里插入图片描述

  • 给所有成员安装httpd并发布默认文件
---
- hosts: all
  vars:
    http_port: 80
  tasks:
    - name: install httpd
      yum:
        name: httpd
        state: present

    - name: copy index.html
      copy:
        src: files/index.html
        dest: /var/www/html/index.html

    - name: configure httpd
      template:
        src: templates/httpd.conf.j2
        dest: /etc/httpd/conf/httpd.conf
        owner: root
        group: root
        mode: 644
      notify: restart httpd

    - name: start httpd and firewalld
      service:
        name: "{{ item }}"
        state: started
      loop:
        - httpd
        - firewalld

    - name: configure firewalld
      firewalld:
        service: http
        permanent: yes
        immediate: yes
        state: enabled
  handlers:
    - name: restart httpd
      service:
        name: httpd
        state: restarted

- hosts: localhost
  tasks:
    - name: test httpd
      uri:
        url: http://172.25.32.3
        status_code: 200

在这里插入图片描述
[devops@server1 ansible]$ mkdir templates
[devops@server1 ansible]$ cp files/httpd.conf templates/httpd.conf.j2
[devops@server1 ansible]$ vim templates/httpd.conf.j2
在这里插入图片描述
推送
[devops@server1 ansible]$ ansible-playbook playbook.yml

在server2和server3查看端口
[root@server2 .ssh]# vi /etc/httpd/conf/httpd.conf
[root@server3 .ssh]# vi /etc/httpd/conf/httpd.conf
在这里插入图片描述
在这里插入图片描述
[root@server2 .ssh]# netstat -lnput | grep httpd
[root@server3 .ssh]# netstat -lnput | grep httpd

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

[devops@server1 ansible]$ cat file.yml
- hosts: all
  tasks:
    - name: create infofile
      template:
        src: templates/info.j2
        dest: /mnt/hostinfo

[devops@server1 ansible]$ cat templates/info.j2 
主机名: {{ ansible_facts['hostname'] }}
boot分区: {{ ansible_facts['devices']['sda']['partitions']['sda1']['size'] }}
内核: {{ ansible_facts['kernel'] }}
内存空闲: {{ ansible_facts['memfree_mb'] }}

编译运行
在这里插入图片描述
在server2上查看
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值