上传文件至 'ansible'

main
diandian 1 year ago
parent 541e37557b
commit 7ef99d3a3b

@ -0,0 +1,142 @@
<h1><center>Ansible-jinja2模板</center></h1>
------
**作者:行癫(盗版必究)**
## 一:简介
ansible通常使用jinja2模板来修改被管理主机的配置文件
使用ansible的jinja2模板也就是template模块该模块和copy模块一样都是将文件复制到远端主机上区别在于template模块可以获取要复制的文件中变量的值而copy则是原封不动的把文件内容复制过去比如针对不同的主机定义不同的变量template会在将配置文件分发出去之前读取变量到jinja2模板然后分发到不通的被管理主机上
ansible允许jinja2模板中使用条件判断和循环但是jinja2判断循环语法不允许在playbook中使用
jinja2 文件件以 .j2 为后缀, 也可以不写后缀
## 二:基本使用
#### 1.模板基本语法
要在配置文件中使用jinja2playbook中的tasks必须使用template模块
模板配置文件里面使用变量,比如{{ port }}或者使用{{ facts变量 }}
#### 2.模板逻辑关系
```jinja
{% for i in EXPR %}...{% endfor %}作为循环表达式
{% if EXPR %}...{% elif EXPR %}...{% endif %}作为条件判断
{# COMMENT #}表示注释
```
## 三:使用案例
#### 1.案例一
使用facts变量
```yaml
[root@db2 memcache]# cat jinja1.yaml
- hosts: all
tasks:
- name: copy template file /etc/motd
template: src=./motd.j2 dest=/etc/motd backup=yes
[root@db2 memcache]# cat motd.j2
Welcome to qingcheng service!!
This System Hostname: {{ ansible_hostname }}
This System total memory is: {{ ansible_memtotal_mb }} MB
This System free memory is: {{ ansible_memfree_mb }} MB
```
#### 2.案例二
nginx负载均衡变量
```yaml
[root@db2 memcache]# cat kod_proxy.conf.j2
upstream {{ server_name }} {
{% for i in range(1,10) %}
server 192.168.10.{{ i }};
{% endfor %}
}
server {
listen {{ http_port }};
server_name {{ server_name }};
location / {
proxy_pass http://{{ server_name }};
include proxy_params;
}
}
[root@db2 memcache]# cat jinja2.yaml
- hosts: webserver
vars:
- http_port: 80
- server_name: kod_qingchen.com
tasks:
- name: install nginx server
yum: name=nginx state=latest
- name: configure nginx virt
template: src=./kod_proxy.conf.j2 dest=/etc/nginx/conf.d/proxy_kod.conf
notify: Restart Nginx Server
- name: start nginx server
service: name=nginx state=started enabled=yes
handlers:
- name: Restart Nginx Server
service: name=nginx state=restarted
```
#### 3.案例三
keepalive主从
```shell
inventory中的host_vars根据不同主机设定不同的变量
在playbook中是when判断主机名称然后分发不同的配置文件
使用jinja2的方式渲染出不同的配置文件
```
```jinja2
[root@db2 memcache]# cat keepalived.yaml
- hosts: all
tasks:
- name: copy template keepalived configure
template: src=keepalived.j2 dest=/etc/keepalived/keepalived.conf
notify: Restart keepalived service
handler:
- name: Restart keepalived service
service: name=keepalived state=restarted
[root@db2 memcache]# cat keepalived.j2
global_defs {
router_id {{ ansible_fqdn }}
}
vrrp_instance VI_1 {
{% if ansible_fqdn == "web01" %}
state MASTER
priority 150
{% elif ansible_fqdn == "web02" %}
state BACKUP
priority 100
{% endif %}
interface eth0
virtual_router_id 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3
}
}
```

@ -0,0 +1,740 @@
<h1><center>Ansible之Setup模块</center></h1>
------
**作者:行癫(盗版必究)**
## 一:所有参数
```yml
10.0.0.111 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"10.0.0.111"
],
"ansible_all_ipv6_addresses": [
"fe80::a1d7:aadb:3ae3:bbb9"
],
"ansible_apparmor": {
"status": "disabled"
},
"ansible_architecture": "x86_64",
"ansible_bios_date": "11/12/2020",
"ansible_bios_version": "6.00",
"ansible_cmdline": {
"BOOT_IMAGE": "/vmlinuz-3.10.0-693.el7.x86_64",
"LANG": "en_US.UTF-8",
"quiet": true,
"rd.lvm.lv": "centos/root",
"rhgb": true,
"ro": true,
"root": "/dev/mapper/centos-root"
},
"ansible_date_time": {
"date": "2023-03-02",
"day": "02",
"epoch": "1677768313",
"hour": "22",
"iso8601": "2023-03-02T14:45:13Z",
"iso8601_basic": "20230302T224513178165",
"iso8601_basic_short": "20230302T224513",
"iso8601_micro": "2023-03-02T14:45:13.178165Z",
"minute": "45",
"month": "03",
"second": "13",
"time": "22:45:13",
"tz": "CST",
"tz_offset": "+0800",
"weekday": "Thursday",
"weekday_number": "4",
"weeknumber": "09",
"year": "2023"
},
"ansible_default_ipv4": {
"address": "10.0.0.111",
"alias": "ens33",
"broadcast": "10.0.0.255",
"gateway": "10.0.0.2",
"interface": "ens33",
"macaddress": "00:0c:29:0d:1c:29",
"mtu": 1500,
"netmask": "255.255.255.0",
"network": "10.0.0.0",
"type": "ether"
},
"ansible_default_ipv6": {},
"ansible_device_links": {
"ids": {
"dm-0": [
"dm-name-centos-root",
"dm-uuid-LVM-jtnKDeIbgg03hClVGR4QFsF0xdtxSolvn3Yfi9VBej4wXITukpJCMpFT914kP7cH"
],
"sda2": [
"lvm-pv-uuid-vWNEKk-udkQ-vl1K-YJck-9KMY-D13P-eAoAnR"
],
"sr0": [
"ata-VMware_Virtual_IDE_CDROM_Drive_10000000000000000001"
]
},
"labels": {
"sr0": [
"CentOS\\x207\\x20x86_64"
]
},
"masters": {
"sda2": [
"dm-0"
]
},
"uuids": {
"dm-0": [
"d0261be2-3486-4ff1-8563-8360393cfeb4"
],
"sda1": [
"bdfa051f-b5a8-4713-9bee-440b49233372"
],
"sr0": [
"2017-09-05-14-14-50-00"
]
}
},
"ansible_devices": {
"dm-0": {
"holders": [],
"host": "",
"links": {
"ids": [
"dm-name-centos-root",
"dm-uuid-LVM-jtnKDeIbgg03hClVGR4QFsF0xdtxSolvn3Yfi9VBej4wXITukpJCMpFT914kP7cH"
],
"labels": [],
"masters": [],
"uuids": [
"d0261be2-3486-4ff1-8563-8360393cfeb4"
]
},
"model": null,
"partitions": {},
"removable": "0",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "",
"sectors": "82853888",
"sectorsize": "512",
"size": "39.51 GB",
"support_discard": "0",
"vendor": null,
"virtual": 1
},
"sda": {
"holders": [],
"host": "",
"links": {
"ids": [],
"labels": [],
"masters": [],
"uuids": []
},
"model": "VMware Virtual S",
"partitions": {
"sda1": {
"holders": [],
"links": {
"ids": [],
"labels": [],
"masters": [],
"uuids": [
"bdfa051f-b5a8-4713-9bee-440b49233372"
]
},
"sectors": "1024000",
"sectorsize": 512,
"size": "500.00 MB",
"start": "2048",
"uuid": "bdfa051f-b5a8-4713-9bee-440b49233372"
},
"sda2": {
"holders": [
"centos-root"
],
"links": {
"ids": [
"lvm-pv-uuid-vWNEKk-udkQ-vl1K-YJck-9KMY-D13P-eAoAnR"
],
"labels": [],
"masters": [
"dm-0"
],
"uuids": []
},
"sectors": "82860032",
"sectorsize": 512,
"size": "39.51 GB",
"start": "1026048",
"uuid": null
}
},
"removable": "0",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "deadline",
"sectors": "83886080",
"sectorsize": "512",
"size": "40.00 GB",
"support_discard": "0",
"vendor": "VMware,",
"virtual": 1
},
"sr0": {
"holders": [],
"host": "",
"links": {
"ids": [
"ata-VMware_Virtual_IDE_CDROM_Drive_10000000000000000001"
],
"labels": [
"CentOS\\x207\\x20x86_64"
],
"masters": [],
"uuids": [
"2017-09-05-14-14-50-00"
]
},
"model": "VMware IDE CDR10",
"partitions": {},
"removable": "1",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "cfq",
"sectors": "1622016",
"sectorsize": "2048",
"size": "792.00 MB",
"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.4",
"ansible_dns": {
"nameservers": [
"10.0.0.2"
],
"search": [
"localdomain"
]
},
"ansible_domain": "",
"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_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_mpls_segmentation": "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": "10.0.0.111",
"broadcast": "10.0.0.255",
"netmask": "255.255.255.0",
"network": "10.0.0.0"
},
"ipv6": [
{
"address": "fe80::a1d7:aadb:3ae3:bbb9",
"prefix": "64",
"scope": "link"
}
],
"macaddress": "00:0c:29:0d:1c:29",
"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": "en_US.UTF-8",
"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": "10.0.0.110 51406 22",
"SSH_CONNECTION": "10.0.0.110 51406 10.0.0.111 22",
"SSH_TTY": "/dev/pts/1",
"TERM": "xterm-256color",
"USER": "root",
"XDG_RUNTIME_DIR": "/run/user/0",
"XDG_SESSION_ID": "15",
"_": "/usr/bin/python"
},
"ansible_fibre_channel_wwn": [],
"ansible_fips": false,
"ansible_form_factor": "Other",
"ansible_fqdn": "xingdian",
"ansible_hostname": "xingdian",
"ansible_hostnqn": "",
"ansible_interfaces": [
"lo",
"ens33"
],
"ansible_is_chroot": false,
"ansible_iscsi_iqn": "",
"ansible_kernel": "3.10.0-693.el7.x86_64",
"ansible_kernel_version": "#1 SMP Tue Aug 22 21:09:27 UTC 2017",
"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_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_mpls_segmentation": "off [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": "39.51",
"vg": "centos"
}
},
"pvs": {
"/dev/sda2": {
"free_g": "0",
"size_g": "39.51",
"vg": "centos"
}
},
"vgs": {
"centos": {
"free_g": "0",
"num_lvs": "1",
"num_pvs": "1",
"size_g": "39.51"
}
}
},
"ansible_machine": "x86_64",
"ansible_machine_id": "b2553b2f1ad64a88a81a775b55de0eb3",
"ansible_memfree_mb": 1754,
"ansible_memory_mb": {
"nocache": {
"free": 1833,
"used": 151
},
"real": {
"free": 1754,
"total": 1984,
"used": 230
},
"swap": {
"cached": 0,
"free": 0,
"total": 0,
"used": 0
}
},
"ansible_memtotal_mb": 1984,
"ansible_mounts": [
{
"block_available": 97092,
"block_size": 4096,
"block_total": 127145,
"block_used": 30053,
"device": "/dev/sda1",
"fstype": "xfs",
"inode_available": 255673,
"inode_total": 256000,
"inode_used": 327,
"mount": "/boot",
"options": "rw,relatime,attr2,inode64,noquota",
"size_available": 397688832,
"size_total": 520785920,
"uuid": "bdfa051f-b5a8-4713-9bee-440b49233372"
},
{
"block_available": 9998628,
"block_size": 4096,
"block_total": 10351679,
"block_used": 353051,
"device": "/dev/mapper/centos-root",
"fstype": "xfs",
"inode_available": 20687486,
"inode_total": 20713472,
"inode_used": 25986,
"mount": "/",
"options": "rw,relatime,attr2,inode64,noquota",
"size_available": 40954380288,
"size_total": 42400477184,
"uuid": "d0261be2-3486-4ff1-8563-8360393cfeb4"
}
],
"ansible_nodename": "xingdian",
"ansible_os_family": "RedHat",
"ansible_pkg_mgr": "yum",
"ansible_proc_cmdline": {
"BOOT_IMAGE": "/vmlinuz-3.10.0-693.el7.x86_64",
"LANG": "en_US.UTF-8",
"quiet": true,
"rd.lvm.lv": "centos/root",
"rhgb": true,
"ro": true,
"root": "/dev/mapper/centos-root"
},
"ansible_processor": [
"0",
"GenuineIntel",
"11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz"
],
"ansible_processor_cores": 1,
"ansible_processor_count": 1,
"ansible_processor_threads_per_core": 1,
"ansible_processor_vcpus": 1,
"ansible_product_name": "VMware Virtual Platform",
"ansible_product_serial": "VMware-56 4d 77 c3 38 eb b5 44-f9 c5 f5 a9 0c 0d 1c 29",
"ansible_product_uuid": "C3774D56-EB38-44B5-F9C5-F5A90C0D1C29",
"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": "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBFWlYnYAyylfiZr2C9xiHsT8i2ao+RY8CY+z6wJEBQxdtg+/VHmxYOejdPKrq94IjLU00x5Iu096aQ5XdNyDUs4=",
"ansible_ssh_host_key_ed25519_public": "AAAAC3NzaC1lZDI1NTE5AAAAIPHn7/QapryKV1IenlFdQngIQed+y9YyziwtVYnBflG9",
"ansible_ssh_host_key_rsa_public": "AAAAB3NzaC1yc2EAAAADAQABAAABAQCso6OpQ9TsQ/2ihnVLtUpqK4y1hjR9WyHqKQFcqH7TN9TDxuyB+gPfN1IzC25IoUb8mp9zIiLNqg8epsNbKFj9l3WQUtx3fcSXqIcZdWeTWkltxz0DxSr0xQd+odYwQ+ppH5gFjvTnJNcVJhqfMLAdQjLFjSrZfvF28qUyTGj8HJC7qGneM9ECLQwQ+ZQ6qvjaXMpgUR4p9JsOdSeicGD3mZwIsvScfhHq011V/2ZCQAgCuUlO/Z6+Xbzc27ZPyu4wkZYWM+DyZEXi0cvO5WYV+tst8uwdBQhSaQi8T5Vu7eUitdoFBC9vTFjZiR39dxEHy2nlkTnJWZ9ChLY08iLf",
"ansible_swapfree_mb": 0,
"ansible_swaptotal_mb": 0,
"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": 3024,
"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
}
```
## 二使用Setup
#### 1.案例一
使用“ansible_fqdn”获取主机名称和域名
```yml
[root@xingdian ansible]# cat test.yml
- name: test a playbook
hosts: web
tasks:
- name: hostname
debug:
msg: "{{ ansible_fqdn }}"
```
运行剧本
```yaml
[root@xingdian ansible]# ansible-playbook test.yml
[WARNING]: Could not match supplied host pattern, ignoring: node1
PLAY [test a playbook] ****************************************************************************************************************************************
skipping: no hosts matched
PLAY RECAP ****************************************************************************************************************************************************
[root@xingdian ansible]# vim test.yml
[root@xingdian ansible]# ansible-playbook test.yml
PLAY [test a playbook] ****************************************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************************************
ok: [10.0.0.111]
TASK [hostname] ***********************************************************************************************************************************************
ok: [10.0.0.111] => {
"msg": "xingdian"
}
PLAY RECAP ****************************************************************************************************************************************************
10.0.0.111 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
```
#### 2.案例二
使用“ansible_ens33.ipv4.address”获取指定网卡IP地址
```yaml
[root@xingdian ansible]# cat test.yml
- name: test a playbook
hosts: web
tasks:
- name: hostname
debug:
msg: "{{ ansible_ens33.ipv4.address }}"
```
运行剧本
```yaml
[root@xingdian ansible]# ansible-playbook test.yml
PLAY [test a playbook] ****************************************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************************************
ok: [10.0.0.111]
TASK [hostname] ***********************************************************************************************************************************************
ok: [10.0.0.111] => {
"msg": "10.0.0.111"
}
PLAY RECAP ****************************************************************************************************************************************************
10.0.0.111 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
```
#### 3.案例三
使用“ansible_devices.sda.partitions.sda2.size”获取磁盘容量
```yaml
[root@xingdian ansible]# cat test.yml
- name: test a playbook
hosts: web
tasks:
- name: hostname
debug:
msg: "{{ ansible_devices.sda.partitions.sda2.size }}"
```
运行剧本
```yaml
[root@xingdian ansible]# ansible-playbook test.yml
PLAY [test a playbook] ****************************************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************************************
ok: [10.0.0.111]
TASK [hostname] ***********************************************************************************************************************************************
ok: [10.0.0.111] => {
"msg": "39.51 GB"
}
PLAY RECAP ****************************************************************************************************************************************************
10.0.0.111 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
```

File diff suppressed because it is too large Load Diff

@ -0,0 +1,172 @@
<h1><center>Yaml语法格式</center></h1>
------
**作者:行癫(盗版必究)**
## 一Yaml介绍
YAML 是 "YAML Ain't a Markup Language"YAML 不是一种标记语言的递归缩写。在开发的这种语言时YAML 的意思其实是:"Yet Another Markup Language"仍是一种标记语言YAML 的语法和其他高级语言类似并且可以简单表达清单、散列表标量等数据形态。它使用空白符号缩进和大量依赖外观的特色特别适合用来表达或编辑数据结构、各种配置文件等YAML 的配置文件后缀为 .ymlrunoob.yml*
#### 1.基本语法
大小写敏感
使用缩进表示层级关系
缩进不允许使用tab只允许空格
缩进的空格数不重要,只要相同层级的元素左对齐即可
'#'表示注释
#### 2.数据类型
对象键值对的集合又称为映射mapping/ 哈希hashes / 字典dictionary
数组一组按次序排列的值又称为序列sequence / 列表list
纯量scalars单个的、不可再分的值
#### 3.Yaml对象
对象键值对使用冒号结构表示 key: value冒号后面要加一个空格
也可以使用 key:{key1: value1, key2: value2, ...}
```yaml
key:
child-key: value
child-key2: value2
```
#### 4.Yaml数组
**-** 开头的行表示构成一个数组
```yaml
- A
- B
- C
```
YAML 支持多维数组,可以使用行内表示
```shell
key: [value1, value2, ...]
```
数据结构的子成员是一个数组,则可以在该项下面缩进一个空格
```shell
-
- A
- B
- C
```
案例companies 属性是一个数组,每一个数组元素又是由 id、name、price 三个属性构成
```yaml
companies:
-
id: 1
name: company1
price: 200W
-
id: 2
name: company2
price: 500W
```
#### 5.复合结构
数组和对象可以构成复合结构
```yaml
languages:
- Ruby
- Perl
- Python
websites:
YAML: yaml.org
Ruby: ruby-lang.org
Python: python.org
Perl: use.perl.org
```
转换为 json 为:
```
{"languages": ["Ruby", "Perl", "Python"], "websites": {"YAML": "yaml.org", "Ruby": "ruby-lang.org", "Python": "python.org", "Perl": "use.perl.org"}}
{
languages: [ 'Ruby', 'Perl', 'Python'],
websites: {
YAML: 'yaml.org',
Ruby: 'ruby-lang.org',
Python: 'python.org',
Perl: 'use.perl.org'
}
}
```
#### 6.纯量
纯量是最基本的,不可再分的值,包括:
字符串
布尔值
整数
浮点数
Null
时间
日期
```yaml
boolean:
- TRUE #true,True都可以
- FALSE #falseFalse都可以
float:
- 3.14
- 6.8523015e+5 #可以使用科学计数法
int:
- 123
- 0b1010_0111_0100_1010_1110 #二进制表示
null:
nodeName: 'node'
parent: ~ #使用~表示null
string:
- 哈哈
- 'Hello world' #可以使用双引号或者单引号包裹特殊字符
- newline
newline2 #字符串可以拆成多行,每一行会被转化成一个空格
date:
- 2018-02-17 #日期必须使用ISO 8601格式即yyyy-MM-dd
datetime:
- 2018-02-17T15:02:31+08:00 #时间使用ISO 8601格式时间和日期之间使用T连接最后使用+代表时区
```
Loading…
Cancel
Save