分享一下ansible管理windows

环境(ansible入门级)

我们用ansible去控制两台windows server,主要是完成文件复制,重命名(添加时间),启动tomcat等,一路走来遇到很多坑。。。。

我们是使用nt账号,/etc/ansible/hosts
[windows_51]
10.148.200.51
[windows_51:vars]
ansible_user=xxxxx
ansible_password=xxxxx
ansible_port=5985
ansible_connection=winrm
ansible_winrm_server_cert_validation=ignore
ansible_winrm_scheme=‘http’
ansible_winrm_transport=ntlm

注意:我们先要去设置windows
1.首先,执行powershell,查看version>4.0,我的是win10,自带版本满足条件
2.然后,查看策略 get-executionpolicy,修改为remote 即set-executionpolicy remotesigned
3.查看状态 winrm quickconfig
4.免密连接,远程认证
winrm set winrm/config/service/auth ‘@{Basic=“true”}’
winrm set winrm/config/service ‘@{AllowUnencrypted=“true”}’
5.查看自己监听的哪个端口
netstat -an

此处影响上面的连接,5985就是http,5986就是https
---------------割--------------割------------------------
注意事项
我的redhat自带python2.6,我又安装了python2.7.16,为了让我的python和ansible版本一致,我是用python安装了pip(没记错的话,还要安装setuptools36.7.0,注意版本)然后再用pip install ansible (前提是要用yum安装环境),

安装报错,没有公钥:
sudo yum install telnet –y --nogpgcheck
plp安装更换源:
pip install telnet -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

----------------代码------------------
启动linux的tomcat很好找,启动windows的tomcat很少有demo。

// 哈哈,一口气创建roles
mkdir -p /etc/ansible/roles/{say,copy,tomcat_start}/{defaults,files,handlers,meta,tasks,templates,vars}
//下面就是tomcat。yml
---
- hosts: windows_51
  remote_user: root
#  gather_facts: False
  roles:
    - tomcat_start
//调用角色中的tomcat_start,展示main.yml
- name: 1.set service startup mode to auto and ensure it is started
  win_service:
    name: "Apache Tomcat 8.5 Tomcat8"
    start_mode: auto
    state: started

- name: 2.sleep
  pause:
   seconds: 10

- name: 3.show ansibel_date_time information
  debug:
    var: ansible_date_time

- name: show current_time
  debug:
    msg: "{{ansible_date_time.iso8601}}"

- name: 4.rename filename
  win_command: "cmd.exe /c rename {{ destination_folder }}\\{{ source_name }} {{ target_name }}{{ ansible_date_time.iso8601_basic }} "

- name: sleep
  pause:
    seconds: 10

- name: 5. copy .war to tomcat/webapp
  win_copy:
    src: /home/deltaman/target/MyTest2.war
    dest: E:/software/apache-tomcat-8.5.32/webapps/MyTest.war

- name: 6.startup tomcat
  win_service:
    name: "Apache Tomcat 8.5 Tomcat8"
    start_mode: auto
    state: started


- name: 7.sleep
  pause:
    seconds: 20

- name: 8.shutdown tomcat
  win_service:
    name: "Apache Tomcat 8.5 Tomcat8"
    state: stopped

两个变量source_name和target_name在vars中设置一下就ok
























  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

百世经纶『一页書』

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值