使用Ansible来配置Windows Server 2012

关于Ansible对Windows的支持(摘自文档)

这是我在2015/08/25写的,但做了部分更新。

    • Windowsホスト情報をゲット

 

    • MSIをインストール、アンインストール

 

    • Windowの機能を有効化・無効化

 

    • Windowsのサービスの管理(起動・停止)

 

    • ローカルユーザ・グループの作成・管理

 

    • ChocolateyパッケージマネージャによるWindowsパッケージの管理

 

    • Windowsアップデートの管理・インストール

 

    • リモートサイトからファイルを取得

 

    • PowerShellでスクリプトを書いても実行可

 

    runas というWindowsのコマンドをサポートできるようになる

在Linux / Unix中使用sudo或su等类似的命令(以下为参考)
在Windows中使用runas命令,临时以其他用户权限运行程序。

    PowerShell で Moduleを書くことができる

Ansible用户编写了许多模块,用于管理文件系统的ACL、Windows防火墙、主机名和域成员关联等。

    Ansible Towerも対応

环境

    • OpenStack Kilo 上インスタンス

 

    • コントロールマシン

Ubuntu 14.04 x86_64

リモートマシン

Windows Server 2012 R2 Standard

2015-08-26 10-33-15.jpg

控制机器准备好。

安装Ansible

$ sudo apt-get install software-properties-common
$ sudo apt-add-repository ppa:ansible/ansible
$ sudo apt-get update
$ sudo apt-get install ansible

For Windows remote host
$ sudo apt-get install python-pip
$ sudo pip install https://github.com/diyan/pywinrm/archive/master.zip#egg=pywinrm
$ sudo apt-get install libkrb5-dev python-dev
$ sudo pip install kerberos

库存文件(〜 1.9.x)

$ more hosts
[windows]
10.1.1.6
test.example.com

[windows:vars]
ansible_ssh_user=Administrator
ansible_ssh_pass=XXXXXXXXXXXX
ansible_ssh_port=5986
ansible_connection=winrm

库存文件(2.x及以上版本)

$ more hosts
[windows]
10.1.1.6
test.example.com

[windows:vars]
ansible_user=Administrator
ansible_password=XXXXXXXXXXXX
ansible_port=5986
ansible_connection=winrm
# The following is necessary for Python 2.7.9+ when using default WinRM self-signed certificates:
ansible_winrm_server_cert_validation: ignore

注意在密码指定的参数上!(ansible_ssh_pass => ansible_password)

ansible_password を ansible_pass など他の文字列にすると、下記のようなエラーがでます。

192.168.1.6 | UNREACHABLE! => {
    "changed": false,
    "msg": "ssl: auth method ssl requires a password",
    "unreachable": true
}

准备好了Windows Server 2012。

    • 特別手を加えていない

 

    PowerShell 3.0以上が必要だが、既に4.0が含まれている
2015-08-24 17-45-21.jpg

模块实验

展示在上述环境中经过确认的Playbook示例。

模块(稳定的Ansible v1.9.2版本)

复制

    • ファイルのコピー(ディレクトリも可能だが、それのみでないとだめ)

 

    • 1MBを越えると数分かかる

 

    • 3MB以上だと500エラーでる

 

    大きいファイルには向かないので、win_get_urlモジュールか、ファイルサーバを利用する
    - name: Copy a file
      win_copy: src=hogefile.zip dest={{ mydir }}

从2.0.0版本开始,进行了改进,现在可以传输大型文件。

胜利特点

一个安装了Telnet客户端的例子。

(Translation: An example of installing a Telnet client.)

    - name: Install Telnet Client
      win_feature:
        name: "Telnet-Client"
        state: absent
        restart: no
        include_sub_features: no
        include_management_tools: no

胜利的文件

    - name: Create directory structure
      win_file: path={{ mydir }}\testfolder state=directory

获胜_获取网址

在互联网上下载文件的示例

    - name: Get a file from web site
      win_get_url:
        url: 'http://sarah.secret.jp/worklog/wp-content/uploads/2015/03/pen8-300x209.jpg'
        dest: '{{ mydir }}\test.jpg'
    • .Net Frameworkの System.Net.WebClient クラスで実装されている

 

    • HTTP Proxy に注意(認証プロキシ未対応)

PRしてみた Add HTTP Proxy options for win_get_url module #1979

マージされたので2.0から利用可能

获胜团队

    - name: Create group
      win_group:
        name: hogefuga
        description: Deploy Group
        state: present

获取用户的胜利

    - name: Add a user
      win_user:
        name: hogefuga
        password: "eisaku@00"
        groups: ["Users"]
        state: present

胜利_微星

安装Vagrant的示例

    - name: MSI Install
      win_msi: path={{ mydir }}\vagrant_1.7.2.msi state=present

胜利的平凡之旅

    - name: Test ping
      action: win_ping

胜利服务

    - name: Restart a service
      win_service:
        name: snmptrap
        state: started

胜利统计

在下面的例子中,获取“win.ini”的信息,并使用assert进行检查。

    - name: Returns information about a Windows file
      tags: stat_file
      win_stat: path={{ mydir }}\win.ini
      register: stat_file

    - debug: var=stat_file

    - name: Check stat_file result
      tags: check_stat
      assert:
        that:
          - "stat_file.stat.exists"
          - "not stat_file.stat.isdir"
          - "stat_file.stat.size > 0"
          - "stat_file.stat.md5"

胜利模板
胜利模板是一种可在比赛或竞争中获胜的经验总结和指导方针。

  vars:
    mydir: C:\Users\Administrator\Desktop
    ntp_srv_addr: 100.100.100.100
  tasks:
    - name: template
      win_template: src=./ntp.conf.j2 dest={{ mydir }} backup=yes

更新胜利

    - name: Update
      win_updates:
        category: critical # critical or security

没有以”win_”开头的模块。

安装

$ ansible 10.1.1.6 -m setup
10.1.1.6 | success >> {
    "ansible_facts": {
        "ansible_distribution": "Microsoft Windows NT 6.3.9600.0",
        "ansible_distribution_version": "6.3.9600.0",
        "ansible_fqdn": "ansible-target-",
        "ansible_hostname": "ANSIBLE-TARGET-",
        "ansible_interfaces": [
            {
                "default_gateway": "10.1.1.254",
                "dns_domain": "openstacklocal",
                "interface_index": 12,
                "interface_name": "Red Hat VirtIO Ethernet Adapter"
            }
        ],
        "ansible_ip_addresses": [
            "10.1.1.6",
            "fe80::69c3:3631:3bd:845a"
        ],
        "ansible_os_family": "Windows",
        "ansible_powershell_version": 4,
        "ansible_system": "Win32NT",
        "ansible_totalmem": 4294967296,
        "ansible_winrm_certificate_expires": "2025-08-24 06:52:04"
    },

吸溜

$ ansible 10.1.1.6 -m slurp -a src='C:\Users\Administrator\Desktop\win.ini'
10.1.1.6 | success >> {
    "changed": false,
    "content": "OyBmb3IgMTYtYml0IGFwcCBzdXBwb3J0DQo=",
    "encoding": "base64"
}
    - name: slurp module
      slurp:
        src: '{{ mydir }}\win.ini'

取回

从远程主机获取数据

在此示例中,远程主机(Windows Server)上的 ntp.conf.txt 文件将以 “test//C:/Users/Administrator/Desktop/ntp.conf.txt” 的形式被传输到控制机的Playbook执行当前位置。

  vars:
    mydir: C:\Users\Administrator\Desktop

    - name: fetch module
      fetch:
        src: '{{ mydir }}\ntp.conf.txt'
        dest: ./test

原始的

    リモートホスト上の個別のコマンドや.exe、PowerShellスクリプトを実行したい時
    - name: raw module
      tags: raw
      raw: ipconfig
      register: ipconfig
    - debug: var=ipconfig

剧本

    実行プログラムを転送した後、PowerShell のスクリプトを実行したい時
    - name: run test script
      script: files/test_script.ps1

模块(Dev Ansible v2.0)

胜利环境(v2.0)

- name: Set Environment
  tags: env
  win_environment:
    state: present
    name: TestVariable
    value: "Test value"
    level: machine

使用win_regedit(v2.0)

- name: Set Registry
  tags: reg
  win_regedit:
    key: HKCU:\Software\MyCompany
    value: hello
    data: 1337
    datatype: dword
2015-08-26 17-30-18.jpg

解压胜利(v2.0)

    • PowerShell Community Extensions (PSCX) Module が必要

 

    • zip 以外も展開可(bzip,gzなど)

 

    • 再帰的にアーカイブを展開可

 

    • 展開先のディレクトリがなければ作ってから展開

 

    展開後、不要なソースファイルを削除可

以下的例子中,”presen.zip”解压后变成”presen\test.zip”,而且进一步解压test.zip后会变成一个文本文件。换句话说,通常情况下,全部解压后的结果如下所示:

presen
      \__test.txt
      \__test.zip

尝试按照以下方式指定并执行(递归展开)这个Playbook。

- name: Unzip file
  tags: unzip
  win_unzip:
    src: '{{ mydir }}\presen.zip'
    dest: '{{ mydir }}\archive'
    recurse: yes
    rm: true
    creates: '{{ mydir }}\archive'

结果如下:展开文件是递归进行的,并且原始的zip文件已被全部删除,但这个过程不太直观。

archive
       \presen #(この下は空)
       \test.txt

Ansible Docs 中的模块。

以下尚未评估。

    • win_chocolatey (v1.9.2 extras) # Enterpriseとして使うか否か

 

    • win_dotnet_ngen (v2.0)

 

    • win_iis_virtualdirectory (v2.0)

 

    • win_iis_webapplication (v2.0)

 

    • win_iis_webapppool (v2.0)

 

    • win_iis_webbinding (v2.0)

 

    • win_iis_website (v2.0)

 

    • win_package (v2.0)

 

    • win_scheduled_task (v2.0)

 

    win_webpicmd (v2.0)

赠品

设置配置文件

guest@ahost:~$ cp /etc/ansible/ansible.cfg ~/
guest@ahost:~$ sed -i -e 's/\/etc\/ansible\/hosts/\/home\/guest\/hosts/g'
guest@ahost:~$ export ANSIBLE_CONFIG=~/ansible.cfg

巧克力软件支持

    1.9.2 のモジュールでうまくいっていない
# cd /usr/share/pyshared/ansible/modules/extras/windows/
# cp win_chocolatey.py org_win_chocolatey.py
# cp win_chocolatey.ps1 org_win_chocolatey.ps1

# curl https://raw.githubusercontent.com/ansible/ansible-modules-extras/devel/windows/win_chocolatey.ps1 -o ./win_chocolatey.ps1
# curl https://raw.githubusercontent.com/ansible/ansible-modules-extras/devel/windows/win_chocolatey.py -o ./win_chocolatey.py

提醒:您必须拥有一台Linux控制机器。

Windows的管理是由Linux控制机器来完成的,而不是由Windows的控制机器来完成的。
此外,不支持Cygwin。

在考试中,关于 Windows Server 2012 的注意事项。

这不是关于Ansible的意思,而是关于Windows Server 2012的备忘录。

    • セキュリティが高い設定は相変わらずだが至極当然

 

    • ブラウザからデフォルト状態ではファイルダウンロードX

 

    • ファイアウォールも有効

 

    隠しファイルはデフォルト見えない

请找一种中国人最自然的方式改写以下内容:

– 参照

以下链接是有关Ansible在Windows系统上的介绍:http://docs.ansible.com/ansible/intro_windows.html

此链接提供了Ansible在Windows系统上可用的模块列表:http://docs.ansible.com/ansible/list_of_windows_modules.html

广告
将在 10 秒后关闭
bannerAds