variable变量:在/etc/ansible/hosts添加以下行:
[dbsrvs]
101.xxx.xxx.102 ansible_ssh_user=root http_port=8888
101.xxx.xxx.101ansible_ssh_user=root http_port=9999
编写一个yaml文件:
- hosts: dbsrvs
remote_user: root
# 定义playbook变量
vars:
- pbvars: playbook variables
tasks:
# 此处是命令行变量
- name: command line variables
copy: content={{ cmdvar }} dest=/tmp/cmd.var
# 此处是playbook变量
- name: playbook variables
copy: content={{ pbvars }} dest=/tmp/pb.var
# 此处是host inventory变量
- name: host inventory variables
copy: content={{ http_port }} dest=/tmp/hi.var
执行:ansible-playbook -e cmdvar=command_line_variable_testing vars.yaml
执行结果,在两台受控主机上写入了传入命令行参数、host inventory参数以及playbook传递的变量: