【问题标题】:Get Ansible on windows to print version在 Windows 上获取 Ansible 以打印版本
【发布时间】:2020-01-07 14:57:55
【问题描述】:

我正在尝试获取一个 Ansible 任务来打印在 Windows 10 上运行时使用的版本。

我目前正在尝试这样的事情:

---

# Source: https://serverfault.com/a/695798
- name: Get version
  win_shell: ansible --version
  register: ansibleVersion

# How I chose to expose the version collected
- name: Display version
  win_msg:
    msg: "Ansible Version: {{ ansibleVersion.stdout }}"
    display_seconds: 30

但是,我得到了这个输出:

"stderr": "ansible : The term 'ansible' is not recognized as the name of a cmdlet, function, script file, or operable program. \r\nCheck the spelling of the name, or if a path was included, verify that the path is correct and try again.\r\n

完全披露,我是 Ansible 的新手。我已经尝试过win_commandwin_shell,但我不确定接下来要尝试什么。

【问题讨论】:

  • 我可以看到在执行 ansible 脚本的主机上无法识别/找到 ansible。如果 ansible 可执行文件存在于某个目录中,您可以将 chdir 参数与 win_shell 模块一起使用。
  • ansible .exe 脚本正常定位在哪里?
  • 据我所知,Windows 没有 Ansible .exe。您是否在 WSL 上安装了 Ansible(如此处所述:docs.ansible.com/ansible/2.5/user_guide/…)?
  • Ansible 正在你的控制器上运行,你的节点上没有对应的,只有 python 运行的脚本,这就是为什么Ansible is said to be agentless
  • @IntrastellarExplorer 你试过这个解决方案了吗,它对你有用吗?

标签: windows ansible windows-10


【解决方案1】:

感谢所有回答和评论的人。这些文章内容丰富,我学到了更多关于 Ansible 的知识。这些答案让我意识到我所做的实际任务。

为了重申我对原始问题的评论,我有一个误解。因为在我的 Windows 机器上我必须添加一个用户 ansible,我认为它以某种方式在本地运行。然而,事实证明,Ansible 部署是在 Linux VM 上运行的。

一旦我消除了这个误解,我意识到我需要在我的 Ansible 任务中使用 delegate_to: 127.0.0.1。这是我的Check Ansible version 任务:

---

# SEE: https://serverfault.com/a/695798/514234
- name: Check Ansible version
  command: ansible --version
  register: ansibleVersion
  delegate_to: 127.0.0.1

- name: Print version
  debug:
    msg: "Ansible Version: {{ ansibleVersion.stdout }}"

【讨论】:

    【解决方案2】:

    Windows 机器可以使用 ansible 进行配置,但不能安装在 Windows 上。

    您可以从 Linux 机器配置 Windows 机器作为控制器主机。 你可以从这个控制器主机上运行 ansible-playbook,它将在 windows 机器上运行。

    ---
    - hosts: all
      tasks:
        - name: Get Windows version
          win_shell: "systeminfo /fo csv | ConvertFrom-Csv | select OS*, System*, Hotfix* | Format-List"
          register: windows_version
    
        - name: Print Windows host information
          debug:
            msg: "{{ windows_version }}"
    

    将此保存为 main.yml

    hosts文件中添加Windows主机IP

    [win]
    172.16.*.*
    
    [win:vars]
    ansible_user=user
    ansible_password=password
    ansible_connection=winrm
    ansible_winrm_server_cert_validation=ignore
    

    使用以下命令运行 playbook

    ansible-playbook -i hosts main.yml
    

    如果你想在 Windows 上运行 ansible,那么还有其他安装方法可以在 Windows 上运行它。

    在 cmets 中也提到过。 我附上了一些在 Windows 10 Linux 子系统上设置 ansible 的链接,

    Ansible - Windows Frequently asked questions

    Using Ansible through Windows 10's Subsystem for Linux

    希望它能解决你的问题。

    【讨论】:

      猜你喜欢
      • 2016-06-09
      • 2011-12-29
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 2020-12-19
      • 2021-06-07
      • 2011-11-02
      相关资源
      最近更新 更多