ansible简单使用安装httpd

ansible安装及配置使用

环境: centos7.5
yum安装,ansible版本:2.7.5
master_ip: 172.28.128.3
node01_ip: 172.28.128.5
node02_ip: 172.28.128.4

配置无密钥访问
  • 生成公钥
    ssh-keygen -t rsa
  • 公钥下发
    ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.28.128.5
    或拷贝公钥至目标服务器
    cat id_rsa.pub >> /root/.ssh/authorized_keys
添加ansible访问

修改/etc/ansible/hosts文件

#添加
[node]
172.28.128.4
172.28.128.5
测试访问
# ansible node -m ping
172.28.128.5 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
172.28.128.4 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

编写httpd playbook

- hosts: node
   remote_user: root
   tasks:
        # 在node节点安装httpd服务
    - name: install httpd packages
      yum: name=httpd state=latest
        # 修改配置文件
    - name: copy httpd.conf to httpd
      copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf
        # 加上tags 使用--tags=conf即可
      tags: conf
        # 加上notify,当修改时触发handlers,
       notify:
        - restart httpd
    # 启动服务
    - name: start httpd service
      service: enabled=true name=httpd state=started
    # 添加handlers
  handlers:
    # 名称需要与notify的名称一致
    - name: restart httpd
      service: name=httpd state=restarted
      

使用命令 ansible-playbook httpd.yaml 即可运行
运行结果

PLAY [all] *********************************************************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************************************
ok: [172.28.128.5]
ok: [172.28.128.4]

TASK [install httpd packages] **************************************************************************************************************************************
changed: [172.28.128.5]
changed: [172.28.128.4]

TASK [copy httpd.conf to httpd] ************************************************************************************************************************************
changed: [172.28.128.5]
changed: [172.28.128.4]

TASK [start httpd service] *****************************************************************************************************************************************
changed: [172.28.128.5]
changed: [172.28.128.4]

RUNNING HANDLER [restart httpd] ************************************************************************************************************************************
changed: [172.28.128.4]
changed: [172.28.128.5]

PLAY RECAP *********************************************************************************************************************************************************
172.28.128.4               : ok=5    changed=4    unreachable=0    failed=0   
172.28.128.5               : ok=5    changed=4    unreachable=0    failed=0  

分别查看两台服务器,发现httpd服务已启动。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值