请帮助设置其他主机的变量:
例如:
---
- name: host1 - varaible from vars_prompt
hosts: '{{ host }}'
become: yes
vars_prompt:
- name: "host"
prompt: "Enter host:"
default: 'Aubuntu'
tasks:
- name:
set_fact:
monitip: "{{ansible_host}}"
- name: host2 - static host
hosts: 'host2'
tasks:
- name: Добавляем в мониторинг
shell: echo {{monitip}}
如何将{{monitip}}
发送到host2?
我需要基于vars_prompt {{ host }}
从host1获取ip,并在host2中使用它
fatal: [host2]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'monitip' is undefined
UPD:
感谢您的答复,但是我自己将变量保存到文件中
---
- name: host1 - varaible from vars_prompt
hosts: '{{ host }}'
become: yes
vars_prompt:
- name: "host"
prompt: "Enter host:"
default: 'Aubuntu'
tasks:
- lineinfile : >
dest=/tmp/s_ip.txt
create=yes
line='{{ansible_host}}'
delegate_to: localhost
- name: host2 - static host
hosts: 'host2'
tasks:
- command: cat /tmp/s_ip.txt
register: monitip
delegate_to: localhost
- name: Добавляем в мониторинг
shell: echo {{monitip.stdout}}
答案 0 :(得分:0)
您可以按名称引用其他主机的hostvars
:
- hosts: host2
tasks:
- name: copy over monitip from the other host
set_fact:
monitip: '{{ hostvars[the_first_host].monitip }}'
vars:
the_first_host: '{{ groups.all | difference([inventory_hostname]) | first }}'
当然,如果host2
代表其中的一组是行不通的,但这还是一般的想法
虽然这是您的问题的答案,但您也可能会不必要地跳过很多麻烦,因为您始终可以在同一本书中直接访问该hostvar:
- hosts: host2
vars_prompt:
- # as before
tasks:
- set_fact:
monitip: '{{ hostvars[host].ansible_host }}'
# tada, drama free
答案 1 :(得分:0)
有趣的请求,我想我设法通过其他方法使它起作用。 库存文件,例如:
$ cat inv
[myhosts]
host1.example.com
host2.example.com
然后是类似的剧本:
---
- hosts: myhosts
become: yes
vars_prompt:
- name: "myhost"
prompt: "Enter host:"
default: localhost
tasks:
- name: Use it
debug:
msg: "IP used on {{ ansible_fqdn }} is {{ hostvars[myhost]['ansible_default_ipv4']['address'] }}"
when: ansible_fqdn == myhost