【问题标题】:How to use Command module in Ansible while domain joining the linux servers?域加入 linux 服务器时如何在 Ansible 中使用命令模块?
【发布时间】:2020-04-13 05:42:29
【问题描述】:

我们正在尝试将领域用于我们的 linux 服务器的域集成

我正在尝试使用命令模块在 playbook 中使用以下代码,但出现错误:

- name: install pexpect using pip
  shell: /bin/bash -c "pip install pexpect"

- name: Join system to AD 
  expect:
    command: "{{ item }}"
    loop:
      - source /etc/os-release
      - realm join --membership-software=adcli --user=username@EXAMPLE.COM --computer-ou="OU={{ env }},OU={{ account }},OU=XXXX,OU=XXXXXX,DC=XXXXXXX,DC=XXXXXXX" --os-name="$PRETTY_NAME" --os-version="$VERSION" <DCNAME>
    responses:
        Password for *: "{{ username | b64decode }}"

错误:

TASK [adjoin : Join system to AD] ***********************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'item' is undefined\n\nThe error appears to be in '/tmp/ansiblepull/playbooks/roles/adjoin/tasks/main.yml': line 58, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Join system to AD\n  ^ here\n"}

谁能帮我解决这个问题。

【问题讨论】:

  • 1) 循环在您的任务中缩进错误:它是一个任务选项,而不是expect 模块选项。 2)与您当前的问题无关,但良好的做法:不要用shell安装python模块,使用pip module
  • 谢谢@Zeitounator 你能给出命令示例来使用循环与期望模块。
  • docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html。 TLDR;将循环放在任务的末尾,缩进级别与expectname 相同。
  • 我没有看到任何关于在期望模块中使用多个命令的示例以及我们如何在命令和期望模块中使用循环?
  • 一种简单的方法是将这些命令添加到脚本中,将脚本推送到主机并在期望模块命令中调用该脚本。如果您想做一些更花哨的事情,请阅读expect documentation,注意注释,移至shell module 文档并在那里查找期望示例。

标签: linux ansible realm


【解决方案1】:

首先感谢大家的cmets。

我已经通过使用 ansible_facts 解决了它:

- name: install pexpect using pip
  pip:
    name: pexpect

- name: Join system to AD 
  expect:
    command: /bin/bash -c "realm join --membership-software=adcli --user=username@DOMAINNAME --computer-ou="OU={{ env }},OU={{ account }},OU=xxx,OU=xxxx,DC=xxxxx,DC=xxxx" --os-name={{ ansible_distribution }} --os-version={{ ansible_distribution_version }} DCNAME"
  responses:
        Password for *: "{{ join_user_pass | b64decode }}"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-29
    • 1970-01-01
    • 1970-01-01
    • 2019-08-20
    • 2017-08-21
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    相关资源
    最近更新 更多