我们正在寻找一个冠军,一个愿意冒险进入Ansible自动化世界的Windows管理员。 不,您将不需要了解Bash脚本或如何在Linux终端上导航。 您需要做的就是全体管理员共同的愿望:尽快完成平凡的任务。

尽管Windows和Ansible的集成取得了长足的进步,但是请注意,当前以及从Ansible版本的最新版本开始,Ansible仍需要Linux来运行和管理远程Windows节点。 但是,您不必担心,因为Windows当前具有称为WSL的Linux子系统或Linux的Windows子系统。 有说明可以帮助您安装WSL

如果您是Windows管理员,对Linux和Ansible零经验,那么开始并运行您的第一本Ansible剧本既不困难也不费时间。 您可以使用Ansible剧本来自动化日常任务,例如部署服务或更改用户帐户,从而对组织的基础架构产生积极影响,提高投资回报(ROI),并有更多时间进行创新。

Ansible可以为混合操作系统环境带来自动化,并提供一种有效的方式来达到基础架构即代码(IaC)状态,而不会给管理员带来负担。 Ansible不能替代System Center Configuration Manager( SCCM )或Chocolatey ; 它是一个补充工具,可让您自动执行软件提供的服务。

开始吧




ansible有windows版本吗 ansible能用在windows上吗_linux


从Ansible控制计算机远程访问Windows服务器或客户端需要正确配置Windows远程管理器(WinRM)。 幸运的是,Ansible团队编写了一个PowerShell脚本ConfigureRemotingForAnsible ,可以轻松在开发或测试环境中开始使用Windows的Ansible 。 该脚本在任何受支持的Windows服务器或客户端目标上配置WinRM。

在Windows PowerShell上运行以下命令:

$url = "https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1"
     
     

$file = "$env:temp\ConfigureRemotingForAnsible.ps1"
     
     

(New-Object -TypeName System.Net.WebClient).DownloadFile($url, $file)
     
     

powershell.exe -ExecutionPolicy ByPass -File $file
     
     

winrm enumerate winrm/config/Listener

Ansible可以使用多种身份验证传输方案,包括NTLM,Kerberos和基本身份验证。 在本教程中,我们将使用Kerberos身份验证方法(假设Windows服务器已注册到域)。

默认情况下, ConfigureRemotingForAnsible脚本已启用基本身份验证; 要禁用基本身份验证并启用Kerberos,请在命令提示符下运行以下命令:

winrm set winrm/config/service/auth @{Basic="false"; Kerberos="true"} 
winrm set winrm/config/service/auth @{Basic="false"; Kerberos="true"}

设置您的控制机器

我们的控制机器正在运行CentOS Linux版本7.5.1804(核心); 您还可以使用RHEL 7或Fedora 19或更高版本。 要安装和配置控制计算机以管理Windows目标主机,请执行以下操作:

  1. 在控制机器上,使用以下命令安装ansiblepython2-winrm软件包:
yum install -y ansible python2-winrm 
yum install -y ansible python2-winrm
yum install -y python-requests-kerberos 
yum install -y python-requests-kerberos
  1. 打开/etc/krb5.conf文件并编辑以下设置(使用您自己的域信息):
[realms]
       
       

   EXAMPLE.COM = {
       
       

   kdc = ad.example.com
       
       

  }
       
       


[domain_realm]
       
       

  .example.com = EXAMPLE.COM

设置您的库存

库存由Ansible主机(也称为Ansible控制器)可以管理的所有末端节点或目标主机组成。 您可以将清单配置为静态或动态;

在本教程中,我们将配置静态清单。

虽然您可以在/ etc / ansible / hosts中配置静态清单,但是最好的做法是创建一个可以根据需要进行编辑的清单文件。 例如,如果您需要将静态清单更改为动态清单以适应基础架构的变化。 清单将通过分组进行配置。 组以方括号( [] )开头,服务器的集合表示该组(例如[group] )。

Ansible清单可以采用多种形式:静态文件格式,例如.ini,.yaml和.toml,以及通过脚本或插件动态生成的清单。 .ini格式适用于小型,简单的清单,可以像要运行的主机名列表一样简单。

Ansible允许您通过在方括号内插入<服务器组名>:vars *的名称来为Ansible主机文件中的每个组设置变量。 下面的代码在Ansible控制机器上为WinRM设置变量。

[linux-server]
     
     

linux-web.example.com
     
     

[linux-server:vars]
     
     

ansible_user=root
     
     

ansible_connection=ssh
     
     

[win-server]
     
     

windows-web
     
     

[windows:vars]
     
     

ansible_user=Administrator@example.com
     
     

ansible_connection=winrm
     
     

ansible_port=5986
     
     

ansible_winrm_transport=kerberos
     
     

ansible_winrm_server_cert_validation=ignore

Ansible可以通过运行ad-hoc命令来检查属于linux-serverwin-server组的所有服务器的ping状态。

ansible linux-server -i (some local path)/(inventory file) -m ping 
ansible linux-server -i (some local path)/(inventory file) -m ping

要么

ansible win-server -m win_ping --ask-pass 
ansible win-server -m win_ping --ask-pass

配置IIS和Apache Web应用程序

创建Ansible剧本

当您需要执行比仅对目标主机执行ping操作或获取主机的正常运行时间信息更复杂的任务时,使用Ansible临时命令可能既繁琐又耗时。 Ansible剧本是YAML格式的文件,包含一组在Ansible Windows或Linux目标主机上达到最终状态的配置和任务。 上面的临时命令可能变为:

- hosts 
     
     :  win-server 
     
     

 gather_facts 
     
     :  
     
     no 
     
     

 tasks : 
     
     

 - name 
     
     :  Checking connection to 
     
     { 
     
     { inventory_hostname 
     
     } 
     
     } 
     
     

   win_ping:

要运行Ansible剧本,请使用ansible -playbook命令。 结果显示所有成功和失败的任务。


ansible有windows版本吗 ansible能用在windows上吗_python_02


要更改Ansible目标主机名,请首先显示当前的Ansible目标主机名:

ansible win-server -m setup --ask-pass | grep ansible_hostname 
ansible win-server -m setup --ask-pass | grep ansible_hostname


ansible有windows版本吗 ansible能用在windows上吗_java_03


在剧本中添加两个任务,以将主机名更改为清单文件中的主机名,然后重新启动Ansible目标主机:

- hosts 
     
     :  win-server 
     
     

 gather_facts 
     
     :  
     
     yes 
     
     

 tasks : 
     
     

 - name 
     
     :  Change current hostname to 
     
     { 
     
     { inventory_hostname 
     
     } 
     
     } 
     
     

   win_hostname : 
     
     

     name 
     
     :  
     
     "{{ inventory_hostname }}" 
     
     

   register 
     
     :  winhostname
     
     


 - name 
     
     :  Reboot 
     
     { 
     
     { inventory_hostname 
     
     } 
     
     } 
     
     

   win_reboot : 
     
     

   when 
     
     :  winhostname.reboot_required

Windows:配置IIS网站

要创建Internet信息服务(IIS)网站:

  • IIS Windows功能必须已安装并正在运行
  • IIS网页文档必须可用于IIS服务
  • Windows防火墙必须配置为允许来自端口80的传入流量

下面的Ansible剧本使用Ansible Windows模块在Ansible目标主机上生成了一个正在运行和配置的IIS Web服务器。 它处理以下配置和任务:

  1. hosts:命名剧本将针对其运行的Ansible目标主机组
  2. collect_facts:查询目标系统设置的列表,例如主机名,IP地址,MAC地址,完全限定的域名(FQDN)等。
  3. 任务:按顺序执行Ansible任务列表:
  1. 检查Ansible控制机器和目标主机之间的连接以确保网络连接
  2. 将目标主机的当前主机名更改为Ansible库存主机名
  3. 在Ansible目标主机上安装IIS功能
  4. 将Web索引文件从Ansible控制机复制到Ansible目标主机上的IIS Web主目录
  5. 打开端口80以便目标主机防火墙上的传入流量
  6. 测试目标主机上的IIS服务器是否正在运行并已正确配置
- hosts 
     
     :  win-server 
     
     

  gather_facts 
     
     :  
     
     yes 
     
     

  tasks : 
     
     

  - name 
     
     :  Install IIS feature on 
     
     { 
     
     { inventory_hostname 
     
     } 
     
     } 
     
     

        win_feature : 
     
     

        name 
     
     :  Web-Server 
     
     

        state 
     
     :  present 
     
     

        restart 
     
     :  
     
     no 
     
     

        include_sub_features 
     
     :  
     
     yes 
     
     

        include_management_tools 
     
     :  
     
     no 
     
     


  - name 
     
     :  Move local web index file to 
     
     { 
     
     { inventory_hostname 
     
     } 
     
     } 
     
     

        win_copy : 
     
     

        src 
     
     :  files/index.html 
     
     

        dest 
     
     :  'C:\\inetpub\\wwwroot\\index.html'
     
     


  - name 
     
     :  Open firewall port 80 for the IIS web server on 
     
     { 
     
     { inventory_hostname 
     
     } 
     
     } 
     
     

        win_firewall_rule : 
     
     

        name 
     
     :  
     
     "{{ inventory_hostname }}_80" 
     
     

        enable 
     
     :  
     
     yes 
     
     

        state 
     
     :  present 
     
     

        localport 
     
     :  80 
     
     

        action 
     
     :  Allow 
     
     

        direction 
     
     :  In 
     
     

        protocol 
     
     :  Tcp
     
     


  - name 
     
     :  Testing IIS is properly configured and running on 
     
     { 
     
     { inventory_hostname 
     
     } 
     
     } 
     
     

        win_uri : 
     
     

        url 
     
     :  
     
     "http://{{ ansible_fqdn }}" 
     
     

        return_content 
     
     :  
     
     yes 
     
     

        register 
     
     :  result 
     
     

        failed_when 
     
     :  
     
     "'Yay! Simple Ansible deployed IIS server ...' not in result.content"

Linux:配置Apache网页

如果要求您成为混合管理员,则可以假定您仅负责处理Windows环境。 但是,随着管理员角色转移到DevOps,您可能会被要求触摸一两个Linux服务器来支持运行Apache的主机。 这里有一些说明可以帮助您。

要创建一个Apache网页:

  1. 必须安装Apache
  1. systemctl启用httpd
  2. systemctl启动httpd
  1. 进行firewalld更改,以确保将HTTP / HTTPS端口和端口一起添加到防火墙规则中:
  1. 命令:firewalld-cmd --permanent -add-service = {http,https}
  2. 命令:firewall-cmd --permanent -add-port = {80,443}
  1. 使用yum模块安装httpd服务
  2. 使用复制模块创建Web内容
  3. 确保防火墙已启用并正在运行
  4. 确保防火墙允许使用httpd服务
  5. 确保httpd服务已启用并正在运行
  6. 测试连接网页配置

这个Ansible剧本将部署Apache Web服务器:

- hosts 
     
     :  webserver 
     
     

  become 
     
     :  true 
     
     

    gather_facts 
     
     :  
     
     no 
     
     

  tasks : 
     
     

-       name 
     
     :  install latest version of Apache 
     
     

yum : 
     
     

  name 
     
     :  
     
     "(( item }}" 
     
     

  state 
     
     :  latest 
     
     

      with_items 
     
     :
 -       httpd
     
     

-       httpd-tools
     
     

  
     
     

  -  name 
     
     :  create web content file 
     
     

     copy : 
     
     

       content 
     
     :  
     
     "Yay!  Simply deployed Apache webserver …" 
     
     

       dest 
     
     :  /var/www/html/index.html
     
     

  
     
     

  - name 
     
     :  firewall enabled and running 
     
     

    service 
     
     :  
     
     

      enabled 
     
     :  true 
     
     

      name 
     
     :  firewalld 
     
     

      state 
     
     :  started
     
     

  
     
     

  - name 
     
     :  firewalld persists httpd service 
     
     

    firewalld : 
     
     

      immediate 
     
     :  
     
     yes 
     
     

      permanent 
     
     :  true 
     
     

      service 
     
     :  http 
     
     

      state 
     
     :  enabled
     
     


  - name 
     
     :  httpd enabled and running 
     
     

    service 
     
     :  
     
     

      name 
     
     :  httpd 
     
     

      state 
     
     :  started 
     
     

      enabled 
     
     :  true

参加聚会

已经有大约90个模块可用,并且还在开发中。 Windows模块有助于Chocolatey,现在大多数Windows基础结构都可以使用Ansible进行管理。 这使Windows管理员可以在悠久的Linux文化中使用Linux管理员执行的相同技术和做法。

Windows领域正在不断发展,通过加入Ansible社区,您可以加入最大的自动化聚会!

翻译自: https://opensource.com/article/19/2/ansible-windows-admin