接上一章:
1 hostvars[' 主机名 '].键值
[root@pp demo2]# cat 8-hostvars.yaml
---
- hosts: up
tasks:
- name: 打印lp的IP
debug: msg={{hostvars['lp'].ansible_default_ipv4.address}}
[root@pp demo2]#
[root@pp demo2]# ansible-playbook 8-hostvars.yaml
PLAY [up] ********************************************************************************
TASK [Gathering Facts] *******************************************************************
ok: [up]
TASK [打印server3的IP] **********************************************************************
fatal: [up]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: \"hostvars['server3']\" is undefined\n\nThe error appears to be in '/root/demo2/8-hostvars.yaml': line 4, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: 打印server3的IP\n ^ here\n"}
PLAY RECAP *******************************************************************************
up : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
[root@pp demo2]# ansible-playbook 8-hostvars.yaml
PLAY [lp] ********************************************************************************
TASK [Gathering Facts] *******************************************************************
ok: [lp]
PLAY [up] ********************************************************************************
TASK [Gathering Facts] *******************************************************************
ok: [up]
TASK [打印lp的IP] ***************************************************************************
ok: [up] => {
"msg": "192.168.248.11"
}
PLAY RECAP *******************************************************************************
lp : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
up : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@pp demo2]#
[root@pp demo2]# cat 9-inventory1.yaml
---
- hosts: db
tasks:
- name: 打印我在清单文件中的名称
debug: msg={{inventory_hostname}}
[root@pp demo2]#
[root@pp demo2]# ansible-playbook 9-inventory1.yaml
PLAY [db] ********************************************************************************
TASK [Gathering Facts] *******************************************************************
ok: [lp]
ok: [up]
TASK [打印我在清单文件中的名称] **********************************************************************
ok: [up] => {
"msg": "up"
}
ok: [lp] => {
"msg": "lp"
}
PLAY RECAP *******************************************************************************
lp : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
up : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
1 {{ 变量名 | 函数 }}
[root@pp demo2]# cat 10-vars1.yaml
---
- hosts: up
vars:
aa: tom
bb: BOB
tasks:
- name: xxx
debug: msg={{bb | lower}}
[root@pp demo2]#
[root@pp demo2]# ansible-playbook 10-vars1.yaml
PLAY [up] ********************************************************************************
TASK [Gathering Facts] *******************************************************************
ok: [up]
TASK [xxx] *******************************************************************************
ok: [up] => {
"msg": "bob"
}
PLAY RECAP *******************************************************************************
up : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@pp demo2]# cat 10-vars2.yaml
---
- hosts: up
tasks:
- name: 数学计算
debug: msg={{3+'3'}}
[root@pp demo2]# ansible-playbook 10-vars2.yaml
PLAY [up] ********************************************************************************
TASK [Gathering Facts] *******************************************************************
ok: [up]
TASK [数学计算] ******************************************************************************
fatal: [up]: FAILED! => {"msg": "Unexpected templating type error occurred on ({{3+'3'}}): unsupported operand type(s) for +: 'int' and 'str'"}
PLAY RECAP *******************************************************************************
up : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
[root@pp demo2]#
[root@pp demo2]# cat 10-vars2.yaml
---
- hosts: up
tasks:
- name: 数学计算
debug: msg={{3+'3' |int}}
[root@pp demo2]#
[root@pp demo2]# ansible-playbook 10-vars2.yaml
PLAY [up] ********************************************************************************
TASK [Gathering Facts] *******************************************************************
ok: [up]
TASK [数学计算] ******************************************************************************
ok: [up] => {
"msg": "6"
}
PLAY RECAP *******************************************************************************
up : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@pp demo2]# cat 10-vars3.yaml
---
- hosts: up
vars:
list1: [1,2,8,3,2]
tasks:
- name: 求列表长度
debug: msg="{{list1 | length}}"
- name: 求列表中的最小值
debug: msg="{{list1 | min}}"
[root@pp demo2]#
[root@pp demo2]# ansible-playbook 10-vars3.yaml
PLAY [up] ********************************************************************************
TASK [Gathering Facts] *******************************************************************
ok: [up]
TASK [求列表长度] *****************************************************************************
ok: [up] => {
"msg": "5"
}
TASK [求列表中的最小值] **************************************************************************
ok: [up] => {
"msg": "1"
}
PLAY RECAP *******************************************************************************
up : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
1 {{ var1 | default(value1) }}
[root@pp demo2]# cat 10-vars4.yaml
---
- hosts: up
vars:
aa: 11
bb:
tasks:
‐ name: aa的值
debug: msg="{{ aa | default('xxx')}}"
‐ name: bb的值
debug: msg="{{ bb | default('xxx')}}"
‐ name: cc的值
debug: msg="{{ cc | default('xxx')}}"
[root@pp demo2]#
[root@pp demo2]# cat 10-vars5.yaml
---
- hosts: up
tasks:
- name: 求和
debug: msg="{{ 3+(3|string)}}"
[root@pp demo2]#
[root@pp demo2]# ansible-playbook 10-vars5.yaml
PLAY [up] ********************************************************************************
TASK [Gathering Facts] *******************************************************************
ok: [up]
TASK [求和] ********************************************************************************
fatal: [up]: FAILED! => {"msg": "Unexpected templating type error occurred on ({{ 3+(3|string)}}): unsupported operand type(s) for +: 'int' and 'str'"}
PLAY RECAP *******************************************************************************
up : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
[root@pp demo2]# cat 10-vars5.yaml
---
- hosts: up
tasks:
- name: 字符转换
debug: msg="{{( 'aa' | capitalize)}}"
[root@pp demo2]#
[root@pp demo2]# ansible-playbook 10-vars5.yaml
PLAY [up] ********************************************************************************
TASK [Gathering Facts] *******************************************************************
ok: [up]
TASK [字符转换] ******************************************************************************
ok: [up] => {
"msg": "Aa"
}
PLAY RECAP *******************************************************************************
up : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@pp demo2]#