标签:ansible yum service playbook setup
ansible模块 yum:
[root@node1 ~]# ansible-doc -s yum less 436 Copyright (C) 1984-2009 Mark Nudelman less comes with NO WARRANTY, to the extent permitted by law. For information about the terms of redistribution, see the file named README in the less distribution. Homepage: http://www.greenwoodsoftware.com/less - name: M a n a g e s p a c k a g e s w i t h t h e I ( y u m ) p a c k a g e m a n a g e r action: yum conf_file # The remote yum configuration file to use for the transaction. disable_gpg_check # Whether to disable the GPG checking of signatures of packages being installed. Has an effect only if state is `present‘ or `latest‘. disablerepo # `Repoid‘ of repositories to disable for the install/update operation. These repos will not persist beyond the transaction. When specifyi enablerepo # `Repoid‘ of repositories to enable for the install/update operation. These repos will not persist beyond the transaction. When specifyin list # Various (non-idempotent) commands for usage with `/usr/bin/ansible‘ and `not‘ playbooks. See examples. name= # Package name, or package specifier with version, like `name-1.0‘. When using state=latest, this can be ‘*‘ which means run: yum -y updat state # Whether to install (`present‘, `latest‘), or remove (`absent‘) a package. update_cache # Force updating the cache. Has an effect only if state is `present‘ or `latest‘. [root@node1 ~]#
安装httpd服务
[root@node1 ~]# ansible testservers -m yum -a ‘name=httpd state=present‘ 192.168.100.132 | success >> { "changed": true, "msg": "", "rc": 0, "results": [ "Loaded plugins: fastestmirror, refresh-packagekit, security\nLoading mirror speeds from cached hostfile\n * base: mirrors.btte.net\n * epel: mirrors.hustunique.com\n * extras: mirrors.btte.net\n * updates: mirrors.yun-idc.com\nSetting up Install Process\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.2.15-47.el6.centos.3 will be installed\n--> Processing Dependency: httpd-tools = 2.2.15-47.el6.centos.3 for package: httpd-2.2.15-47.el6.centos.3.x86_64\n--> Running transaction check\n---> Package httpd-tools.x86_64 0:2.2.15-29.el6.centos will be updated\n---> Package httpd-tools.x86_64 0:2.2.15-47.el6.centos.3 will be an update\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n httpd x86_64 2.2.15-47.el6.centos.3 updates 831 k\nUpdating for dependencies:\n httpd-tools x86_64 2.2.15-47.el6.centos.3 updates 77 k\n\nTransaction Summary\n================================================================================\nInstall 1 Package(s)\nUpgrade 1 Package(s)\n\nTotal download size: 908 k\nDownloading Packages:\n--------------------------------------------------------------------------------\nTotal 4.4 MB/s | 908 kB 00:00 \nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r Updating : httpd-tools-2.2.15-47.el6.centos.3.x86_64 1/3 \n\r Installing : httpd-2.2.15-47.el6.centos.3.x86_64 2/3 \n\r Cleanup : httpd-tools-2.2.15-29.el6.centos.x86_64 3/3 \n\r Verifying : httpd-tools-2.2.15-47.el6.centos.3.x86_64 1/3 \n\r Verifying : httpd-2.2.15-47.el6.centos.3.x86_64 2/3 \n\r Verifying : httpd-tools-2.2.15-29.el6.centos.x86_64 3/3 \n\nInstalled:\n httpd.x86_64 0:2.2.15-47.el6.centos.3 \n\nDependency Updated:\n httpd-tools.x86_64 0:2.2.15-47.el6.centos.3 \n\nComplete!\n" ] } 192.168.100.131 | success >> { "changed": true, "msg": "", "rc": 0, "results": [ "Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\n * epel: mirrors.opencas.cn\nSetting up Install Process\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.2.15-29.el6.centos will be installed\n--> Processing Dependency: httpd-tools = 2.2.15-29.el6.centos for package: httpd-2.2.15-29.el6.centos.x86_64\n--> Processing Dependency: apr-util-ldap for package: httpd-2.2.15-29.el6.centos.x86_64\n--> Processing Dependency: /etc/mime.types for package: httpd-2.2.15-29.el6.centos.x86_64\n--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.2.15-29.el6.centos.x86_64\n--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.2.15-29.el6.centos.x86_64\n--> Running transaction check\n---> Package apr.x86_64 0:1.3.9-5.el6_2 will be installed\n---> Package apr-util.x86_64 0:1.3.9-3.el6_0.1 will be installed\n---> Package apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1 will be installed\n---> Package httpd-tools.x86_64 0:2.2.15-29.el6.centos will be installed\n---> Package mailcap.noarch 0:2.1.31-2.el6 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository\n Size\n================================================================================\nInstalling:\n httpd x86_64 2.2.15-29.el6.centos 1 821 k\nInstalling for dependencies:\n apr x86_64 1.3.9-5.el6_2 1 123 k\n apr-util x86_64 1.3.9-3.el6_0.1 1 87 k\n apr-util-ldap x86_64 1.3.9-3.el6_0.1 1 15 k\n httpd-tools x86_64 2.2.15-29.el6.centos 1 73 k\n mailcap noarch 2.1.31-2.el6 1 27 k\n\nTransaction Summary\n================================================================================\nInstall 6 Package(s)\n\nTotal download size: 1.1 M\nInstalled size: 3.6 M\nDownloading Packages:\n--------------------------------------------------------------------------------\nTotal 13 MB/s | 1.1 MB 00:00 \nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r Installing : apr-1.3.9-5.el6_2.x86_64 1/6 \n\r Installing : apr-util-1.3.9-3.el6_0.1.x86_64 2/6 \n\r Installing : apr-util-ldap-1.3.9-3.el6_0.1.x86_64 3/6 \n\r Installing : httpd-tools-2.2.15-29.el6.centos.x86_64 4/6 \n\r Installing : mailcap-2.1.31-2.el6.noarch 5/6 \n\r Installing : httpd-2.2.15-29.el6.centos.x86_64 6/6 \n\r Verifying : httpd-2.2.15-29.el6.centos.x86_64 1/6 \n\r Verifying : apr-util-ldap-1.3.9-3.el6_0.1.x86_64 2/6 \n\r Verifying : httpd-tools-2.2.15-29.el6.centos.x86_64 3/6 \n\r Verifying : apr-1.3.9-5.el6_2.x86_64 4/6 \n\r Verifying : mailcap-2.1.31-2.el6.noarch 5/6 \n\r Verifying : apr-util-1.3.9-3.el6_0.1.x86_64 6/6 \n\nInstalled:\n httpd.x86_64 0:2.2.15-29.el6.centos \n\nDependency Installed:\n apr.x86_64 0:1.3.9-5.el6_2 \n apr-util.x86_64 0:1.3.9-3.el6_0.1 \n apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1 \n httpd-tools.x86_64 0:2.2.15-29.el6.centos \n mailcap.noarch 0:2.1.31-2.el6 \n\nComplete!\n" ] }
name:可以指定版本号,否定为默认最新
卸载 httpd 服务:
[root@node1 ~]# ansible testservers -m yum -a ‘name=httpd state=absent ‘ 192.168.100.131 | success >> { "changed": true, "msg": "", "rc": 0, "results": [ "Loaded plugins: fastestmirror\nSetting up Remove Process\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.2.15-29.el6.centos will be erased\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nRemoving:\n httpd x86_64 2.2.15-29.el6.centos @1 2.9 M\n\nTransaction Summary\n================================================================================\nRemove 1 Package(s)\n\nInstalled size: 2.9 M\nDownloading Packages:\nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r Erasing : httpd-2.2.15-29.el6.centos.x86_64 1/1 \n\r Verifying : httpd-2.2.15-29.el6.centos.x86_64 1/1 \n\nRemoved:\n httpd.x86_64 0:2.2.15-29.el6.centos \n\nComplete!\n" ] } 192.168.100.132 | success >> { "changed": true, "msg": "", "rc": 0, "results": [ "Loaded plugins: fastestmirror, refresh-packagekit, security\nSetting up Remove Process\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.2.15-47.el6.centos.3 will be erased\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nRemoving:\n httpd x86_64 2.2.15-47.el6.centos.3 @updates 2.9 M\n\nTransaction Summary\n================================================================================\nRemove 1 Package(s)\n\nInstalled size: 2.9 M\nDownloading Packages:\nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r Erasing : httpd-2.2.15-47.el6.centos.3.x86_64 1/1 \n\r Verifying : httpd-2.2.15-47.el6.centos.3.x86_64 1/1 \n\nRemoved:\n httpd.x86_64 0:2.2.15-47.el6.centos.3 \n\nComplete!\n" ] } [root@node1 ~]#
模块 service :
[root@node1 ~]# ansible-doc -s service less 436 Copyright (C) 1984-2009 Mark Nudelman less comes with NO WARRANTY, to the extent permitted by law. For information about the terms of redistribution, see the file named README in the less distribution. Homepage: http://www.greenwoodsoftware.com/less - name: M a n a g e s e r v i c e s . action: service arguments # Additional arguments provided on the command line enabled # Whether the service should start on boot. *At least one of state and enabled are required.* name= # Name of the service. pattern # If the service does not respond to the status command, name a substring to look for as would be found in the output of the `ps‘ command runlevel # For OpenRC init scripts (ex: Gentoo) only. The runlevel that this service belongs to. sleep # If the service is being `restarted‘ then sleep this many seconds between the stop and start command. This helps to workaround badly beha state # `started‘/`stopped‘ are idempotent actions that will not run commands unless necessary. `restarted‘ will always bounce the service. `r [root@node1 ~]#
启动 httpd 服务,并设置开机启动
[root@node1 ~]# ansible testservers -m yum -a ‘name=httpd state=present‘ [root@node1 ~]# ansible testservers -m command -a ‘service httpd status‘ 192.168.100.131 | FAILED | rc=3 >> httpd 已停 192.168.100.132 | FAILED | rc=3 >> httpd 已停 [root@node1 ~]# ansible testservers -m command -a ‘chkconfig --list httpd‘ 192.168.100.131 | success | rc=0 >> httpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off 192.168.100.132 | success | rc=0 >> httpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off [root@node1 ~]# ansible testservers -m service -a ‘name=httpd enabled=true state=started‘ 192.168.100.131 | success >> { "changed": true, "enabled": true, "name": "httpd", "state": "started" } 192.168.100.132 | success >> { "changed": true, "enabled": true, "name": "httpd", "state": "started" } [root@node1 ~]# ansible testservers -m command -a ‘service httpd status‘ 192.168.100.131 | success | rc=0 >> httpd (pid 3270) 正在运行... 192.168.100.132 | success | rc=0 >> httpd (pid 33341) 正在运行... [root@node1 ~]# ansible testservers -m command -a ‘chkconfig --list httpd‘ 192.168.100.131 | success | rc=0 >> httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off 192.168.100.132 | success | rc=0 >> httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off [root@node1 ~]#
模块 setup : 收集远程主机的信息
每个被管理的主机,在执行命令操作前都会执行setup模块,将信息汇报到ansible server 端
[root@node1 ~]# ansible-doc -s setup less 436 Copyright (C) 1984-2009 Mark Nudelman less comes with NO WARRANTY, to the extent permitted by law. For information about the terms of redistribution, see the file named README in the less distribution. Homepage: http://www.greenwoodsoftware.com/less - name: G a t h e r s f a c t s a b o u t r e m o t e h o s t s action: setup fact_path # path used for local ansible facts (*.fact) - files in this dir will be run (if executable) and their results be added to ansible_local f filter # if supplied, only return facts that match this shell-style (fnmatch) wildcard. [root@node1 ~]#
以 testservers 组中的192.168.100.132 主机为例:
192.168.100.132 | success >> { "ansible_facts": { "ansible_all_ipv4_addresses": [ "192.168.100.132" ], "ansible_all_ipv6_addresses": [ "fe80::20c:29ff:fe42:3d4b" ], "ansible_architecture": "x86_64", "ansible_bios_date": "07/31/2013", "ansible_bios_version": "6.00", "ansible_cmdline": { "KEYBOARDTYPE": "pc", "KEYTABLE": "us", "LANG": "zh_CN.UTF-8", "nomodeset": true, "quiet": true, "rd_LVM_LV": "vg_v3/lv_swap", "rd_NO_DM": true, "rd_NO_LUKS": true, "rd_NO_MD": true, "rhgb": true, "ro": true, "root": "/dev/mapper/vg_v3-lv_root" }, "ansible_date_time": { "date": "2016-02-29", "day": "29", "epoch": "1456726561", "hour": "14", "iso8601": "2016-02-29T06:16:01Z", "iso8601_micro": "2016-02-29T06:16:01.657953Z", "minute": "16", "month": "02", "second": "01", "time": "14:16:01", "tz": "CST", "tz_offset": "+0800", "weekday": "Monday", "year": "2016" }, "ansible_default_ipv4": { "address": "192.168.100.132", "alias": "eth0", "gateway": "192.168.100.2", "interface": "eth0", "macaddress": "00:0c:29:42:3d:4b", "mtu": 1500, "netmask": "255.255.255.0", "network": "192.168.100.0", "type": "ether" }, "ansible_default_ipv6": {}, "ansible_devices": { "sda": { "holders": [], "host": "SCSI storage controller: LSI Logic / Symbios Logic 53c1030 PCI-X Fusion-MPT Dual Ultra320 SCSI (rev 01)", "model": "VMware Virtual S", "partitions": { "sda1": { "sectors": "1024000", "sectorsize": 512, "size": "500.00 MB", "start": "2048" }, "sda2": { "sectors": "40916992", "sectorsize": 512, "size": "19.51 GB", "start": "1026048" } }, "removable": "0", "rotational": "1", "scheduler_mode": "cfq", "sectors": "41943040", "sectorsize": "512", "size": "20.00 GB", "support_discard": "0", "vendor": "VMware," }, "sr0": { "holders": [], "host": "SATA controller: VMware SATA AHCI controller", "model": "VMware SATA CD01", "partitions": {}, "removable": "1", "rotational": "1", "scheduler_mode": "cfq", "sectors": "2097151", "sectorsize": "512", "size": "1024.00 MB", "support_discard": "0", "vendor": "NECVMWar" } }, "ansible_distribution": "CentOS", "ansible_distribution_major_version": "6", "ansible_distribution_release": "Final", "ansible_distribution_version": "6.5", "ansible_domain": "bta.net.cn", "ansible_env": { "CVS_RSH": "ssh", "G_BROKEN_FILENAMES": "1", "HOME": "/root", "LANG": "C", "LC_CTYPE": "C", "LESSOPEN": "|/usr/bin/lesspipe.sh %s", "LOGNAME": "root", "MAIL": "/var/mail/root", "PATH": "/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin", "PWD": "/root", "QTDIR": "/usr/lib64/qt-3.3", "QTINC": "/usr/lib64/qt-3.3/include", "QTLIB": "/usr/lib64/qt-3.3/lib", "SHELL": "/bin/bash", "SHLVL": "2", "SSH_ASKPASS": "/usr/libexec/openssh/gnome-ssh-askpass", "SSH_CLIENT": "192.168.100.129 46085 22", "SSH_CONNECTION": "192.168.100.129 46085 192.168.100.132 22", "SSH_TTY": "/dev/pts/0", "TERM": "vt100", "USER": "root", "_": "/usr/bin/python" }, "ansible_eth0": { "active": true, "device": "eth0", "ipv4": { "address": "192.168.100.132", "netmask": "255.255.255.0", "network": "192.168.100.0" }, "ipv6": [ { "address": "fe80::20c:29ff:fe42:3d4b", "prefix": "64", "scope": "link" } ], "macaddress": "00:0c:29:42:3d:4b", "module": "e1000", "mtu": 1500, "promisc": false, "type": "ether" }, "ansible_fips": false, "ansible_form_factor": "Other", "ansible_fqdn": "bt-199-037.bta.net.cn", "ansible_hostname": "v3", "ansible_interfaces": [ "lo", "pan0", "eth0" ], "ansible_kernel": "2.6.32-431.el6.x86_64", "ansible_lo": { "active": true, "device": "lo", "ipv4": { "address": "127.0.0.1", "netmask": "255.0.0.0", "network": "127.0.0.0" }, "ipv6": [ { "address": "::1", "prefix": "128", "scope": "host" } ], "mtu": 16436, "promisc": false, "type": "loopback" }, "ansible_lsb": { "codename": "Final", "description": "CentOS release 6.5 (Final)", "id": "CentOS", "major_release": "6", "release": "6.5" }, "ansible_machine": "x86_64", "ansible_machine_id": "7fa908076135396c09ba795100000011", "ansible_memfree_mb": 217, "ansible_memory_mb": { "nocache": { "free": 640, "used": 348 }, "real": { "free": 217, "total": 988, "used": 771 }, "swap": { "cached": 0, "free": 1981, "total": 1983, "used": 2 } }, "ansible_memtotal_mb": 988, "ansible_mounts": [ { "device": "/dev/mapper/vg_v3-lv_root", "fstype": "ext4", "mount": "/", "options": "rw", "size_available": 13751066624, "size_total": 18569568256, "uuid": "5b1e1ab5-6a12-4466-a832-0fa80ec05610" }, { "device": "/dev/sda1", "fstype": "ext4", "mount": "/boot", "options": "rw", "size_available": 445854720, "size_total": 507744256, "uuid": "01b79867-8bb5-4492-bc99-ee4c1e77ad45" } ], "ansible_nodename": "v3.lansgg.com", "ansible_os_family": "RedHat", "ansible_pan0": { "active": false, "device": "pan0", "id": "8000.000000000000", "interfaces": [], "macaddress": "82:5d:e7:15:7f:60", "mtu": 1500, "promisc": false, "stp": false, "type": "bridge" }, "ansible_pkg_mgr": "yum", "ansible_processor": [ "GenuineIntel", "Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz" ], "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 4b fa 62 36 82 17-59 18 88 5d 7e 42 3d 4b", "ansible_product_uuid": "564D4BFA-6236-8217-5918-885D7E423D4B", "ansible_product_version": "None", "ansible_python_version": "2.6.6", "ansible_selinux": { "status": "disabled" }, "ansible_ssh_host_key_dsa_public": "AAAAB3NzaC1kc3MAAACBAJvg4oIca9Wzg59BbkQv4fP2b0e/e6Er5AwKb7fe2NC5pIBYDZFum68IJcIwrjb1WalwMXJtRl6OxPq3dO+D7RDitATv8z8N4eviAbOwg8zlSYcadazYcElSBYaWfvuSH+PoNuLc3KwL6S45vNnXjJRA40QALWYPCKNBn/g1Lqc/AAAAFQD9+juFm6jft2mUEB8+H7wu+IrtMQAAAIEAi2RsOt4Svr8G4nLgZhzMeegq6zNZm2IPDDjLIK6Hk0dMqLGjNsJ9muUX9NRnRj4UVmon4LuMU6Z/fXag5UhbUUHRftLf9tplGR5CX0MtQplDvBHiB+KXu9l1hzaFgRW+oZMVsaFTEox+4lQBLSjVjX/3vALpnGYdkheN6nU4FEMAAACANQMb+9zNptx3s1ohb9mFJmG9MKVP8IXY3VNZqCSHVlmIp3p1kepNH6sKZDyo22SFO0l0rXP9hADxwlhvhxOyupnDmpsM1Ar5vFxusgZgGisPzOtljQbzeuXlCo+SUswEoxS4NY/CSP8Gc8eZD+JAcS+pDuBxQckC8JIDfOstlS4=", "ansible_ssh_host_key_rsa_public": "AAAAB3NzaC1yc2EAAAABIwAAAQEAzM8GNFPLJTd+3PYOlj5wcJMYgZNIdN7ozvQVZn8O7CpDnBXscGBezQ1m2QJsrzeTWR2FOKrAqv3oWeFwW65cxcSgVgdzVHatR6KhVqqJyF0+39sRAAAxAwK3vXK2IdKLLIIWShvIH+F0uL9aNP0UQt0gJLsqVI6yJ1zAB3qexPX+bZNRa3K33tkeAWfJdlJUScgaUe3oUnvAdXMBuTYp9fJwZyMo3HibUuE2YbeFAXTj6qnRyKOgwponPkkv9TuM8yenNIKqbjxIALgNyERMx0FDHc22Ow2AEfWASE+KT8ZrdbjKl+pMUz64MAHpaNgWzrivuSFRUzjrECwFolk47w==", "ansible_swapfree_mb": 1981, "ansible_swaptotal_mb": 1983, "ansible_system": "Linux", "ansible_system_vendor": "VMware, Inc.", "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", "module_setup": true }, "changed": false } [root@node1 ~]#
一般在 playbook 中使用较多,如template模块文件中。
本文出自 “大風” 博客,请务必保留此出处http://lansgg.blog.51cto.com/5675165/1746054
标签:ansible yum service playbook setup
原文地址:http://lansgg.blog.51cto.com/5675165/1746054