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密碼