Defining variables in host inventory hosts
- The http_port defined by the 101 host here is 85, and the one directly defined by 102 is 86. Although the variable names are the same, different values can be set for different hosts
- then we can call with the command line
[root@ansible~]#ansible webser -m hostname -a'name=web{{http_port}}'//Modify the hostname, the variable value will be different for different hosts [root@ansible~]#ansible webser- a'hostname' 192.168.27.101 | success | rc=0 >>web85 This is setting a different value for a certain host 192.168.27.102 | success | rc=0 >>web86
Multiple variables can also be defined and used in the playbook
[root@ansible ansible]#vim var4.yml---- Moderator: webser remote_user:root Task: - name: set the hostname hostname:name={{hostname}}-{{http_port}}[root@ansible ansible]# ansible webser -a 'hostname' 192.168.27.101 | success | rc=0 >>web1-88192.168.27.102 | success | rc = 0 >>web2-86
If we artificially specify the variable value with -e, the parameter defined by -e will prevail, and the variable in the host will not be called
[root @ ansible ansible]#ansible-playbook -e“http_port = 9090 hostname = abc.com”var4.yml[root @ ansible ansible]#ansible webser -a'主机名'192.168.27.101 | 成功| rc = 0 >>abc.com-9090192.168.27.102 | 成功| rc = 0 >>abc.com-9090