ansible playbook剧本
- [一、ansible playbook](#一、ansible playbook)
-
- 1、介绍
- 2、YAML语法的注意事项
- 3、playbook的结构
- 4、playbook的使用流程
-
- [4.1 编写剧本](#4.1 编写剧本)
- [4.2 执行剧本](#4.2 执行剧本)
- 5、部署MySQL剧本
- 二、setup模块的使用
-
- [2.1 取消anisble调用setup行为](#2.1 取消anisble调用setup行为)
- 三、playbook变量的使用
一、ansible playbook
1、介绍
便于功能的重用
本质上是文本文件,xxxx.yml, xxxx.yaml
遵循YAML语法
2、YAML语法的注意事项
1、一个键对应一个值时,冒号后必须有空格
yaml
key: value
2、一个键对应多个值时
yaml
key:
value
value
key:
value
value
3、同逻辑的代码缩进必须一致,建议是4个空格
3、playbook的结构
yaml
- hosts: 被管理机
tasks:
- name: 任务名称
模块名称:
参数
参数
- name: 任务名称
模块名称:
参数
参数
4、playbook的使用流程
4.1 编写剧本
yaml
[root@zabbix_server work]# cat user.yaml
- hosts: db
tasks:
- name: create user named hadoop
user:
name: hadoop
shell: /sbin/nologin
state: present
4.2 执行剧本
bash
[root@zabbix_server work]# ansible-playbook user.yaml
PLAY [db] ****************************************************************************************************************************
TASK [Gathering Facts] ***************************************************************************************************************
ok: [192.168.140.11]
ok: [192.168.140.12]
ok: [192.168.140.13]
TASK [create user named hadoop] ******************************************************************************************************
changed: [192.168.140.11]
changed: [192.168.140.12]
changed: [192.168.140.13]
PLAY RECAP ***************************************************************************************************************************
192.168.140.11 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.140.12 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.140.13 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
5、部署MySQL剧本
yaml
[root@zabbix_server work]# cat installMySQL.yaml
- hosts: db
tasks:
- name: install MySQL
yum:
name: mariadb-server
state: present
- name: push MySQL config
copy:
src: /opt/work/my.cnf
dest: /etc/my.cnf
- name: start MySQL daemon
service:
name: mariadb
state: started
enabled: yes
二、setup模块的使用
ansible执行剧本时,会自动执行一个任务,名称叫Gathering Facts。
自动调用setup模块搜集被管理机的状态数据(IP、主机名、CPU、系统版本),统称叫Facts变量
bash
[root@zabbix_server work]# ansible 192.168.140.12 -m setup
192.168.140.12 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"192.168.140.12"
],
"ansible_all_ipv6_addresses": [],
"ansible_apparmor": {
"status": "disabled"
},
"ansible_architecture": "x86_64",
"ansible_bios_date": "07/22/2020",
"ansible_bios_version": "6.00",
"ansible_cmdline": {
"BOOT_IMAGE": "/vmlinuz-3.10.0-1160.el7.x86_64",
"LANG": "en_US.UTF-8",
"crashkernel": "auto",
"quiet": true,
"rd.lvm.lv": "centos/swap",
"rhgb": true,
"ro": true,
"root": "/dev/mapper/centos-root",
"spectre_v2": "retpoline"
},
"ansible_date_time": {
"date": "2024-06-25",
"day": "25",
"epoch": "1719300403",
"hour": "15",
"iso8601": "2024-06-25T07:26:43Z",
"iso8601_basic": "20240625T152643632527",
"iso8601_basic_short": "20240625T152643",
"iso8601_micro": "2024-06-25T07:26:43.632527Z",
"minute": "26",
"month": "06",
"second": "43",
"time": "15:26:43",
"tz": "CST",
"tz_offset": "+0800",
"weekday": "Tuesday",
"weekday_number": "2",
"weeknumber": "26",
"year": "2024"
},
"ansible_default_ipv4": {
"address": "192.168.140.12",
"alias": "ens33",
"broadcast": "192.168.140.255",
"gateway": "192.168.140.2",
"interface": "ens33",
"macaddress": "00:0c:29:05:24:99",
"mtu": 1500,
"netmask": "255.255.255.0",
"network": "192.168.140.0",
"type": "ether"
},
"ansible_default_ipv6": {},
"ansible_device_links": {
"ids": {
"dm-0": [
"dm-name-centos-root",
"dm-uuid-LVM-MkyTiscfjoyt81RkaAXML7107FgBvc8KVvylypSilpl9cEVvJhEzG6H1Idg41rre"
],
"dm-1": [
"dm-name-centos-swap",
"dm-uuid-LVM-MkyTiscfjoyt81RkaAXML7107FgBvc8KB0zTEQKyCM4MQcIFRhi7FQAzc30PfS5e"
],
"sda2": [
"lvm-pv-uuid-6KAZv5-pbxy-Ykio-i5g6-czBQ-LwVu-wkGBjn"
],
"sr0": [
"ata-VMware_Virtual_IDE_CDROM_Drive_10000000000000000001"
]
},
"labels": {
"sr0": [
"CentOS\\x207\\x20x86_64"
]
},
"masters": {
"sda2": [
"dm-0",
"dm-1"
]
},
"uuids": {
"dm-0": [
"df52b733-b621-418b-ac62-882673d15ac5"
],
"dm-1": [
"569fef35-800d-4b72-90f2-bca8469b5ede"
],
"sda1": [
"00d6ad9d-08cc-4ff7-979a-0d55b671c317"
],
"sr0": [
"2020-11-02-15-15-23-00"
]
}
},
"ansible_devices": {
"dm-0": {
"holders": [],
"host": "",
"links": {
"ids": [
"dm-name-centos-root",
"dm-uuid-LVM-MkyTiscfjoyt81RkaAXML7107FgBvc8KVvylypSilpl9cEVvJhEzG6H1Idg41rre"
],
"labels": [],
"masters": [],
"uuids": [
"df52b733-b621-418b-ac62-882673d15ac5"
]
},
"model": null,
"partitions": {},
"removable": "0",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "",
"sectors": "36716544",
"sectorsize": "512",
"size": "17.51 GB",
"support_discard": "0",
"vendor": null,
"virtual": 1
},
"dm-1": {
"holders": [],
"host": "",
"links": {
"ids": [
"dm-name-centos-swap",
"dm-uuid-LVM-MkyTiscfjoyt81RkaAXML7107FgBvc8KB0zTEQKyCM4MQcIFRhi7FQAzc30PfS5e"
],
"labels": [],
"masters": [],
"uuids": [
"569fef35-800d-4b72-90f2-bca8469b5ede"
]
},
"model": null,
"partitions": {},
"removable": "0",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "",
"sectors": "4194304",
"sectorsize": "512",
"size": "2.00 GB",
"support_discard": "0",
"vendor": null,
"virtual": 1
},
"sda": {
"holders": [],
"host": "SCSI storage controller: Broadcom / LSI 53c1030 PCI-X Fusion-MPT Dual Ultra320 SCSI (rev 01)",
"links": {
"ids": [],
"labels": [],
"masters": [],
"uuids": []
},
"model": "VMware Virtual S",
"partitions": {
"sda1": {
"holders": [],
"links": {
"ids": [],
"labels": [],
"masters": [],
"uuids": [
"00d6ad9d-08cc-4ff7-979a-0d55b671c317"
]
},
"sectors": "1024000",
"sectorsize": 512,
"size": "500.00 MB",
"start": "2048",
"uuid": "00d6ad9d-08cc-4ff7-979a-0d55b671c317"
},
"sda2": {
"holders": [
"centos-root",
"centos-swap"
],
"links": {
"ids": [
"lvm-pv-uuid-6KAZv5-pbxy-Ykio-i5g6-czBQ-LwVu-wkGBjn"
],
"labels": [],
"masters": [
"dm-0",
"dm-1"
],
"uuids": []
},
"sectors": "40916992",
"sectorsize": 512,
"size": "19.51 GB",
"start": "1026048",
"uuid": null
}
},
"removable": "0",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "deadline",
"sectors": "41943040",
"sectorsize": "512",
"size": "20.00 GB",
"support_discard": "0",
"vendor": "VMware,",
"virtual": 1
},
"sr0": {
"holders": [],
"host": "IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)",
"links": {
"ids": [
"ata-VMware_Virtual_IDE_CDROM_Drive_10000000000000000001"
],
"labels": [
"CentOS\\x207\\x20x86_64"
],
"masters": [],
"uuids": [
"2020-11-02-15-15-23-00"
]
},
"model": "VMware IDE CDR10",
"partitions": {},
"removable": "1",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "deadline",
"sectors": "19922944",
"sectorsize": "2048",
"size": "9.50 GB",
"support_discard": "0",
"vendor": "NECVMWar",
"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.9",
"ansible_dns": {
"nameservers": [
"223.5.5.5",
"114.114.114.114"
],
"search": [
"linux.com"
]
},
"ansible_domain": "linux.com",
"ansible_effective_group_id": 0,
"ansible_effective_user_id": 0,
"ansible_ens33": {
"active": true,
"device": "ens33",
"features": {
"busy_poll": "off [fixed]",
"fcoe_mtu": "off [fixed]",
"generic_receive_offload": "on",
"generic_segmentation_offload": "on",
"highdma": "off [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",
"rx_checksumming": "off",
"rx_fcs": "off",
"rx_gro_hw": "off [fixed]",
"rx_udp_tunnel_port_offload": "off [fixed]",
"rx_vlan_filter": "on [fixed]",
"rx_vlan_offload": "on",
"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": "off [fixed]",
"tx_tcp_ecn_segmentation": "off [fixed]",
"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": "on [fixed]",
"tx_vlan_stag_hw_insert": "off [fixed]",
"udp_fragmentation_offload": "off [fixed]",
"vlan_challenged": "off [fixed]"
},
"hw_timestamp_filters": [],
"ipv4": {
"address": "192.168.140.12",
"broadcast": "192.168.140.255",
"netmask": "255.255.255.0",
"network": "192.168.140.0"
},
"macaddress": "00:0c:29:05:24:99",
"module": "e1000",
"mtu": 1500,
"pciid": "0000:02:01.0",
"promisc": false,
"speed": 1000,
"timestamping": [
"tx_software",
"rx_software",
"software"
],
"type": "ether"
},
"ansible_env": {
"HOME": "/root",
"LANG": "C",
"LC_ALL": "C",
"LC_NUMERIC": "C",
"LESSOPEN": "||/usr/bin/lesspipe.sh %s",
"LOGNAME": "root",
"LS_COLORS": "rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:",
"MAIL": "/var/mail/root",
"PATH": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin",
"PWD": "/root",
"SHELL": "/bin/bash",
"SHLVL": "2",
"SSH_CLIENT": "192.168.140.10 35806 22",
"SSH_CONNECTION": "192.168.140.10 35806 192.168.140.12 22",
"SSH_TTY": "/dev/pts/1",
"TERM": "xterm",
"USER": "root",
"XDG_RUNTIME_DIR": "/run/user/0",
"XDG_SESSION_ID": "43",
"_": "/usr/bin/python"
},
"ansible_fibre_channel_wwn": [],
"ansible_fips": false,
"ansible_form_factor": "Other",
"ansible_fqdn": "node02.linux.com",
"ansible_hostname": "node02",
"ansible_hostnqn": "",
"ansible_interfaces": [
"lo",
"ens33"
],
"ansible_is_chroot": false,
"ansible_iscsi_iqn": "",
"ansible_kernel": "3.10.0-1160.el7.x86_64",
"ansible_kernel_version": "#1 SMP Mon Oct 19 16:18:59 UTC 2020",
"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": "",
"netmask": "255.0.0.0",
"network": "127.0.0.0"
},
"ipv6": [
{
"address": "::1",
"prefix": "128",
"scope": "host"
}
],
"mtu": 65536,
"promisc": false,
"timestamping": [
"rx_software",
"software"
],
"type": "loopback"
},
"ansible_local": {},
"ansible_lsb": {},
"ansible_lvm": {
"lvs": {
"root": {
"size_g": "17.51",
"vg": "centos"
},
"swap": {
"size_g": "2.00",
"vg": "centos"
}
},
"pvs": {
"/dev/sda2": {
"free_g": "0",
"size_g": "19.51",
"vg": "centos"
}
},
"vgs": {
"centos": {
"free_g": "0",
"num_lvs": "2",
"num_pvs": "1",
"size_g": "19.51"
}
}
},
"ansible_machine": "x86_64",
"ansible_machine_id": "cbec721ed927407db9b6b63703860c84",
"ansible_memfree_mb": 161,
"ansible_memory_mb": {
"nocache": {
"free": 691,
"used": 281
},
"real": {
"free": 161,
"total": 972,
"used": 811
},
"swap": {
"cached": 0,
"free": 2047,
"total": 2047,
"used": 0
}
},
"ansible_memtotal_mb": 972,
"ansible_mounts": [
{
"block_available": 93832,
"block_size": 4096,
"block_total": 127145,
"block_used": 33313,
"device": "/dev/sda1",
"fstype": "xfs",
"inode_available": 255674,
"inode_total": 256000,
"inode_used": 326,
"mount": "/boot",
"options": "rw,relatime,attr2,inode64,noquota",
"size_available": 384335872,
"size_total": 520785920,
"uuid": "00d6ad9d-08cc-4ff7-979a-0d55b671c317"
},
{
"block_available": 4146076,
"block_size": 4096,
"block_total": 4587008,
"block_used": 440932,
"device": "/dev/mapper/centos-root",
"fstype": "xfs",
"inode_available": 9145486,
"inode_total": 9179136,
"inode_used": 33650,
"mount": "/",
"options": "rw,relatime,attr2,inode64,noquota",
"size_available": 16982327296,
"size_total": 18788384768,
"uuid": "df52b733-b621-418b-ac62-882673d15ac5"
}
],
"ansible_nodename": "node02.linux.com",
"ansible_os_family": "RedHat",
"ansible_pkg_mgr": "yum",
"ansible_proc_cmdline": {
"BOOT_IMAGE": "/vmlinuz-3.10.0-1160.el7.x86_64",
"LANG": "en_US.UTF-8",
"crashkernel": "auto",
"quiet": true,
"rd.lvm.lv": [
"centos/root",
"centos/swap"
],
"rhgb": true,
"ro": true,
"root": "/dev/mapper/centos-root",
"spectre_v2": "retpoline"
},
"ansible_processor": [
"0",
"GenuineIntel",
"Intel(R) Core(TM) i7-10510U CPU @ 1.80GHz",
"1",
"GenuineIntel",
"Intel(R) Core(TM) i7-10510U CPU @ 1.80GHz",
"2",
"GenuineIntel",
"Intel(R) Core(TM) i7-10510U CPU @ 1.80GHz",
"3",
"GenuineIntel",
"Intel(R) Core(TM) i7-10510U CPU @ 1.80GHz"
],
"ansible_processor_cores": 1,
"ansible_processor_count": 4,
"ansible_processor_threads_per_core": 1,
"ansible_processor_vcpus": 4,
"ansible_product_name": "VMware Virtual Platform",
"ansible_product_serial": "VMware-56 4d 83 e5 ca 95 7f 00-b7 f5 03 34 13 05 24 99",
"ansible_product_uuid": "E5834D56-95CA-007F-B7F5-033413052499",
"ansible_product_version": "None",
"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": "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBIx9IBu2t0KQUVpl8kYW7vhNVHj/Ibhi+TENpAjeVjrZRyzFArhP7cKcxLMKi52uVB7FtJIL+B5uGtahNLSOJyo=",
"ansible_ssh_host_key_ed25519_public": "AAAAC3NzaC1lZDI1NTE5AAAAILPVxfjQ4hpJdkZpXkohxY1BUr2oFKE6BH8oNMHCZTae",
"ansible_ssh_host_key_rsa_public": "AAAAB3NzaC1yc2EAAAADAQABAAABAQDsnz/IO8vaZ/HLyVp/8lGyQ/uxW8Q2GHG/B5Hun/ORGBJQ55+qw67OIXvibrU6hiVbjKAZ8FSRCK+6JnCU+YJMUqBh17huV4fFe148aju0rCDl17158mPxVqx/PNEnLJpXfVqeRkfd9jvYs5iGi7EyYPuBhsN3nmrM5tmLCm8EmHmZlcwb+QLK60WcwMm7vLi9ea1zdC7k+W87BMRWgriElCTqsbQdaurAvT5cSDJ0hmtHI5mG5F9WOfMnJJYu23SRiErrkxV5WqcsFftjjF0DOGpZI6XLnWYcnxq5+GVAQzZqn3p/sswMif8liE1q3eoN6pUI7TCfPFCjM61frDN7",
"ansible_swapfree_mb": 2047,
"ansible_swaptotal_mb": 2047,
"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": "VMware, Inc.",
"ansible_uptime_seconds": 21660,
"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": "VMware",
"discovered_interpreter_python": "/usr/bin/python",
"gather_subset": [
"all"
],
"module_setup": true
},
"changed": false
}
2.1 取消anisble调用setup行为
yaml
- hosts: db
gather_facts: false
tasks:
- name: create user named hadoop
user:
name: hadoop
shell: /sbin/nologin
state: present
三、playbook变量的使用
1、支持调用变量
yaml
{{ 变量名称 }}
2、定义变量
2.1 使用vars关键字定义变量
yaml
[root@zabbix_server work]# cat user.yaml
- hosts: db
gather_facts: false
vars:
- user_name: "AA"
- sh_name: "/bin/false"
tasks:
- name: create user
user:
name: "{{ user_name }}"
shell: "{{ sh_name }}"
state: present
2.2 主机清单文件定义变量
2.2.1 为单个主机定义变量
yaml
[db]
192.168.140.11 user_name="c1" sh_name="/bin/bash"
192.168.140.12 user_name="c2" sh_name="/sbin/nologin"
192.168.140.13 ansible_ssh_user="root" ansible_ssh_pass="redhat" ansible_ssh_port=22 user_name="c3" sh_name="/bin/false"
2.2.2 为主机组定义变量
yaml
[db:vars]
user_name="BB"
sh_name="/sbin/nologin"
2.3 在外部文件中定义变量
yaml
[root@zabbix_server work]# cat userInfo
user_name: "D1"
sh_name: "/bin/bash"
[root@zabbix_server work]# cat user.yaml
- hosts: db
gather_facts: false
vars_files:
- /opt/work/userInfo
tasks:
- name: create user
user:
name: "{{ user_name }}"
shell: "{{ sh_name }}"
state: present
2.3.1 变量文件进行加密
bash
[root@zabbix_server work]# ansible-vault encrypt userInfo
New Vault password:
Confirm New Vault password:
Encryption successful
[root@zabbix_server work]# cat userInfo
$ANSIBLE_VAULT;1.1;AES256
33343031333062363163333533653662356235336239643964333239623564616237623964663636
6330633231373630393935653763336132383636663033380a653631626439613561346636653466
61663031646365623861326239303531633030363664666261666139373661306132616362363965
3930316262306536650a633166323139393631303039346438306465623430623565616265396431
37343365333566653837653638386236323063323662656366663062366461356465313832316563
6236613664303333623231636231623738633635306362623837
bash
[root@zabbix_server work]# ansible-playbook --ask-vault user.yaml
Vault password:
2.3.2 变量文件进行解密
bash
[root@zabbix_server work]# ansible-vault decrypt userInfo
Vault password:
Decryption successful
[root@zabbix_server work]# cat userInfo
user_name: "D1"
sh_name: "/bin/bash"
2.4 部署MySQL
bash
[db]
192.168.140.11 db_id=11
192.168.140.12 db_id=12
192.168.140.13 ansible_ssh_user="root" ansible_ssh_pass="redhat" ansible_ssh_port=22 db_id=13
[db:vars]
db_port=3307
bash
[root@zabbix_server work]# cat my.cnf
[mysqld]
bind-address={{ ansible_ens33["ipv4"]["address"] }} // 调用setup模块中的数据
port={{ db_port }}
server_id={{ db_id }}
log_bin=master
.....................
yaml
- hosts: db
tasks:
- name: install MySQL
yum:
name: mariadb-server
state: present
- name: push MySQL config
template: // template模块触发变量替换
src: /opt/work/my.cnf
dest: /etc/my.cnf
- name: start MySQL daemon
service:
name: mariadb
state: started
enabled: yes