ansible获取机器ip切割

ansible下字符串split的使用

由于工作需要,需要通过ansible对一批服务器的ip取最后一位,发现了ansible支持对字符串进行split切割操作

task.yaml

- hosts: all
  become_user: root
  become: yes
  gather_facts: no

  tasks:
   - name: print ip
     debug:
         var: {{inventory_hostname.split('.')[3]}}

hosts.ini

[all]
192.168.0.1 hostname="master" ansible_ssh_port=22 ansible_ssh_user=ubuntu
192.168.0.2 hostname="node1" ansible_ssh_port=22 ansible_ssh_user=ubuntu
192.168.0.3 hostname="node2" ansible_ssh_port=22 ansible_ssh_user=ubuntu

ansible.cfg

[defaults]
host_key_checking = False
any_errors_fatal = True
stdout_callback = debug
timeout = 30
forks = 50

[ssh_connection]
ssh_args=-F ansible_ssh_config
retries=10
pipelining = true 

ansible_ssh_config

Host *
ForwardAgent no
ControlMaster=auto
ControlPersist=1800s

执行命令

ansible-playbook -i hosts.ini task.yaml -k -K

-k 输入用户ssh密码
-K 输入sudo密码

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容