标签:started ocs public 技术分享 ipv4 管理 remove resolved reset
ansible 简介 :ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。
ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。
Ansible 基本架构由六个部分构成 :
角色 | 主机名 | IP地址 | 组名 |
---|---|---|---|
控制主机 | node 1 | 192.168.217.137 | |
被控制主机 | node 2 | 192.168.217.138 | webserver |
被控制主机 | node 3 | 192.168.217.139 | mysql |
被控制主机 | node 4 | 192.168.217.140 | mysql |
yum install -y epel-release #安装epel源
yum install ansible -y
ansible --version #查看ansible版本
yum install tree -y
tree /etc/ansible/ #树状结构展示文件夹
/etc/ansible/
├── ansible.cfg #ansible的配置文件
├── hosts #ansible的主仓库,用于存储需要管理的远程主机的相关信息
└── roles #角色
cd /etc/ansible
vim hosts
[webserver] #被管理主机分类 可以添加多个ip
192.168.217.138
[mysql]
192.168.217.139
192.168.217.140
[root@bogon ansible]# ssh-keygen -t rsa #生成密钥
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): #密钥生成位置是否存在root家目录 回车即可
Created directory ‘/root/.ssh‘.
Enter passphrase (empty for no passphrase): #密钥验证的密码
Enter same passphrase again:
.......
The key‘s randomart image is:
+---[RSA 2048]----+
|. +=*O. . |
|.+oo+o= o |
|.+ E. B |
|. + o B |
| . S + . . |
| ..+o . .o|
| .* = ..+|
| .oO o o.|
| .oo=... |
+----[SHA256]-----+
[root@bogon ansible]# ssh-copy-id root@192.168.217.138 #把公钥发送到被管理主机
[root@bogon ansible]# ssh-copy-id root@192.168.217.139 #公钥默认放在对方用户家目录下 .ssh
.....
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.217.139‘s password: #对方root用户密码
[root@bogon ansible]# ssh-agent bash #把bash挂到ssh-agent下面
[root@bogon ansible]# ssh-add #添加私钥
Enter passphrase for /root/.ssh/id_rsa: #密钥验证密码
Identity added: /root/.ssh/id_rsa (/root/.ssh/id_rsa)
#如果没有免交互,则使用 ansible 管理被管理主机是需要输入密钥验证密码。
Ansible 基本语法 :
ansible <host-pattern> [-m module_name] [-a args]
<host-pattern> : 对那些主机生效
[-m module_name] : 要使用的模块
[-a args] : 模块特有参数
ansible-doc -s command #查看模块的使用说明
[root@bogon ~]# ansible 192.168.217.138 -m command -a ‘date‘
192.168.217.138 | SUCCESS | rc=0 >>
2018年 08月 02日 星期四 09:43:45 CST
[root@bogon ~]# ansible webserver -m command -a ‘date‘
192.168.217.138 | SUCCESS | rc=0 >>
2018年 08月 02日 星期四 09:45:40 CST
[root@bogon ~]# ansible all -m command -a ‘date‘
192.168.217.138 | SUCCESS | rc=0 >>
2018年 08月 02日 星期四 09:47:11 CST
192.168.217.139 | SUCCESS | rc=0 >>
2018年 08月 02日 星期四 09:47:11 CST
ansible-doc -s cron #查看模块的使用说明
[root@bogon ~]# ansible webserver -m cron -a ‘minute="*/1" job="/bin/echo hello" name="test cron job"‘ #添加任务
192.168.217.138 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"test cron job"
]
}
[root@bogon ~]# ansible webserver -a ‘crontab -l‘ #查看任务
192.168.217.138 | SUCCESS | rc=0 >>
#Ansible: test cron job
*/1 * * * * /bin/echo hello
[root@bogon ~]# ansible webserver -m cron -a ‘minute="*/1" job="/bin/echo hello" name="test cron job" state=absent‘
192.168.217.138 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": []
}
[root@bogon ~]# ansible webserver -a ‘crontab -l‘
192.168.217.138 | SUCCESS | rc=0 >>
ansible-doc -s user #查看模块的使用说明
[root@bogon ~]# ansible mysql -m user -a ‘name="user01"‘
192.168.217.139 | SUCCESS => {
"changed": true,
"comment": "",
"create_home": true,
"group": 1001,
"home": "/home/user01",
"name": "user01",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1001
}
[root@bogon ~]# ansible mysql -m user -a ‘name="user01" state=absent‘
192.168.217.139 | SUCCESS => {
"changed": true,
"force": false,
"name": "user01",
"remove": false,
"state": "absent"
}
ansible-doc -s group #查看模块的使用说明
[root@bogon ~]# ansible mysql -m group -a ‘name=mysql gid=306 system=yes‘ #创建 mysql 组 指定gid
192.168.217.139 | SUCCESS => {
"changed": true,
"gid": 306,
"name": "mysql",
"state": "present",
"system": true
}
[root@bogon ~]# ansible mysql -m user -a ‘name=mysql uid=306 system=yes group=mysql‘ #创建用户 指定uid 指定组
192.168.217.139 | SUCCESS => {
"changed": true,
"comment": "",
"create_home": true,
"group": 306,
"home": "/home/mysql",
"name": "mysql",
"shell": "/bin/bash",
"state": "present",
"system": true,
"uid": 306
}
ansible-doc -s copy #查看模块的使用说明
[root@bogon ~]# ansible mysql -m copy -a ‘src=/etc/fstab dest=/opt/abc.ansible owner=root mode=640‘ #属主、文件权限
192.168.217.139 | SUCCESS => {
"changed": true,
"checksum": "4b11a9d8a720fb2ea2b2bcbafef6c37a0621b9ef",
"dest": "/opt/abc.ansible",
"gid": 0,
"group": "root",
"md5sum": "e826ed6cacb28bdb65d4af2defb77bf1",
"mode": "0640",
"owner": "root",
"secontext": "system_u:object_r:usr_t:s0",
"size": 595,
"src": "/root/.ansible/tmp/ansible-tmp-1533176788.96-59671060453195/source",
"state": "file",
"uid": 0
}
[root@bogon ~]# ansible mysql -m copy -a ‘content="hello" dest=/opt/abc.ansible‘
192.168.217.139 | SUCCESS => {
"changed": true,
"checksum": "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d",
"dest": "/opt/abc.ansible",
"gid": 0,
"group": "root",
"md5sum": "5d41402abc4b2a76b9719d911017c592",
"mode": "0640",
"owner": "root",
"secontext": "system_u:object_r:usr_t:s0",
"size": 5,
"src": "/root/.ansible/tmp/ansible-tmp-1533177209.24-236200926193569/source",
"state": "file",
"uid": 0
}
ansible-doc -s file #查看模块的使用说明
[root@bogon ~]# ansible mysql -m file -a ‘owner=mysql group=mysql mode=644 path=/opt/abc.ansible‘
192.168.217.139 | SUCCESS => {
"changed": true,
"gid": 306,
"group": "mysql",
"mode": "0644",
"owner": "mysql",
"path": "/opt/abc.ansible",
"secontext": "system_u:object_r:usr_t:s0",
"size": 5,
"state": "file",
"uid": 306
}
[root@bogon ~]# ansible mysql -m file -a ‘path=/opt/abc.link src=/opt/abc.ansible state=link‘
192.168.217.139 | SUCCESS => {
"changed": true,
"dest": "/opt/abc.link",
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"secontext": "unconfined_u:object_r:usr_t:s0",
"size": 16,
"src": "/opt/abc.ansible",
"state": "link",
"uid": 0
}
[root@bogon ~]# ansible all -m ping
192.168.217.139 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.217.138 | SUCCESS => {
"changed": false,
"ping": "pong"
}
ansible-doc -s service #查看模块的使用说明
[root@bogon ~]# ansible webserver -a ‘systemctl status httpd.service‘ #(centos 7 )
192.168.217.138 | FAILED | rc=3 >>
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Docs: man:httpd(8)
man:apachectl(8)
ansible webserver -a ‘chkconfig --list httpd‘
[root@bogon ~]# ansible webserver -m service -a ‘enabled=true name=httpd state=started‘
192.168.217.138 | SUCCESS => {
"changed": true,
"enabled": true,
"name": "httpd",
"state": "started",
........
ansible-doc -s shell #查看模块的使用说明
[root@bogon ~]# ansible mysql -m user -a ‘name=user01‘
[root@bogon ~]# ansible mysql -m shell -a ‘echo abc123 | passwd --stdin user01‘
192.168.217.139 | SUCCESS | rc=0 >>
更改用户 user01 的密码 。
passwd:所有的身份验证令牌已经成功更新。
ansible-doc -s script #查看模块的使用说明
[root@bogon ~]# vim test.sh
[root@bogon ~]# chmod +x test.sh
[root@bogon ~]# ./test.sh
haha
[root@bogon ~]# ansible mysql -m script -a ‘test.sh‘
192.168.217.139 | SUCCESS => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.217.139 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.217.139 closed."
],
"stdout": "haha\r\n",
"stdout_lines": [
"haha"
]
}
ansible-doc -s yum #查看模块的使用说明
[root@bogon ~]# ansible mysql -m yum -a ‘name=zsh‘
192.168.217.139 | SUCCESS => {
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror, langpacks\nLoading mirror speeds from cached hostfile\n * base: mirrors.nju.edu.cn\n * extras: mirrors.nju.edu.cn\n * updates: mirrors.shu.edu.cn\nResolving Dependencies\n--> Running transaction check\n---> Package zsh.x86_64 0:5.0.2-28.el7 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n zsh x86_64 5.0.2-28.el7 base 2.4 M\n\nTransaction Summary\n================================================================================\nInstall 1 Package\n\nTotal download size: 2.4 M\nInstalled size: 5.6 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : zsh-5.0.2-28.el7.x86_64 1/1 \n Verifying : zsh-5.0.2-28.el7.x86_64 1/1 \n\nInstalled:\n zsh.x86_64 0:5.0.2-28.el7 \n\nComplete!\n"
]
}
[root@bogon ~]# ansible mysql -m yum -a ‘name=zsh state=absent‘
192.168.217.139 | SUCCESS => {
"changed": true,
"msg": "",
"rc": 0,
"results": [
"已加载插件:fastestmirror, langpacks\n正在解决依赖关系\n--> 正在检查事务\n---> 软件包 zsh.x86_64.0.5.0.2-28.el7 将被 删除\n--> 解决依赖关系完成\n\n依赖关系解决\n\n================================================================================\n Package 架构 版本 源 大小\n================================================================================\n正在删除:\n zsh x86_64 5.0.2-28.el7 @base 5.6 M\n\n事务概要\n================================================================================\n移除 1 软件包\n\n安装大小:5.6 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n 正在删除 : zsh-5.0.2-28.el7.x86_64 1/1 \n 验证中 : zsh-5.0.2-28.el7.x86_64 1/1 \n\n删除:\n zsh.x86_64 0:5.0.2-28.el7 \n\n完毕!\n"
]
}
ansible-doc -s setup #查看模块的使用说明
[root@bogon ~]# ansible mysql -m setup
192.168.217.139 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"192.168.122.1",
"192.168.217.139"
],
"ansible_all_ipv6_addresses": [
"fe80::702c:dff:a392:257c",
"fe80::129f:929e:aad4:8dde"
],
"ansible_apparmor": {
"status": "disabled"
......
#输出信息较多
标签:started ocs public 技术分享 ipv4 管理 remove resolved reset
原文地址:http://blog.51cto.com/13640803/2153583