ansible獲取機器ip切割

ansible下字符串split的使用

由于工作需要,需要通過ansible對一批服務(wù)器的ip取最后一位,發(fā)現(xiàn)了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

執(zhí)行命令

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

-k 輸入用戶ssh密碼
-K 輸入sudo密碼

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容