A, setup module
Mainly used to obtain host information, often to a gether_facts parameters that will be used in connection with any module playbooks inside. a setup module parameters are frequently used filter parameter. Each managed node before receiving the command and operation management, will host their own information (such as system version, host ip address) inform ansible management host --tree: save the output information for all hosts to the specified directory to / etc / ansible / hosts file name in the host name ansible All Setup -a -m 'filter = ansible_default_ipv4' --tree / tmp /
filter: filtering keywords:
[root @ localhost ansible] # ansible 127.0.0.1 -m Setup -a '= filter ansible_default_ipv4'
gather_subset: collecting information by a subset, values are all, main, bardware, network, virtual, ohai, facter, does not include the use! Number, such as,! Network
filter parameters:
ansible_nodename #节点名字 [root@ansible ~]# ansible test -m setup -a 'filter=ansible_nodename' 192.168.100.120 | SUCCESS => { "ansible_facts": { "ansible_nodename": "ansible" }, "changed": false }
ansible_fqdb #fqdn名
[root@ansible ~]# ansible test -m setup -a 'filter=ansible_fqdn'
192.168.100.120 | SUCCESS => {
"ansible_facts": {
"ansible_fqdn": "ansible"
},
"changed": false
}
ansible_hostname # host short name
[root@ansible ~]# ansible test -m setup -a 'filter=ansible_hostname'
192.168.100.120 | SUCCESS => {
"ansible_facts": {
"ansible_hostname": "ansible"
},
"changed": false
}
ansible_domain # host domain name suffixes
[root@ansible ~]# ansible test -m setup -a 'filter=ansible_domain'
192.168.100.120 | SUCCESS => {
"ansible_facts": {
"ansible_domain": ""
},
"changed": false
}
ansible_memtotal_mb # Total Physical Memory
[root@ansible ~]# ansible test -m setup -a 'filter=ansible_memtotal_mb'
192.168.100.120 | SUCCESS => {
"ansible_facts": {
"ansible_memtotal_mb": 976
},
"changed": false
}
ansible_swaptotal_mb #swap total size
[root@ansible ~]# ansible test -m setup -a 'filter=ansible_swaptotal_mb'
192.168.100.120 | SUCCESS => {
"ansible_facts": {
"ansible_swaptotal_mb": 2047
},
"changed": false
}
ansible_processor #cpu information
[root@ansible ~]# ansible test -m setup -a 'filter=ansible_processor'
192.168.100.120 | SUCCESS => {
"ansible_facts": {
"ansible_processor": [
"0",
"GenuineIntel",
"Intel(R) Core(TM) i7-3612QM CPU @ 2.10GHz"
]
},
"changed": false
}
ansible_process_cores #cpu number of cores
[root@ansible ~]# ansible test -m setup -a 'filter=ansible_processor_cores'
192.168.100.120 | SUCCESS => {
"ansible_facts": {
"ansible_processor_cores": 1
},
"changed": false
}
ansible_processor_vcpus #cpu logic cores
[root@ansible ~]# ansible test -m setup -a 'filter=ansible_processor_vcpus'
192.168.100.120 | SUCCESS => {
"ansible_facts": {
"ansible_processor_vcpus": 1
},
"changed": false
}
All ansible_all_ipv4_addresses # ipv4 address
[root@ansible ~]# ansible test -m setup -a 'filter=ansible_all_ipv4_addresses'
192.168.100.120 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"192.168.100.120"
]
},
"changed": false
}
ansible_all_ipv6_addresses # ipv6 address all
[root@ansible ~]# ansible test -m setup -a 'filter=ansible_all_ipv6_addresses'
192.168.100.120 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv6_addresses": [
"fe80::e010:cb1a:204a:861d"
]
},
"changed": false
}
NIC ansible_default_ipv4 # default gateway configuration information
[root@ansible ~]# ansible test -m setup -a 'filter=ansible_default_ipv4'
192.168.100.120 | SUCCESS => {
"ansible_facts": {
"ansible_default_ipv4": {
"address": "192.168.100.120",
"alias": "ens33",
"broadcast": "192.168.100.255",
"gateway": "192.168.100.2",
"interface": "ens33",
"macaddress": "00:0c:29:73:fd:2e",
"mtu": 1500,
"netmask": "255.255.255.0",
"network": "192.168.100.0",
"type": "ether"
}
},
"changed": false
}
ansible_ens33 # NICs certain specific information
[root@ansible ~]# ansible test -m setup -a 'filter=ansible_ens33'
192.168.100.120 | SUCCESS => {
"ansible_facts": {
"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": "192.168.100.120",
"broadcast": "192.168.100.255",
"netmask": "255.255.255.0",
"network": "192.168.100.0"
},
"ipv6": [
{
"address": "fe80::e010:cb1a:204a:861d",
"prefix": "64",
"scope": "link"
}
],
"macaddress": "00:0c:29:73:fd:2e",
"module": "e1000",
"mtu": 1500,
"pciid": "0000:02:01.0",
"promisc": false,
"speed": 1000,
"timestamping": [
"tx_software",
"rx_software",
"software"
],
"type": "ether"
}
},
"changed": false
}
ansible_dns # dns card information
[root@ansible ~]# ansible test -m setup -a 'filter=ansible_dns'
192.168.100.120 | SUCCESS => {
"ansible_facts": {
"ansible_dns": {
"nameservers": [
"192.168.100.2",
"114.114.114.114"
]
}
},
"changed": false
}
ansible_architecture # System Architecture
x86_64
ansible_machine # host type
x86_64
ansible_kernel # kernel version
2.6.32-696.el6.x86_64
ansible_distribution # Release
CentOS
ansible_distribution_release # Release Name
final
ansible_distribution_major_version # OS major version number
6
ansible_distribution_release # Release Name
Final
ansible_distribution_version # full version number
7.4.1708
ansible_pkg_mgr # Package management
yum
ansible_service-mgr # perform service mode
systemd
family of ansible_os_family #
RedHat
ansible_cmdline # kernel boot parameters
[root@ansible ~]# ansible test -m setup -a 'filter=ansible_cmdline'
192.168.100.120 | SUCCESS => {
"ansible_facts": {
"ansible_cmdline": {
"BOOT_IMAGE": "/vmlinuz-3.10.0-693.el7.x86_64",
"LANG": "zh_CN.UTF-8",
"crashkernel": "auto",
"quiet": true,
"rd.lvm.lv": "centos/swap",
"rhgb": true,
"ro": true,
"root": "/dev/mapper/centos-root"
}
},
"changed": false
}
ansible_selinux #SElinux状态
disbled
ansible_env # current environment variable parameter
Time-related ansible_data_time #
ansible_python_version #python version
2.7.5
ansible_lvm #lvm information about a volume
All mount points ansible_mounts #
[root@ansible ~]# ansible test -m setup -a 'filter=ansible_mounts'
192.168.100.120 | SUCCESS => {
"ansible_facts": {
"ansible_mounts": [
{
"block_available": 4130338,
"block_size": 4096,
"block_total": 4452864,
"block_used": 322526,
"device": "/dev/mapper/centos-root",
"fstype": "xfs",
"inode_available": 8863332,
"inode_total": 8910848,
"inode_used": 47516,
"mount": "/",
"options": "rw,relatime,attr2,inode64,noquota",
"size_available": 16917864448,
"size_total": 18238930944,
"uuid": "282c72d5-8cf6-4ac2-9e6a-64d19ebe2998"
},
{
"block_available": 227195,
"block_size": 4096,
"block_total": 259584,
"block_used": 32389,
"device": "/dev/sda1",
"fstype": "xfs",
"inode_available": 523961,
"inode_total": 524288,
"inode_used": 327,
"mount": "/boot",
"options": "rw,relatime,attr2,inode64,noquota",
"size_available": 930590720,
"size_total": 1063256064,
"uuid": "700f5755-18e9-45b5-ab39-9c699eb0f636"
}
]
},
"changed": false
}
ansible_device_links # uuid all hanging in the equipment and label name
[root@ansible ~]# ansible test -m setup -a 'filter=ansible_device_links'
192.168.100.120 | SUCCESS => {
"ansible_facts": {
"ansible_device_links": {
"ids": {
"dm-0": [
"dm-name-centos-root",
"dm-uuid-LVM-rdlock3VAHcB3dNwwyYLym2uGO9gVYLylUjawiCM9Ei01XfqxeDCwuFZGm5GKTN4"
],
"dm-1": [
"dm-name-centos-swap",
"dm-uuid-LVM-rdlock3VAHcB3dNwwyYLym2uGO9gVYLye5ByMZPoe3dex42T2VCoRo0C2Rd4riqf"
],
"sda2": [
"lvm-pv-uuid-f3IVfS-XHtK-6UjN-ZyOj-s1GO-1NdX-ZIh8UN"
]
},
"labels": {},
"masters": {
"sda2": [
"dm-0",
"dm-1"
]
},
"uuids": {
"dm-0": [
"282c72d5-8cf6-4ac2-9e6a-64d19ebe2998"
],
"dm-1": [
"c351d02a-6ffa-41b1-b108-9129dbcac1a2"
],
"sda1": [
"700f5755-18e9-45b5-ab39-9c699eb0f636"
]
}
}
},
"changed": false
}
Device information is being used by all ansible_devices # / dev / under
[root@ansible ~]# ansible test -m setup -a 'filter=ansible_devices'
192.168.100.120 | SUCCESS => {
"ansible_facts": {
"ansible_devices": {
"dm-0": {
"holders": [],
"host": "",
"links": {
"ids": [
"dm-name-centos-root",
"dm-uuid-LVM-rdlock3VAHcB3dNwwyYLym2uGO9gVYLylUjawiCM9Ei01XfqxeDCwuFZGm5GKTN4"
],
"labels": [],
"masters": [],
"uuids": [
"282c72d5-8cf6-4ac2-9e6a-64d19ebe2998"
]
},
"model": null,
"partitions": {},
"removable": "0",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "",
"sectors": "35643392",
"sectorsize": "512",
"size": "17.00 GB",
"support_discard": "0",
"vendor": null,
"virtual": 1
},
"dm-1": {
"holders": [],
"host": "",
"links": {
"ids": [
"dm-name-centos-swap",
"dm-uuid-LVM-rdlock3VAHcB3dNwwyYLym2uGO9gVYLye5ByMZPoe3dex42T2VCoRo0C2Rd4riqf"
],
"labels": [],
"masters": [],
"uuids": [
"c351d02a-6ffa-41b1-b108-9129dbcac1a2"
]
},
"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: LSI Logic / Symbios Logic 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": [
"700f5755-18e9-45b5-ab39-9c699eb0f636"
]
},
"sectors": "2097152",
"sectorsize": 512,
"size": "1.00 GB",
"start": "2048",
"uuid": "700f5755-18e9-45b5-ab39-9c699eb0f636"
},
"sda2": {
"holders": [
"centos-root",
"centos-swap"
],
"links": {
"ids": [
"lvm-pv-uuid-f3IVfS-XHtK-6UjN-ZyOj-s1GO-1NdX-ZIh8UN"
],
"labels": [],
"masters": [
"dm-0",
"dm-1"
],
"uuids": []
},
"sectors": "39843840",
"sectorsize": 512,
"size": "19.00 GB",
"start": "2099200",
"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
}
}
},
"changed": false
}
ansible_user_dir # implementation of the user's home directory
root
ansible_user_gecos # execution of the user description information
the root
ansible_user_gid # execution of the user's gid
0
ansible_user_id # execution user name
root
ansible_user_shell # execution of the user's shell type
/bin/bash
ansible_user_uid # implementation of the uid
0
Two, command module
command command module, the default module, for (can not use variables) remote command execution
Parameters: Creates: a file name, the file exists, the command does not execute free_from: linux instruction to be executed chdir: before executing the command, switch to the specified directory removes: a file name when the file does not exist, this option does not perform executable: switch shell to execute instructions, the execution path must be an absolute path
Three, cron timer task module
1, backup: do a backup before the original scheduled tasks to modify content on the remote host to
2, cron_file: If you specify this option. The replacement plan under the user's task cron.d directory on the remote host using the file
3, day: day (1-31, *, *, 2, ....)
4, hours: hours (0-23, *, *, 2, ....)
5, minute: minutes
6, month: May
7, weekday: Week
8, job: tasks to be performed, dependent on state = present
9, name: the task description
10, special_time: specify when executed, parameters: reboot, yearly, annually, monthly, weekly
11, state: confirmation that the mission plan is to create or delete
12, user: what user to perform
[@ ansible the root tmp] Test # ansible the cron -m -a '= minute "* / 10" Job = "/ bin / DATE" name = "Test Job the cron" State = "Present"' 192.168.100.120 | = SUCCESS> { "changed": to true, "Envs": [], "Jobs": [ "Test Job the cron" ] }
Note:
1, the timing provided to write the specified value, is not provided may not write (default * )
2, name must be written
3, state there are two states: present (Add (default)) or absent (remove)
[root@ansible tmp]# ansible test -a 'crontab -l'
192.168.100.120 | SUCCESS | rc=0 >>
#Ansible: test cron job
*/10 * * * * /bin/date
####### deleting scheduled tasks
[root@ansible tmp]# ansible test -m cron -a 'minute="*/10" job="/bin/date" name="test cron job" state="absent"'
192.168.100.120 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": []
}
##### Query Scheduled Tasks
[root@ansible tmp]# ansible test -a "crontab -l"
192.168.100.120 | SUCCESS | rc=0 >>
Four, user / group module
a user request module is useradd, userdel, usermod, group requests is groupadd, groupdel, groupdel
Parameter Description:
groups: the specified user is a group uid: uid designated with the password: Set a password for the user login, this password is the inscription password, enter the password is encrypted update_password: Always / on_create Always: at the same time be updated only if the password is not in password (default) on_create: new user password only for the name: Specifies the user name createhome: whether to create a home directory yes | no (default is yes) system: Does the system user remove: when state = absent when, remove = yes indicates along with the home directory is removed together, equivalent to the userdel -r (default is NO) State: to create or delete present (add default values) or absent (remove) shell: Specifies the user's shell environment the append: yes / NO yes: increase Add the amount of group NO: full amount of change group, only the groups specified group setting group (default) the expires: set the user's expiration time, the value is a timestamp
Note: When you specify the password parameter, you can not use the back of this genetic code will be sent directly to the managed host's / etc / shadow file, it first needs to be encrypted password string. Then the resulting string can be put in the password
example:
- the User: name = johnd the Comment = "John Doe" uid = 1040 Group = ADMIN - the User: name = james shell = / bin / bash Groups = Admins, Developers the append = yes - the User: name = johnd State = Absent-the Remove = yes - User: name = james18 the shell = / bin / zsh Groups Developers Expires = 1422403387 = # key is generated, only generates a public key and the key file, and direct command using ssh-keygen same effect, does not generate authorized_keys file . - user: name = test generate_ssh_key = yes ssh_key_bits = 2048 ssh_key_file = .ssh / id_rsa
Generates an encrypted password:
1, mounted python-pip, install the encryption library -passlib
yum -y install python-pip pip install --upgrade pip pip install passlib
2, using an encryption library, acquires cipher text password
# python -c "from passlib.hash import sha512_crypt; import getpass; print(sha512_crypt.using(rounds=5000).hash(getpass.getpass()))" Password: $6$0lwTSmqKOkL.ktgl$OnBexXC7haBf0FRHVMIZM2edDeFWBbpKJ2r9cxVwNvY.vh3IIUzwFz8n7jFglc0CrtQSY12ziDonVL6e71Og2.
3, create a user to specify user password
ansible test -m user -a 'name="testops" password="$6$0lwTSmqKOkL.ktgl$OnBexXC7haBf0FRHVMIZM2edDeFWBbpKJ2r9cxVwNvY.vh3IIUzwFz8n7jFglc0CrtQSY12ziDonVL6e71Og2."'
4, the user deletes:
[root@ansible ~]# ansible test -m user -a 'name="test" state="absent" remove="yes"' 192.168.100.120 | SUCCESS => { "changed": true, "force": false, "name": "test", "remove": true, "state": "absent" }
Five, copy module
Copy files
Parameter Description: . 1, Backup: the source file, overwriting the previous backup, the backup file contains time information, there are two options: Yes | NO 2, Content: used in place of "src", the value may be set directly specified file 3, desc: mandatory copy, to the source file to the absolute path to the remote host, if the source file is a directory, then the path must be a directory 4, directory_mode: recursive directory permissions, the default is the system default permissions 5 , force: if the target host that contains the file, but with different contents, if set to yes, mandatory coverage, if no, then only if the target location of the file does not exist, just copy the default is yes
6, Others: All file module where options can be used here
7, src: to copy a file to a remote host address in local or absolute path, or a relative path. If the path is a directory, it will recursively copy, in which case, if the path using the "/" to the end, only the contents of the directory replication, if no "/" to the end, including the directory containing the entire copy all content, NVC rsync