我写了一个master.yaml
如下定义的剧本
- hosts: master remote_user: "{{ ansible_user }}" tasks: - name: Get env command: id -g -n {{ lookup('env', '$USER') }} register: group_user vars: is_done: "false" - include: slave.yaml vars: sethostname: "{{ group_user }}" worker: worker when: is_dOne== "true" where: inventory_hostname in groups['worker']
slave.yaml
在满足某些条件后,我正在尝试运行另一本名为以下定义的剧本。
- hosts: worker remote_user: "{{ ansible_user }}" tasks: - name: Write to a file for deamon setup copy: content: "{{ sethostname }}" dest: "/home/ubuntu/test.text"
现在我有两个问题:
我无法设置var的值isDone
。slave.yaml仅在isDone
为true 时才起作用。
2. salve.yaml如何访问价值工作者。我在inventory.yaml中定义了小组工作人员
Romain.. 5
我不知道这是否是实现目标的正确方法。但是,我试图通过尽可能多地保持逻辑来使本手册发挥作用。希望能帮助到你。
关键是您不能import_playbook
在剧本内部使用。查看模块文档以获取更多信息。因此,我建议使用角色而不是剧本来共享代码。您将能够slave
在master
剧本和另一个剧本(slave
例如,一个剧本)之间共享角色。
ansible文件夹结构如下。
??? hosts ??? master.yml ??? roles ??? slave ??? tasks ??? main.yml
--- - name: 'Master Playbook' # Using the serial keyword to run the playbook for each host one by one hosts: master serial: 1 remote_user: "{{ ansible_user }}" tasks: - name: 'Get env' command: id -g -n {{ lookup('env', '$USER') }} register: group_user - name: 'Calling the slave role' import_role: name: 'slave' # The return value of the command is stored in stdout vars: sethostname: "{{ group_user.stdout }}" # Only run when the task get env has been done (state changed) when: group_user.changed # Delegate the call to the worker host(s) -> don't know if it's the expected behavior delegate_to: 'worker'
--- - name: 'Write to a file for deamon setup' copy: content: "{{ sethostname }}" dest: "/tmp/test.text"
最后/tmp/test.text
包含有效的用户组名称。
我不知道这是否是实现目标的正确方法。但是,我试图通过尽可能多地保持逻辑来使本手册发挥作用。希望能帮助到你。
关键是您不能import_playbook
在剧本内部使用。查看模块文档以获取更多信息。因此,我建议使用角色而不是剧本来共享代码。您将能够slave
在master
剧本和另一个剧本(slave
例如,一个剧本)之间共享角色。
ansible文件夹结构如下。
??? hosts ??? master.yml ??? roles ??? slave ??? tasks ??? main.yml
--- - name: 'Master Playbook' # Using the serial keyword to run the playbook for each host one by one hosts: master serial: 1 remote_user: "{{ ansible_user }}" tasks: - name: 'Get env' command: id -g -n {{ lookup('env', '$USER') }} register: group_user - name: 'Calling the slave role' import_role: name: 'slave' # The return value of the command is stored in stdout vars: sethostname: "{{ group_user.stdout }}" # Only run when the task get env has been done (state changed) when: group_user.changed # Delegate the call to the worker host(s) -> don't know if it's the expected behavior delegate_to: 'worker'
--- - name: 'Write to a file for deamon setup' copy: content: "{{ sethostname }}" dest: "/tmp/test.text"
最后/tmp/test.text
包含有效的用户组名称。