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密码
网友评论