运行剧本,将host作为arg传递,但是host在/ ansible / hosts中不作为条目出现

时间:2018-08-08 11:16:39

标签: ansible

为什么在运行剧本时将主机名作为参数传递,并且/ ansible / host文件中没有该主机名?

我确实尝试过

ansible-playbook -i主机名playook.yml

2 个答案:

答案 0 :(得分:0)

您可以在某处动态定义变量主机名。

export hostname=myHostAddress

然后,使用定义的$ hostname创建文件“ hosts”并返回其名称。然后,您只为$ hostname限制playbook.yml的调用。

ansible-playbook -i $(echo "$hostname" > hosts %% echo "hosts -l $hostname") playook.yml

答案 1 :(得分:0)

确保您的主机是剧本中的全部主机或变量:

---
- name: Test
  hosts: all
  gather_facts: True

  tasks:
    - name: Debug
      debug:
        var: groups

然后运行为:

ansible-playbook --inventory=windows, debug.yml

您将获得:

PLAY [Test] **************************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************************
ok: [windows]

TASK [Debug] *************************************************************************************************************************************************************
ok: [windows] => {
    "groups": {
        "all": [
            "windows"
        ],
        "ungrouped": [
            "windows"
        ]
    }
}

PLAY RECAP ***************************************************************************************************************************************************************
windows                    : ok=2    changed=0    unreachable=0    failed=0 

其他选项正在使用变量,并在运行时将其传递:

所以:

---
- name: Test
  hosts: "{{ host }}"
  gather_facts: True

  tasks:
    - name: Debug
      debug:
        var: groups

然后做

ansible-playbook  debug.yml -e host=windows 

除此之外,您还有add_hostgroup_by选项

相关问题