Install Ansible on RHEL6(64bit)_YUM
当前系统版本:
1 2 3 |
[root@cobbler-server software]# cat /etc/redhat-release Red Hat Enterprise Linux Server release 6.6 (Santiago) [root@cobbler-server software]# |
修改主机名与hosts表:
1 2 3 4 5 6 7 8 9 |
[root@cobbler-server ansible]# cat /etc/hosts 127.0.0.1 localhost 192.168.232.131 ansible-server [root@cobbler-server ansible]# [root@cobbler-server ansible]# cat /etc/sysconfig/network NETWORKING=yes HOSTNAME=ansible-server GATEWAY=192.168.232.2 [root@cobbler-server ansible]# |
PyYAML:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
[root@cobbler-server software]# yum install -y libyaml Loaded plugins: product-id, refresh-packagekit, security, subscription-manager This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register. Setting up Install Process Resolving Dependencies --> Running transaction check ---> Package libyaml.x86_64 0:0.1.3-1.4.el6 will be installed --> Finished Dependency Resolution Dependencies Resolved ======================================================================================== Package Arch Version Repository Size ======================================================================================== Installing: libyaml x86_64 0.1.3-1.4.el6 ISO 52 k Transaction Summary ======================================================================================== Install 1 Package(s) Total download size: 52 k Installed size: 127 k Downloading Packages: libyaml-0.1.3-1.4.el6.x86_64.rpm | 52 kB 00:00 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Warning: RPMDB altered outside of yum. Installing : libyaml-0.1.3-1.4.el6.x86_64 1/1 HighAvailability/productid | 1.6 kB 00:00 ISO/productid | 1.6 kB 00:00 LoadBalancer/productid | 1.6 kB 00:00 ResilientStorage/productid | 1.6 kB 00:00 ScalableFileSystem/productid | 1.6 kB 00:00 Server/productid | 1.6 kB 00:00 Verifying : libyaml-0.1.3-1.4.el6.x86_64 1/1 Installed: libyaml.x86_64 0:0.1.3-1.4.el6 Complete! [root@cobbler-server software]# [root@cobbler-server software]# rpm -ivh ftp://ftp.icm.edu.pl/vol/rzm5/linux-oracle-repo/OracleLinux/OL6/openstack10/x86_64/getPackage/PyYAML-3.10-3.el6.x86_64.rpm Retrieving ftp://ftp.icm.edu.pl/vol/rzm5/linux-oracle-repo/OracleLinux/OL6/openstack10/x86_64/getPackage/PyYAML-3.10-3.el6.x86_64.rpm warning: /var/tmp/rpm-tmp.yoF89s: Header V3 RSA/SHA256 Signature, key ID ec551f03: NOKEY Preparing... ########################################### [100%] 1:PyYAML ########################################### [100%] [root@cobbler-server software]# |
python-jinja2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
[root@cobbler-server software]# yum list | grep --color python-babel python-babel.noarch 0.9.4-5.1.el6 ISO python-babel-BabelGladeExtractor.noarch 0.2-0.2.r288.el6 epel [root@cobbler-server software]# [root@cobbler-server software]# yum install -y python-babel* Loaded plugins: product-id, refresh-packagekit, security, subscription-manager This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register. Setting up Install Process Resolving Dependencies --> Running transaction check ---> Package python-babel.noarch 0:0.9.4-5.1.el6 will be installed ---> Package python-babel-BabelGladeExtractor.noarch 0:0.2-0.2.r288.el6 will be installed --> Finished Dependency Resolution Dependencies Resolved ======================================================================================== Package Arch Version Repository Size ======================================================================================== Installing: python-babel noarch 0.9.4-5.1.el6 ISO 1.4 M python-babel-BabelGladeExtractor noarch 0.2-0.2.r288.el6 epel 9.9 k Transaction Summary ======================================================================================== Install 2 Package(s) Total download size: 1.4 M Installed size: 5.2 M Downloading Packages: (1/2): python-babel-0.9.4-5.1.el6.noarch.rpm | 1.4 MB 00:00 (2/2): python-babel-BabelGladeExtractor-0.2-0.2.r288.el6.noarch. | 9.9 kB 00:00 ---------------------------------------------------------------------------------------- Total 1.7 MB/s | 1.4 MB 00:00 warning: rpmts_HdrFromFdno: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6 Importing GPG key 0x0608B895: Userid : EPEL (6) <epel@fedoraproject.org> Package: epel-release-6-8.noarch (installed) From : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Warning: RPMDB altered outside of yum. Installing : python-babel-0.9.4-5.1.el6.noarch 1/2 Installing : python-babel-BabelGladeExtractor-0.2-0.2.r288.el6.noarch 2/2 Verifying : python-babel-0.9.4-5.1.el6.noarch 1/2 Verifying : python-babel-BabelGladeExtractor-0.2-0.2.r288.el6.noarch 2/2 Installed: python-babel.noarch 0:0.9.4-5.1.el6 python-babel-BabelGladeExtractor.noarch 0:0.2-0.2.r288.el6 Complete! [root@cobbler-server software]# [root@cobbler-server software]# rpm -ivh ftp://ftp.pbone.net/mirror/dag.wieers.com/redhat/el6/en/x86_64/dag/RPMS/python-jinja2-2.2.1-1.el6.rf.x86_64.rpm Retrieving ftp://ftp.pbone.net/mirror/dag.wieers.com/redhat/el6/en/x86_64/dag/RPMS/python-jinja2-2.2.1-1.el6.rf.x86_64.rpm warning: /var/tmp/rpm-tmp.HTCK2E: Header V3 DSA/SHA1 Signature, key ID 6b8d79e6: NOKEY Preparing... ########################################### [100%] 1:python-jinja2 ########################################### [100%] [root@cobbler-server software]# |
EPEL for RHEL6:
1 2 3 4 5 6 |
[root@cobbler-server software]# rpm -ivh http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm Retrieving http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm warning: /var/tmp/rpm-tmp.O49dGh: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY Preparing... ########################################### [100%] 1:epel-release ########################################### [100%] [root@cobbler-server software]# |
刷新YUM库:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
[root@cobbler-server software]# yum repolist Loaded plugins: product-id, refresh-packagekit, security, subscription-manager This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register. epel/metalink | 4.6 kB 00:00 epel | 4.3 kB 00:00 epel/primary_db | 5.8 MB 00:00 repo id repo name status HighAvailability HighAvailability 55 ISO ISO 3,785 LoadBalancer LoadBalancer 4 ResilientStorage ResilientStorage 62 ScalableFileSystem ScalableFileSystem 7 Server Server 3,785 epel Extra Packages for Enterprise Linux 6 - x86_64 12,056 repolist: 19,754 [root@cobbler-server software]# |
以YUM的方式安装Ansible:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
[root@cobbler-server software]# yum list | grep --color ansible ansible.noarch 1.9.4-1.el6 epel ansible-inventory-grapher.noarch 1.0.1-2.el6 epel ansible-lint.noarch 2.0.1-1.el6 epel [root@cobbler-server software]# [root@cobbler-server software]# yum install ansible Loaded plugins: product-id, refresh-packagekit, security, subscription-manager This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register. Setting up Install Process Resolving Dependencies --> Running transaction check ---> Package ansible.noarch 0:1.9.4-1.el6 will be installed --> Processing Dependency: sshpass for package: ansible-1.9.4-1.el6.noarch --> Processing Dependency: python-simplejson for package: ansible-1.9.4-1.el6.noarch --> Processing Dependency: python-setuptools for package: ansible-1.9.4-1.el6.noarch --> Processing Dependency: python-keyczar for package: ansible-1.9.4-1.el6.noarch --> Processing Dependency: python-httplib2 for package: ansible-1.9.4-1.el6.noarch --> Processing Dependency: python-crypto2.6 for package: ansible-1.9.4-1.el6.noarch --> Running transaction check ---> Package python-crypto2.6.x86_64 0:2.6.1-2.el6 will be installed ---> Package python-httplib2.noarch 0:0.7.7-1.el6 will be installed ---> Package python-keyczar.noarch 0:0.71c-1.el6 will be installed --> Processing Dependency: python-pyasn1 for package: python-keyczar-0.71c-1.el6.noarch ---> Package python-setuptools.noarch 0:0.6.10-3.el6 will be installed ---> Package python-simplejson.x86_64 0:2.0.9-3.1.el6 will be installed ---> Package sshpass.x86_64 0:1.05-1.el6 will be installed --> Running transaction check ---> Package python-pyasn1.noarch 0:0.0.12a-1.el6 will be installed --> Finished Dependency Resolution Dependencies Resolved ======================================================================================== Package Arch Version Repository Size ======================================================================================== Installing: ansible noarch 1.9.4-1.el6 epel 1.7 M Installing for dependencies: python-crypto2.6 x86_64 2.6.1-2.el6 epel 513 k python-httplib2 noarch 0.7.7-1.el6 epel 70 k python-keyczar noarch 0.71c-1.el6 epel 219 k python-pyasn1 noarch 0.0.12a-1.el6 ISO 70 k python-setuptools noarch 0.6.10-3.el6 ISO 336 k python-simplejson x86_64 2.0.9-3.1.el6 ISO 126 k sshpass x86_64 1.05-1.el6 epel 19 k Transaction Summary ======================================================================================== Install 8 Package(s) Total download size: 3.0 M Installed size: 12 M Is this ok [y/N]: y Downloading Packages: (1/8): ansible-1.9.4-1.el6.noarch.rpm | 1.7 MB 00:23 (2/8): python-crypto2.6-2.6.1-2.el6.x86_64.rpm | 513 kB 00:10 (3/8): python-httplib2-0.7.7-1.el6.noarch.rpm | 70 kB 00:00 (4/8): python-keyczar-0.71c-1.el6.noarch.rpm | 219 kB 00:04 (5/8): python-pyasn1-0.0.12a-1.el6.noarch.rpm | 70 kB 00:00 (6/8): python-setuptools-0.6.10-3.el6.noarch.rpm | 336 kB 00:00 (7/8): python-simplejson-2.0.9-3.1.el6.x86_64.rpm | 126 kB 00:00 (8/8): sshpass-1.05-1.el6.x86_64.rpm | 19 kB 00:00 ---------------------------------------------------------------------------------------- Total 76 kB/s | 3.0 MB 00:40 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Warning: RPMDB altered outside of yum. Installing : sshpass-1.05-1.el6.x86_64 1/8 Installing : python-setuptools-0.6.10-3.el6.noarch 2/8 Installing : python-crypto2.6-2.6.1-2.el6.x86_64 3/8 Installing : python-simplejson-2.0.9-3.1.el6.x86_64 4/8 Installing : python-httplib2-0.7.7-1.el6.noarch 5/8 Installing : python-pyasn1-0.0.12a-1.el6.noarch 6/8 Installing : python-keyczar-0.71c-1.el6.noarch 7/8 Installing : ansible-1.9.4-1.el6.noarch 8/8 Verifying : python-keyczar-0.71c-1.el6.noarch 1/8 Verifying : python-pyasn1-0.0.12a-1.el6.noarch 2/8 Verifying : python-httplib2-0.7.7-1.el6.noarch 3/8 Verifying : python-simplejson-2.0.9-3.1.el6.x86_64 4/8 Verifying : ansible-1.9.4-1.el6.noarch 5/8 Verifying : python-crypto2.6-2.6.1-2.el6.x86_64 6/8 Verifying : python-setuptools-0.6.10-3.el6.noarch 7/8 Verifying : sshpass-1.05-1.el6.x86_64 8/8 Installed: ansible.noarch 0:1.9.4-1.el6 Dependency Installed: python-crypto2.6.x86_64 0:2.6.1-2.el6 python-httplib2.noarch 0:0.7.7-1.el6 python-keyczar.noarch 0:0.71c-1.el6 python-pyasn1.noarch 0:0.0.12a-1.el6 python-setuptools.noarch 0:0.6.10-3.el6 python-simplejson.x86_64 0:2.0.9-3.1.el6 sshpass.x86_64 0:1.05-1.el6 Complete! [root@cobbler-server software]# |
这样,Ansible就安装完了。
下面是Ansible的配置:
文件:/etc/ansible/hosts,添加主机信息(192.168.232.132)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
[root@cobbler-server ansible]# pwd /etc/ansible [root@cobbler-server ansible]# ll total 20 -rw-r--r-- 1 root root 8625 Oct 11 02:41 ansible.cfg -rw-r--r-- 1 root root 965 Oct 11 02:41 hosts drwxr-xr-x 2 root root 4096 Oct 11 02:41 roles [root@cobbler-server ansible]# [root@cobbler-server ansible]# cat hosts # This is the default ansible 'hosts' file. # # It should live in /etc/ansible/hosts # # - Comments begin with the '#' character # - Blank lines are ignored # - Groups of hosts are delimited by [header] elements # - You can enter hostnames or ip addresses # - A hostname/ip can be a member of multiple groups # Ex 1: Ungrouped hosts, specify before any group headers. green.example.com blue.example.com 192.168.100.1 192.168.100.10 # adamhuan 192.168.232.132 # Ex 2: A collection of hosts belonging to the 'webservers' group [webservers] alpha.example.org beta.example.org 192.168.1.100 192.168.1.110 # If you have multiple hosts following a pattern you can specify # them like this: www[001:006].example.com # Ex 3: A collection of database servers in the 'dbservers' group [dbservers] db01.intranet.mydomain.net db02.intranet.mydomain.net 10.25.1.56 10.25.1.57 # Here's another example of host ranges, this time there are no # leading 0s: db-[99:101]-node.example.com [root@cobbler-server ansible]# |
上面,我添加的目标主机是:192.168.232.132。
为Ansible设置到目标主机的无口令登录(SSH等价):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
[root@cobbler-server ansible]# ll /root/.ssh/ total 4 -rw-r--r-- 1 root root 397 Feb 24 03:21 known_hosts [root@cobbler-server ansible]# [root@cobbler-server ansible]# ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: f6:7f:a7:08:9a:ac:25:6a:a3:6f:de:24:b7:94:20:45 root@ansible-server The key's randomart image is: +--[ RSA 2048]----+ | E | | . | | . | | . | | . . S | | . . o . | | . * . o | | +B = o o .. .| | .*+.+.+ o..o | +-----------------+ [root@cobbler-server ansible]# [root@cobbler-server ansible]# ll /root/.ssh/ total 12 -rw------- 1 root root 1675 Feb 24 03:26 id_rsa -rw-r--r-- 1 root root 401 Feb 24 03:26 id_rsa.pub -rw-r--r-- 1 root root 397 Feb 24 03:21 known_hosts [root@cobbler-server ansible]# [root@cobbler-server ansible]# ssh-copy-id -i 192.168.232.132 root@192.168.232.132's password: Now try logging into the machine, with "ssh '192.168.232.132'", and check in: .ssh/authorized_keys to make sure we haven't added extra keys that you weren't expecting. [root@cobbler-server ansible]# |
Ansible登录目标主机:
1 2 3 4 5 6 7 |
[root@cobbler-server ansible]# ansible 192.168.232.132 -m ping 192.168.232.132 | success >> { "changed": false, "ping": "pong" } [root@cobbler-server ansible]# |
需要注意的是:在这一步,如果上面没有做SSH等价关系,那么你会看到这样的错误:
1 2 3 4 5 |
[root@cobbler-server ansible]# ansible 192.168.232.132 -m ping 192.168.232.132 | FAILED => SSH Error: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password). while connecting to 192.168.232.132:22 It is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue. [root@cobbler-server ansible]# |
用Ansible对目标主机发起命令:
1 2 3 4 5 6 7 |
[root@cobbler-server ansible]# ansible 192.168.232.132 -m shell -a "echo -e '\n@@@ date is: $(date).\non Machine: $(hostname), IP: $(ifconfig eth0 | grep "inet addr:" | cut -d':' -f2 | cut -d' ' -f1)'" -u root 192.168.232.132 | success | rc=0 >> @@@ date is: Wed Feb 24 03:40:24 CST 2016. on Machine: ansible-server, IP: 192.168.232.131 [root@cobbler-server ansible]# |
————————————————
Done。