ansible批量自动化管理工具

当下有许多的运维自动化工具( 配置管理 ),例如:Ansible、SaltStack、Puppet、Fabric 等。

Ansible 一种集成 IT 系统的配置管理、应用部署、执行特定任务的开源平台,是 AnsibleWorks 公司名下的项目,该公司由 Cobbler 及 Func 的作者于 2012 年创建成立。

Ansible 基于 Python 语言实现,由 Paramiko 和 PyYAML 两个关键模块构建。

Ansible 特点:

>> 部署简单,只需在主控端部署 Ansible 环境,被控端无需做任何操作。
>> 默认使用 SSH(Secure Shell)协议对设备进行管理。
>> 主从集中化管理。
>> 配置简单、功能强大、扩展性强。
>> 支持 API 及自定义模块,可通过 Python 轻松扩展。
>> 通过 Playbooks 来定制强大的配置、状态管理。
>> 对云计算平台、大数据都有很好的支持。
>> 提供一个功能强大、操作性强的 Web 管理界面和 REST API 接口 ---- AWX 平台。

Andible服务的安装和部署:

1.  [root@localhost ~]# cat /etc/redhat-release

2.  CentOS Linux release 7.5.1804 (Core)

3.  [root@localhost ~]# uname -r

4.  3.10.0-862.3.3.el7.x86_64

5.  [root@localhost ~]# systemctl stop firewalld

6.  [root@localhost ~]# systemctl disable firewalld

7.  [root@localhost ~]# systemctl stop NetworkManager

8.  [root@localhost ~]# systemctl disable NetworkManager

9.   

10. #通过yum源方式安装ansible

11. [root@ansible ~]# yum -y install epel-release

12. [root@ansible ~]# yum -y install ansible

13.  

14. #通过Python的pip方式安装ansible

15. [root@ansible ~]# yum -y install epel-release

16. [root@ansible ~]# yum -y install python2-pip

17. [root@ansible ~]# pip install ansible

命令的基本语法如下所示:

1.  ansible <被操控的主机或主机组或all> [-m 模块名] [-a 具体命令]

ping模块

Ansible中使用ping模块来检测指定主机的连通性

1.  #测试单主机

2.  [root@ansible ~]# ansible Web01 -m ping

3.  Web01 | SUCCESS => {

4.      "changed": false,

5.      "ping": "pong"

6.  }

command模块

在远程主机执行命令,不支持管道符和重定向等复杂命令,可完全被shell模块替代

1.  [root@ansible ~]# ansible Web01 -m command -a 'uptime'

2.  Web01 | SUCCESS | rc=0 >>

3.   22:14:43 up  9:43,  3 users,  load average: 0.00, 0.01, 0.05

shell模块

Ansible中的shell模块可以在被管理主机上运行命令,并支持像管道符重定向这样的复杂命令。

1.  #在Web01上创建用户yunjisuan,并非交互方式设定密码

2.  [root@ansible ~]# ansible Web01 -m shell -a 'useradd yunjisuan'

3.  Web01 | SUCCESS | rc=0 >>

4.   

5.  [root@ansible ~]# ansible Web01 -m shell -a 'echo 123123 | passwd --stdin yunjisuan'

6.  Web01 | SUCCESS | rc=0 >>

7.  更改用户 yunjisuan 的密码 。

8.  passwd:所有的身份验证令牌已经成功更新。

9.   

10. [root@ansible ~]# ansible Web01 -m shell -a 'id yunjisuan'

11. Web01 | SUCCESS | rc=0 >>

12. uid=1000(yunjisuan) gid=1000(yunjisuan) 组=1000(yunjisuan)

13.  

14. [root@ansible ~]# ansible Web01 -m shell -a 'tail -1 /etc/shadow'

15. Web01 | SUCCESS | rc=0 >>

16. yunjisuan:$6$4y7c1tkV$oPZW0psDdAzJp5RomBrOpSlTuvsdQ/5JaBYHU.LOPsYQ0o7EpPFRMuh/X9ruwcmBcZbN.l/glBTfDKm//jJP60:17782:0:99999:7:::

17.  

18. #在所有被管理的主机的/etc/hosts文件里添加Ansible管理服务器的IP地址映射

19. [root@ansible ~]# ansible all -m shell -a 'echo "ansible 192.168.200.183" >> /etc/hosts'

20. Web02 | SUCCESS | rc=0 >>

21.  

22.  

23. Web01 | SUCCESS | rc=0 >>

24.  

25. [root@ansible ~]# ansible all -m shell -a 'tail -1 /etc/hosts'

26. Web01 | SUCCESS | rc=0 >>

27. ansible 192.168.200.183

28.  

29. Web02 | SUCCESS | rc=0 >>

30. ansible 192.168.200.183

copy模块

Ansible中的copy模块用于实现文件复制和批量下发文件。其中使用src来定义本地源文件路径;使用dest定义被管理主机文件路径;使用content则是使用指定信息内容来生成目标文件。

1.  #将本地的/etc/hosts文件拷贝到所有被管理的主机的/etc/hosts路径下覆盖同名文件,并指定属主和权限,若拷贝的文件与目标文件内容不同,则备份目标文件再覆盖。

2.  [root@ansible ~]# ansible all -m copy -a 'src=/etc/hosts dest=/etc/hosts owner=root mode=640 backup=yes'

script模块

Ansible中的script模块可以将本地脚本复制到被管理主机的内存中并运行,不会在被管理主机中留下脚本文件。

1.  #编写一个脚本,然后通过ansible的script模块远程向被管理主机执行此脚本

2.  [root@ansible ~]# echo 'echo "1111" >> /tmp/test' >> /tmp/test.sh

3.  [root@ansible ~]# cat /tmp/test.sh

4.  echo "1111" >> /tmp/test

5.   

6.   

7.  [root@ansible ~]# ansible all -m script -a '/tmp/test.sh'

8.  Web01 | SUCCESS => {

9.      "changed": true,

10.     "rc": 0,

11.     "stderr": "Shared connection to 192.168.200.184 closed.\r\n",

12.     "stderr_lines": [

13.         "Shared connection to 192.168.200.184 closed."

14.     ],

15.     "stdout": "",

16.     "stdout_lines": []

17. }

18. Web02 | SUCCESS => {

19.     "changed": true,

20.     "rc": 0,

21.     "stderr": "Shared connection to 192.168.200.185 closed.\r\n",

22.     "stderr_lines": [

23.         "Shared connection to 192.168.200.185 closed."

24.     ],

25.     "stdout": "",

26.     "stdout_lines": []

27. }

script模块

Ansible中的script模块可以将本地脚本复制到被管理主机的内存中并运行,不会在被管理主机中留下脚本文件。

1.  #编写一个脚本,然后通过ansible的script模块远程向被管理主机执行此脚本

2.  [root@ansible ~]# echo 'echo "1111" >> /tmp/test' >> /tmp/test.sh

3.  [root@ansible ~]# cat /tmp/test.sh

4.  echo "1111" >> /tmp/test

5.   

6.   

7.  [root@ansible ~]# ansible all -m script -a '/tmp/test.sh'

8.  Web01 | SUCCESS => {

9.  "changed": true,

10. "rc": 0,

11. "stderr": "Shared connection to 192.168.200.184 closed.\r\n",

12. "stderr_lines": [

13. "Shared connection to 192.168.200.184 closed."

14. ],

15. "stdout": "",

16. "stdout_lines": []

17. }

18. Web02 | SUCCESS => {

19. "changed": true,

20. "rc": 0,

21. "stderr": "Shared connection to 192.168.200.185 closed.\r\n",

22. "stderr_lines": [

23. "Shared connection to 192.168.200.185 closed."

24. ],

25. "stdout": "",

26. "stdout_lines": []

27. }

28.  

29. [root@ansible ~]# ansible all -m shell -a 'cat /tmp/test'

30. Web02 | SUCCESS | rc=0 >>

31. 1111

32.  

33. Web01 | SUCCESS | rc=0 >>

34. 1111

yum模块

利用yum模块安装软件包,虽然能被shell模块替代
但是用yum模块更显专业一些

  • 软件包名:

o   name:指定软件包的名字

  • state状态:

o   present:安装软件包(默认就是这个)

o   absent:卸载软件包

1.  #安装nmap软件包

2.  [root@ansible ~]# ansible all -m yum -a 'name=nmap'

3.   

4.  #卸载nmap软件包

5.  [root@ansible ~]# ansible all -m yum -a 'name=nmap state=absent'

service模块

利用service模块管理服务程序,虽然能被shell模块替代
但是用service模块更显专业一些

  • 服务名称:

o   name:指定服务的名字

  • state状态:

o   started:启动服务

o   stopped:停止服务

o   restarted:重启服务

o   reloaded:平滑重载

  • enabled开机自启动:

o   true:设置开机自启动

o   false:设置开启不启动

1.  #启动firewalld并设置开机自启动

2.  [root@ansible ~]# ansible Web01 -m service -a 'name=firewalld state=started enabled=true'

3.   

4.  #关闭firewalld并设置开机不启动

5.  [root@ansible ~]# ansible Web01 -m service -a 'name=firewalld state=stopped enabled=false'

3.9 user模块

用户管理模块。管理用户账号

  • :指定用户名

o   name:指定操作的用户的名字

  • :用户描述

o   comment:指定用户的描述信息

  • :createhome:是否创建家目录
  • :uid:指定用户的uid号
  • :groups:指定用户的附加组(默认创建和用户名相同的组)
  • :password:指定用户的密码
  • :update_password:更新用户的密码
  • :shell指定用户的登陆方式

o   /bin/bash:能登录系统

o   /sbin/nologin:不能登录系统

  • :home:指定用户的家目录路径
  • :state状态:

o   present:创建用户(默认就是这个)

o   absent:删除用户

  • :remove:当指定state=absent时,确认是否删除用户家目录

o   true

o   false

1.  #在Web02上创建一个普通用户yunjisuan,并设置用户的密码为123123

2.  [root@ansible ~]# ansible Web02 -m user -a 'name=yunjisuan comment="welcom to yunjisuan" uid=1066 groups=wheel password=123123 shell=/bin/bash home=/home/yunjisuan'

3.  Web02 | SUCCESS => {

4.      "changed": true,

5.      "comment": "welcom to yunjisuan",

6.      "create_home": true,

7.      "group": 1066,

8.      "groups": "wheel",

9.      "home": "/home/yunjisuan",

10.    "name": "yunjisuan",

11.    "password": "NOT_LOGGING_PASSWORD",

12.    "shell": "/bin/bash",

13.    "state": "present",

14.    "system": false,

15.    "uid": 1066

16.}

17.[root@ansible ~]# ansible Web02 -m shell -a 'tail -1 /etc/passwd'

18.Web02 | SUCCESS | rc=0 >>

19.yunjisuan:x:1066:1066:welcom to yunjisuan:/home/yunjisuan:/bin/bash

20. 

21.[root@ansible ~]# ansible Web02 -m shell -a 'tail -1 /etc/shadow'

22.Web02 | SUCCESS | rc=0 >>

23.yunjisuan:123123:17783:0:99999:7:::     #密码居然是明文!!!

利用ansible的user模块状态用户时要注意在password参数的后边添加密文,否则不能登陆用户
通过Python的pip程序安装passlib即可为密码加密

1.  #安装Python2的pip工具,并通过pip工具安装Python的加密模块来给密码加密

2.  [root@ansible ~]# yum -y install epel-release

3.  [root@ansible ~]# yum -y install python2-pip

4.  [root@ansible ~]# pip install passlib

5.   

6.  #生成密文密码

7.  [root@ansible ~]# python -c "from passlib.hash import sha512_crypt;import getpass;print sha512_crypt.encrypt(getpass.getpass())"

8.  Password:       #输入你想要加密的密码

9.  $6$rounds=656000$Tw15COd8DLh/VS94$Mcmz/8CcjBKiEl0mYHcOQQCxEA5mz66EcGH2qXVk6o.Sm7FsRS.DsDVy6ET8iI6jDa045I94slZqWFwyYnRSW1          #加密后的密码

10. 

11. 

12.#删除之前创建的yunjisuan用户,并删除它的家目录

13.[root@ansible ~]# ansible Web02 -m user -a 'name=yunjisuan state=absent remove=true'

14.Web02 | SUCCESS => {

15.    "changed": true,

16.    "force": false,

17.    "name": "yunjisuan",

18.    "remove": true,

19.    "state": "absent"

20.}

21. 

22.#继续在Web02上创建yunjisuan用户

23.[root@ansible ~]# ansible Web02 -m user -a 'name=yunjisuan comment="welcom to yunjisuan" uid=1066 groups=wheel password=$6$rounds=656000$Tw15COd8DLh/VS94$Mcmz/8CcjBKiEl0mYHcOQQCxEA5mz66EcGH2qXVk6o.Sm7FsRS.DsDVy6ET8iI6jDa045I94slZqWFwyYnRSW1 shell=/bin/bash' home=/home/yunjisuan'

24. 

25.[root@ansible ~]# ansible Web02 -m shell -a 'tail -1 /etc/shadow'

26.Web02 | SUCCESS | rc=0 >>

27.yunjisuan:$6$rounds=656000$Tw15COd8DLh/VS94$Mcmz/8CcjBKiEl0mYHcOQQCxEA5mz66EcGH2qXVk6o.Sm7FsRS.DsDVy6ET8iI6jDa045I94slZqWFwyYnRSW1:17783:0:99999:7:::      #终于密文了

3.10 setup模块

Ansible中使用setup模块收集,查看被管理主机的factsfactsAnsible采集被管理主机设备信息的一个功能)。每个被管理主机在接收并运行管理命令之前,都会将自己的相关信息(操作系统版本,IP地址等)发送给控制主机

1.  #查看远程主机的facts信息

2.  [root@ansible ~]# ansible Web01 -m setup | head

3.  Web01 | SUCCESS => {

4.      "ansible_facts": {

5.          "ansible_all_ipv4_addresses": [

6.              "192.168.200.184"

7.          ],

8.          "ansible_all_ipv6_addresses": [

9.              "fe80::20c:29ff:fe77:16ad"

10.         ],

11.         "ansible_apparmor": {

12.             "status": "disabled"

posted @ 2018-11-14 16:38  aspen-  阅读(220)  评论(0)    收藏  举报