Ansible 可从主机读取ip地址
我需要将日期设置为远程主机,因此我读取localhost date,然后需要获取ansible的hosts文件中定义的其他主机“ipv4\U地址”Ansible 可从主机读取ip地址,ansible,Ansible,我需要将日期设置为远程主机,因此我读取localhost date,然后需要获取ansible的hosts文件中定义的其他主机“ipv4\U地址” - hosts: localhost become_user : root tasks: - name: align datetime shell: | data="$(date +'%Y/%m/%d +%H:%m:00')" ssh user@{{
- hosts: localhost
become_user : root
tasks:
- name: align datetime
shell: |
data="$(date +'%Y/%m/%d +%H:%m:00')"
ssh user@{{ otherhost.ipv4_address }} "sudo date -s $data"
- hosts: otherhost
become: true
his tasks....
但这似乎不是获得ipv4的正确方法:
致命:[127.0.0.1]:失败!=>{“msg”:“该任务包含一个选项
使用未定义的变量。错误为:“otherhost.ipv4\u地址”为
未定义\n\n错误似乎在
问题是,
{{otherhost.ipv4\u address}}
在您使用它时不可用,因为您没有收集到有关otherhost
的事实
您可以选择以下几个选项:
ssh
ansible --version
ansible 2.9.2
config file = None
configured module search path = ['/home/tec1/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /home/tec1/.local/share/virtualenvs/sniperx-EdPGXWMw/lib/python3.7/site-packages/ansible
executable location = /home/tec1/.local/share/virtualenvs/sniperx-EdPGXWMw/bin/ansible
python version = 3.7.3 (default, Apr 3 2019, 05:39:12) [GCC 8.3.0]
---
- hosts: all
gather_facts: true
# here you gather facts about otherhost
- hosts: localhost
tasks:
- debug: var=hostvars["otherhost"].ansible_all_ipv4_addresses
# and here you can access those facts about
- hosts: otherhost
tasks:
- name: get localhost time
shell: date +'%Y/%m/%d +%H:%m:00'
become: false
register: local_date
- name: set the date on the server
shell: sudo date -s '{{ local_date.stdout_lines[0] }}'