|
v3-4_srvgroup.yml:
- ---
- - name: Local Var
- hosts: srvgroup
- gather_facts: yes
- tasks:
- - name: file
- file:
- path: "/etc/ansible/facts.d"
- state: directory
- - name: Copy
- copy:
- src: v3.d/local.fact
- dest: /etc/ansible/facts.d
- - name: var
- debug:
- msg: |
- "{{ ansible_facts['ansible_local'] }}".
复制代码 v3.d/local.fact
[student@workstation ansible]$ vim v3-4_srvgroup.yml
[student@workstation ansible]$ ansible-playbook v3-4_srvgroup.yml
PLAY [Local Var] *************************************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************************
ok: [serverd]
ok: [serverc]
ok: [serverb]
TASK [file] ******************************************************************************************************************************************************
ok: [serverb]
ok: [serverc]
ok: [serverd]
TASK [Copy] ******************************************************************************************************************************************************
ok: [serverd]
ok: [serverb]
ok: [serverc]
TASK [var] *******************************************************************************************************************************************************
ok: [serverb] => {
"msg": "\"{'local': {'a': {'v1': '1', 'v2': '2'}}}\".\n"
}
ok: [serverc] => {
"msg": "\"{'local': {'a': {'v1': '1', 'v2': '2'}}}\".\n"
}
ok: [serverd] => {
"msg": "\"{'local': {'a': {'v1': '1', 'v2': '2'}}}\".\n"
}
PLAY RECAP *******************************************************************************************************************************************************
serverb : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
serverc : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
serverd : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[student@workstation ansible]$
------------------------------------------------------------------------------------
- ---
- - name: Local Var
- hosts: srvgroup
- gather_facts: yes
- tasks:
- - name: file
- file:
- path: "/etc/ansible/facts.d"
- state: directory
- - name: Copy
- copy:
- src: v3.d/local.fact
- dest: /etc/ansible/facts.d
- - name: var
- debug:
- msg: |
- "{{ ansible_local.local.a.v1 }}".
复制代码
--------------------------------------------------------------动态清单
inventory.py 要有可执行位:
- #!/usr/bin/env python3
- #coding = utf8
- import json
- import sys
- def group():
- hosts = ['serverb','serverc','serverd']
- group = 'srvgroup'
- vars = {'ansible_ssh_port': 22, 'ansible_connection': 'ssh'}
- hostdata = {group:{"hosts": hosts, "vars": vars}}
- print(json.dumps(hostdata, indent=4))
- if len(sys.argv) == 2 and (sys.argv[1] == '--list'):
- group()
- else:
- print("Usage: %s --list or --host <hostname>" % sys.argv[0])
- sys.exit(1)
复制代码 ----------------------
[student@workstation ansible]$ ./inventory.py --list
{
"srvgroup": {
"hosts": [
"serverb",
"serverc",
"serverd"
],
"vars": {
"ansible_ssh_port": 22,
"ansible_connection": "ssh"
}
}
}
----------------------------
- [student@workstation ansible]$ ansible-playbook -i inventory.py v3-4_srvgroup.yml
复制代码
|
|