I. Tản mạn
Ansible hỗ trợ lấy biến của host, cách lấy như sau:
với ad-hoc:
sudo ansible -i hosts 104.207.151.59 -m setup -u root |
file hosts đã có sẵn nha quí dị =)) chạy không được rồi lại chửi tui đi, tạo file hosts và thêm vào line đầu tiên IP là được.
Còn nếu chưa có file hosts hoặc muốn chạy mà không cần file hosts thì mình làm như sau
ansible all -i “104.207.151.59,” -m setup -u root -k |
chú ý có dấu “,” sau IP nha. Mình search google mãi mới ra cách này đó 😀
104.207.151.59 | SUCCESS => { "ansible_facts": { "ansible_all_ipv4_addresses": [ "104.207.151.59" ], "ansible_all_ipv6_addresses": [], "ansible_apparmor": { "status": "disabled" }, "ansible_architecture": "x86_64", "ansible_bios_date": "04/01/2014", "ansible_bios_version": "rel-1.12.1-0-ga5cab58e9a3f-prebuilt.qemu.org", "ansible_cmdline": { "BOOT_IMAGE": "/boot/vmlinuz-3.10.0-957.12.1.el7.x86_64", "LANG": "en_US.UTF-8", "consoleblank": "0", "crashkernel": "auto", "quiet": true, "rhgb": true, "ro": true, "root": "UUID=e9842005-de6f-45ec-b782-a60f850f532d" }, "ansible_date_time": { "date": "2020-05-23", "day": "23", "epoch": "1590208561", "hour": "04", "iso8601": "2020-05-23T04:36:01Z", "iso8601_basic": "20200523T043601531323", "iso8601_basic_short": "20200523T043601", "iso8601_micro": "2020-05-23T04:36:01.531422Z", "minute": "36", "month": "05", "second": "01", "time": "04:36:01", "tz": "UTC", "tz_offset": "+0000", "weekday": "Thứ bảy", "weekday_number": "6", "weeknumber": "20", "year": "2020" }, "ansible_default_ipv4": { "address": "104.207.151.59", "alias": "eth0", "broadcast": "104.207.151.255", "gateway": "104.207.150.1", "interface": "eth0", "macaddress": "56:00:02:05:95:22", "mtu": 1500, "netmask": "255.255.254.0", "network": "104.207.150.0", "type": "ether" }, "ansible_default_ipv6": {}, "ansible_device_links": { "ids": { "sr0": [ "ata-QEMU_DVD-ROM_QM00003" ] }, "labels": {}, "masters": {}, "uuids": { "vda1": [ "e9842005-de6f-45ec-b782-a60f850f532d" ] } }, "ansible_devices": { "sr0": { "holders": [], "host": "", "links": { "ids": [ "ata-QEMU_DVD-ROM_QM00003" ], "labels": [], "masters": [], "uuids": [] }, "model": "QEMU DVD-ROM", "partitions": {}, "removable": "1", "rotational": "1", "sas_address": null, "sas_device_handle": null, "scheduler_mode": "deadline", "sectors": "2097151", "sectorsize": "512", "size": "1024.00 MB", "support_discard": "0", "vendor": "QEMU", "virtual": 1 }, "vda": { "holders": [], "host": "", "links": { "ids": [], "labels": [], "masters": [], "uuids": [] }, "model": null, "partitions": { "vda1": { "holders": [], "links": { "ids": [], "labels": [], "masters": [], "uuids": [ "e9842005-de6f-45ec-b782-a60f850f532d" ] }, "sectors": "335542239", "sectorsize": 512, "size": "160.00 GB", "start": "2048", "uuid": "e9842005-de6f-45ec-b782-a60f850f532d" } }, "removable": "0", "rotational": "1", "sas_address": null, "sas_device_handle": null, "scheduler_mode": "mq-deadline", "sectors": "335544320", "sectorsize": "512", "size": "160.00 GB", "support_discard": "0", "vendor": "0x1af4", "virtual": 1 } }, "ansible_distribution": "CentOS", "ansible_distribution_file_parsed": true, "ansible_distribution_file_path": "/etc/redhat-release", "ansible_distribution_file_variety": "RedHat", "ansible_distribution_major_version": "7", "ansible_distribution_release": "Core", "ansible_distribution_version": "7.7", "ansible_dns": { "nameservers": [ "108.61.10.10" ] }, "ansible_domain": "local", "ansible_effective_group_id": 0, "ansible_effective_user_id": 0, "ansible_env": { "HOME": "/root", "LANG": "en_US.UTF-8", "LC_ADDRESS": "vi_VN", "LC_IDENTIFICATION": "vi_VN", "LC_MEASUREMENT": "vi_VN", "LC_MONETARY": "vi_VN", "LC_NAME": "vi_VN", "LC_NUMERIC": "vi_VN", "LC_PAPER": "vi_VN", "LC_TELEPHONE": "vi_VN", "LC_TIME": "vi_VN", "LESSOPEN": "||/usr/bin/lesspipe.sh %s", "LOGNAME": "root", "LS_COLORS": "rs=0:di=38;5;27:ln=38;5;51:mh=44;38;5;15:pi=40;38;5;11:so=38;5;13:do=38;5;5:bd=48;5;232;38;5;11:cd=48;5;232;38;5;3:or=48;5;232;38;5;9:mi=05;48;5;232;38;5;15:su=48;5;196;38;5;15:sg=48;5;11;38;5;16:ca=48;5;196;38;5;226:tw=48;5;10;38;5;16:ow=48;5;10;38;5;21:st=48;5;21;38;5;15:ex=38;5;34:*.tar=38;5;9:*.tgz=38;5;9:*.arc=38;5;9:*.arj=38;5;9:*.taz=38;5;9:*.lha=38;5;9:*.lz4=38;5;9:*.lzh=38;5;9:*.lzma=38;5;9:*.tlz=38;5;9:*.txz=38;5;9:*.tzo=38;5;9:*.t7z=38;5;9:*.zip=38;5;9:*.z=38;5;9:*.Z=38;5;9:*.dz=38;5;9:*.gz=38;5;9:*.lrz=38;5;9:*.lz=38;5;9:*.lzo=38;5;9:*.xz=38;5;9:*.bz2=38;5;9:*.bz=38;5;9:*.tbz=38;5;9:*.tbz2=38;5;9:*.tz=38;5;9:*.deb=38;5;9:*.rpm=38;5;9:*.jar=38;5;9:*.war=38;5;9:*.ear=38;5;9:*.sar=38;5;9:*.rar=38;5;9:*.alz=38;5;9:*.ace=38;5;9:*.zoo=38;5;9:*.cpio=38;5;9:*.7z=38;5;9:*.rz=38;5;9:*.cab=38;5;9:*.jpg=38;5;13:*.jpeg=38;5;13:*.gif=38;5;13:*.bmp=38;5;13:*.pbm=38;5;13:*.pgm=38;5;13:*.ppm=38;5;13:*.tga=38;5;13:*.xbm=38;5;13:*.xpm=38;5;13:*.tif=38;5;13:*.tiff=38;5;13:*.png=38;5;13:*.svg=38;5;13:*.svgz=38;5;13:*.mng=38;5;13:*.pcx=38;5;13:*.mov=38;5;13:*.mpg=38;5;13:*.mpeg=38;5;13:*.m2v=38;5;13:*.mkv=38;5;13:*.webm=38;5;13:*.ogm=38;5;13:*.mp4=38;5;13:*.m4v=38;5;13:*.mp4v=38;5;13:*.vob=38;5;13:*.qt=38;5;13:*.nuv=38;5;13:*.wmv=38;5;13:*.asf=38;5;13:*.rm=38;5;13:*.rmvb=38;5;13:*.flc=38;5;13:*.avi=38;5;13:*.fli=38;5;13:*.flv=38;5;13:*.gl=38;5;13:*.dl=38;5;13:*.xcf=38;5;13:*.xwd=38;5;13:*.yuv=38;5;13:*.cgm=38;5;13:*.emf=38;5;13:*.axv=38;5;13:*.anx=38;5;13:*.ogv=38;5;13:*.ogx=38;5;13:*.aac=38;5;45:*.au=38;5;45:*.flac=38;5;45:*.mid=38;5;45:*.midi=38;5;45:*.mka=38;5;45:*.mp3=38;5;45:*.mpc=38;5;45:*.ogg=38;5;45:*.ra=38;5;45:*.wav=38;5;45:*.axa=38;5;45:*.oga=38;5;45:*.spx=38;5;45:*.xspf=38;5;45:", "MAIL": "/var/mail/root", "PATH": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin", "PWD": "/root", "SHELL": "/bin/bash", "SHLVL": "2", "SSH_CLIENT": "1.55.0.30 29284 22", "SSH_CONNECTION": "1.55.0.30 29284 104.207.151.59 22", "SSH_TTY": "/dev/pts/0", "TERM": "xterm-256color", "USER": "root", "XDG_RUNTIME_DIR": "/run/user/0", "XDG_SESSION_ID": "139101", "_": "/usr/bin/python" }, "ansible_eth0": { "active": true, "device": "eth0", "features": { "busy_poll": "off [fixed]", "fcoe_mtu": "off [fixed]", "generic_receive_offload": "on", "generic_segmentation_offload": "on", "highdma": "on [fixed]", "hw_tc_offload": "off [fixed]", "l2_fwd_offload": "off [fixed]", "large_receive_offload": "off [fixed]", "loopback": "off [fixed]", "netns_local": "off [fixed]", "ntuple_filters": "off [fixed]", "receive_hashing": "off [fixed]", "rx_all": "off [fixed]", "rx_checksumming": "on [fixed]", "rx_fcs": "off [fixed]", "rx_gro_hw": "off [fixed]", "rx_udp_tunnel_port_offload": "off [fixed]", "rx_vlan_filter": "on [fixed]", "rx_vlan_offload": "off [fixed]", "rx_vlan_stag_filter": "off [fixed]", "rx_vlan_stag_hw_parse": "off [fixed]", "scatter_gather": "on", "tcp_segmentation_offload": "on", "tx_checksum_fcoe_crc": "off [fixed]", "tx_checksum_ip_generic": "on", "tx_checksum_ipv4": "off [fixed]", "tx_checksum_ipv6": "off [fixed]", "tx_checksum_sctp": "off [fixed]", "tx_checksumming": "on", "tx_fcoe_segmentation": "off [fixed]", "tx_gre_csum_segmentation": "off [fixed]", "tx_gre_segmentation": "off [fixed]", "tx_gso_partial": "off [fixed]", "tx_gso_robust": "off [fixed]", "tx_ipip_segmentation": "off [fixed]", "tx_lockless": "off [fixed]", "tx_nocache_copy": "off", "tx_scatter_gather": "on", "tx_scatter_gather_fraglist": "off [fixed]", "tx_sctp_segmentation": "off [fixed]", "tx_sit_segmentation": "off [fixed]", "tx_tcp6_segmentation": "on", "tx_tcp_ecn_segmentation": "on", "tx_tcp_mangleid_segmentation": "off", "tx_tcp_segmentation": "on", "tx_udp_tnl_csum_segmentation": "off [fixed]", "tx_udp_tnl_segmentation": "off [fixed]", "tx_vlan_offload": "off [fixed]", "tx_vlan_stag_hw_insert": "off [fixed]", "udp_fragmentation_offload": "on", "vlan_challenged": "off [fixed]" }, "hw_timestamp_filters": [], "ipv4": { "address": "104.207.151.59", "broadcast": "104.207.151.255", "netmask": "255.255.254.0", "network": "104.207.150.0" }, "macaddress": "56:00:02:05:95:22", "module": "virtio_net", "mtu": 1500, "pciid": "virtio0", "promisc": false, "timestamping": [ "rx_software", "software" ], "type": "ether" }, "ansible_fibre_channel_wwn": [], "ansible_fips": false, "ansible_form_factor": "Other", "ansible_fqdn": "drop_us_vip.local", "ansible_hostname": "sweb-1", "ansible_hostnqn": "", "ansible_interfaces": [ "lo", "eth0" ], "ansible_is_chroot": false, "ansible_iscsi_iqn": "", "ansible_kernel": "3.10.0-957.12.1.el7.x86_64", "ansible_kernel_version": "#1 SMP Mon Apr 29 14:59:59 UTC 2019", "ansible_lo": { "active": true, "device": "lo", "features": { "busy_poll": "off [fixed]", "fcoe_mtu": "off [fixed]", "generic_receive_offload": "on", "generic_segmentation_offload": "on", "highdma": "on [fixed]", "hw_tc_offload": "off [fixed]", "l2_fwd_offload": "off [fixed]", "large_receive_offload": "off [fixed]", "loopback": "on [fixed]", "netns_local": "on [fixed]", "ntuple_filters": "off [fixed]", "receive_hashing": "off [fixed]", "rx_all": "off [fixed]", "rx_checksumming": "on [fixed]", "rx_fcs": "off [fixed]", "rx_gro_hw": "off [fixed]", "rx_udp_tunnel_port_offload": "off [fixed]", "rx_vlan_filter": "off [fixed]", "rx_vlan_offload": "off [fixed]", "rx_vlan_stag_filter": "off [fixed]", "rx_vlan_stag_hw_parse": "off [fixed]", "scatter_gather": "on", "tcp_segmentation_offload": "on", "tx_checksum_fcoe_crc": "off [fixed]", "tx_checksum_ip_generic": "on [fixed]", "tx_checksum_ipv4": "off [fixed]", "tx_checksum_ipv6": "off [fixed]", "tx_checksum_sctp": "on [fixed]", "tx_checksumming": "on", "tx_fcoe_segmentation": "off [fixed]", "tx_gre_csum_segmentation": "off [fixed]", "tx_gre_segmentation": "off [fixed]", "tx_gso_partial": "off [fixed]", "tx_gso_robust": "off [fixed]", "tx_ipip_segmentation": "off [fixed]", "tx_lockless": "on [fixed]", "tx_nocache_copy": "off [fixed]", "tx_scatter_gather": "on [fixed]", "tx_scatter_gather_fraglist": "on [fixed]", "tx_sctp_segmentation": "on", "tx_sit_segmentation": "off [fixed]", "tx_tcp6_segmentation": "on", "tx_tcp_ecn_segmentation": "on", "tx_tcp_mangleid_segmentation": "on", "tx_tcp_segmentation": "on", "tx_udp_tnl_csum_segmentation": "off [fixed]", "tx_udp_tnl_segmentation": "off [fixed]", "tx_vlan_offload": "off [fixed]", "tx_vlan_stag_hw_insert": "off [fixed]", "udp_fragmentation_offload": "on", "vlan_challenged": "on [fixed]" }, "hw_timestamp_filters": [], "ipv4": { "address": "127.0.0.1", "broadcast": "host", "netmask": "255.0.0.0", "network": "127.0.0.0" }, "mtu": 65536, "promisc": false, "timestamping": [ "rx_software", "software" ], "type": "loopback" }, "ansible_local": {}, "ansible_lsb": {}, "ansible_machine": "x86_64", "ansible_machine_id": "2ebd9a45d094438fbe08857df978b94e", "ansible_memfree_mb": 731, "ansible_memory_mb": { "nocache": { "free": 3319, "used": 4502 }, "real": { "free": 731, "total": 7821, "used": 7090 }, "swap": { "cached": 12, "free": 3615, "total": 4095, "used": 480 } }, "ansible_memtotal_mb": 7821, "ansible_mounts": [ { "block_available": 18812069, "block_size": 4096, "block_total": 41291712, "block_used": 22479643, "device": "/dev/vda1", "fstype": "ext4", "inode_available": 8798388, "inode_total": 10240000, "inode_used": 1441612, "mount": "/", "options": "rw,relatime,data=ordered", "size_available": 77054234624, "size_total": 169130852352, "uuid": "e9842005-de6f-45ec-b782-a60f850f532d" } ], "ansible_nodename": "sweb-1", "ansible_os_family": "RedHat", "ansible_pkg_mgr": "yum", "ansible_proc_cmdline": { "BOOT_IMAGE": "/boot/vmlinuz-3.10.0-957.12.1.el7.x86_64", "LANG": "en_US.UTF-8", "consoleblank": "0", "crashkernel": "auto", "quiet": true, "rhgb": true, "ro": true, "root": "UUID=e9842005-de6f-45ec-b782-a60f850f532d" }, "ansible_processor": [ "0", "GenuineIntel", "Virtual CPU 82d9ed4018dd", "1", "GenuineIntel", "Virtual CPU 82d9ed4018dd", "2", "GenuineIntel", "Virtual CPU 82d9ed4018dd", "3", "GenuineIntel", "Virtual CPU 82d9ed4018dd" ], "ansible_processor_cores": 4, "ansible_processor_count": 1, "ansible_processor_threads_per_core": 1, "ansible_processor_vcpus": 4, "ansible_product_name": "Standard PC (i440FX + PIIX, 1996)", "ansible_product_serial": "NA", "ansible_product_uuid": "2EBD9A45-D094-438F-BE08-857DF978B94E", "ansible_product_version": "pc-i440fx-3.0", "ansible_python": { "executable": "/usr/bin/python", "has_sslcontext": true, "type": "CPython", "version": { "major": 2, "micro": 5, "minor": 7, "releaselevel": "final", "serial": 0 }, "version_info": [ 2, 7, 5, "final", 0 ] }, "ansible_python_version": "2.7.5", "ansible_real_group_id": 0, "ansible_real_user_id": 0, "ansible_selinux": { "status": "disabled" }, "ansible_selinux_python_present": true, "ansible_service_mgr": "systemd", "ansible_ssh_host_key_ecdsa_public": "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBAlvQIQIWrMz+7fdSZXJgzG7QLU+HFLtWrFfTqFwXcZKhymtD0ZiQMbf/HbMwCvX9aBtc+2QpuRrQ19/kaJIn/I=", "ansible_ssh_host_key_ed25519_public": "AAAAC3NzaC1lZDI1NTE5AAAAIPhM8eEJyUQtpPUAy/aNn/BAb5/sup+oDX4syVp3ua5N", "ansible_ssh_host_key_rsa_public": "AAAAB3NzaC1yc2EAAAADAQABAAABAQDVpf5A31WdX6L1N1FRUBb5bPNiN4rmmi3yShBvyFwhnarC6kgFEty0wZd17MfLm3VMZRBVUlD913b5ZVUugGgVImNvXlMVzSs7hAE+NMQSh1Mu2zn8A5X+ZoCyYIgsCLCkpqA7KOUs4J6T5ArdVMsVp/AxHcJw41yYdLyONy7EaGW1+c3jFly6AtDWekWbJiw4XXzOaBl9P5HWBUT7mUQdd6p2UOvoXJUPHTNZtEEcmaRDGX1Ai0IFTD/gdKxTGtTmbbR5YCNPsw1sgdBtPdqLEBHHXyclAupAdnl0o+9Iv6oXnY89lWdOPZh2n7Q6r9HDmqKImMIu/92lwCcahEAL", "ansible_swapfree_mb": 3615, "ansible_swaptotal_mb": 4095, "ansible_system": "Linux", "ansible_system_capabilities": [ "cap_chown", "cap_dac_override", "cap_dac_read_search", "cap_fowner", "cap_fsetid", "cap_kill", "cap_setgid", "cap_setuid", "cap_setpcap", "cap_linux_immutable", "cap_net_bind_service", "cap_net_broadcast", "cap_net_admin", "cap_net_raw", "cap_ipc_lock", "cap_ipc_owner", "cap_sys_module", "cap_sys_rawio", "cap_sys_chroot", "cap_sys_ptrace", "cap_sys_pacct", "cap_sys_admin", "cap_sys_boot", "cap_sys_nice", "cap_sys_resource", "cap_sys_time", "cap_sys_tty_config", "cap_mknod", "cap_lease", "cap_audit_write", "cap_audit_control", "cap_setfcap", "cap_mac_override", "cap_mac_admin", "cap_syslog", "35", "36+ep" ], "ansible_system_capabilities_enforced": "True", "ansible_system_vendor": "QEMU", "ansible_uptime_seconds": 8165867, "ansible_user_dir": "/root", "ansible_user_gecos": "root", "ansible_user_gid": 0, "ansible_user_id": "root", "ansible_user_shell": "/bin/bash", "ansible_user_uid": 0, "ansible_userspace_architecture": "x86_64", "ansible_userspace_bits": "64", "ansible_virtualization_role": "guest", "ansible_virtualization_type": "kvm", "discovered_interpreter_python": "/usr/bin/python", "gather_subset": [ "all" ], "module_setup": true }, "changed": false }
Kết quả trả về định dạng json, bạn có thể lên https://jsoneditoronline.org/, copy vào nó sẽ ra filter cho mình dễ nhìn.
Các bạn có thể nhìn rõ hơn tại link sau: https://jsoneditoronline.org/#left=cloud.dca33b468f7d4f2a98f98d9367081e52
Có kết quả rồi bạn muốn lấy biến nào cũng sẽ được, ví dụ để lấy IP của host, mình sẽ đi theo cây thư mục vào như hình sau:
như vậy mình sẽ có variables là {{ ansible_eth0.ipv4.address }}, thử xem đúng ko bạn có thể kiểm chứng lại với ansible-playbook
– Tạo 1 file main.yml
--- - name: collect hosts: all become: yes become_method: sudo tasks: - debug: msg: '{{ansible_eth0.ipv4.address}}'
run playbook:
sudo ansible-playbook -i hosts test.yml -u root |
đây là kết quả:
tritran@tritran-Latitude-E6540:~/Desktop/Ansible/LEMP-COMPILE-CENTOS7_v1.2$ sudo ansible-playbook -i hosts test.yml -u root
PLAY [collect] ****************************************************************************************************************************************************************************************************** TASK [Gathering Facts] ********************************************************************************************************************************************************************************************** ok: [104.207.151.59] TASK [debug] ******************************************************************************************************************************************************************************************************** ok: [104.207.151.59] => { “msg”: “104.207.151.59” } PLAY RECAP ********************************************************************************************************************************************************************************************************** 104.207.151.59 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 |
Tương tự, mình muốn lấy dung lượng disk của host chẳng hạn, sẽ lấy variable {{ansible_devices.vda.size}}
tritran@tritran-Latitude-E6540:~/Desktop/Ansible/LEMP-COMPILE-CENTOS7_v1.2$ sudo ansible-playbook -i hosts test.yml -u root
PLAY [collect] ****************************************************************************************************************************************************************************************************** TASK [Gathering Facts] ********************************************************************************************************************************************************************************************** ok: [104.207.151.59] TASK [debug] ******************************************************************************************************************************************************************************************************** ok: [104.207.151.59] => { “msg”: “160.00 GB” } PLAY RECAP ********************************************************************************************************************************************************************************************************** 104.207.151.59 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 |
Để show all variables cả trên host và tự định nghĩa, bạn dùng var: hostvars[inventory_hostname]
--- - name: collect hosts: all become: yes become_method: sudo tasks: - debug: var: hostvars[inventory_hostname]
II. Lý do
Mục đích lấy variable này làm gì nhỉ? 😐 😐 😐
– Thu thập thông tin. Bạn có thể biết nhiều thông tin khác nhau như OS type, disk, memory…
– Sử dụng variable này vào playbook mà không cần tư định nghĩa. Điều này giúp playbook mình chuyên nghiệp hơn nhiều đấy.
III. Tổng kết
Kết thúc bài giới thiệu, đây không hẳn là một tuts, chỉ mong là các bạn vận dụng nó như một trick giúp công việc trôi chảy hơn. Thân chào và cám ơn các bạn!