与Ansible中的“自我”等效

时间:2018-07-12 15:51:23

标签: ansible

我有一个用于在Ansible中进行多路复用的主机文件

[masters]
master ansible_host=10.10.1.1 ansible_ssh_user=root ansible_ssh_pass=froyo 

[workers]
worker1 ansible_host=10.10.1.2 ansible_ssh_user=root ansible_ssh_pass=froyo
worker2 ansible_host=10.10.1.3 ansible_ssh_user=root ansible_ssh_pass=froyo

并且我想执行一个shell命令来设置节点名称

- hosts: workers
  become: yes
  tasks:
    - name: join cluster
      shell: "{{ hostvars['master'].join_command }} --node-name {{self.name}} >> node_joined.txt"
      args:
        chdir: $HOME
        creates: node_joined.txt

How to get the host name of the current machine as defined in the Ansible hosts file?中的Q / A标识为包含inventory_hostname,但这对我不起作用..我可以更改工作程序的主机名,这是我要避免的。

从上述戏剧中,我希望通过worker1文件中存在的名称worker2hosts在主节点中标识两个节点。

我无法设置任何内容:inventory_hostname或其他ansible_hostnameansible_nodename。有什么方法可以从主机访问名称worker1worker2?似乎无法设置ansible -i hosts worker1 -m setup中的变量...,请提前谢谢我帮忙找出我所缺少的内容

1 个答案:

答案 0 :(得分:-2)

您需要将剧本限制为worker1

尝试:

ansible-playbook -i hosts --limit worker1 -m setup