环境(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
























Logo

助力广东及东莞地区开发者,代码托管、在线学习与竞赛、技术交流与分享、资源共享、职业发展,成为松山湖开发者首选的工作与学习平台

更多推荐