ansible

来源:互联网 发布:python的库怎么安装 编辑:程序博客网 时间:2024/06/06 22:56

Ansible

一:ansible介绍

自动化运维工具
(1)puppet(ruby) 各个节点上运行一个agent端的程序
(2)saltstack(Python)
(3)ansible(Python)

作用:在不同的节点上,安装不同的服务,且能够配置不同服务的配置文件
能够基于playbook,实现自动化安装,自动化配置 ,自动化管理

运维工作的发展历程:

1:写脚本
2:使用自动化运维工具
3:自动化运维平台

ansible workplace 实现各个节点的管理。

zabbix: agent端,通过执行对于的操作,在节点的本地获取需要采集的数据
将数据返回给zabbix server

ansible是通过ssh服务连接到各个node,各个node而言,不需要有任何的agent端,只需要开启
sshd服务,ansible可以通过ssh协议,连接到各个node,基于免密码登入

ansible服务端:ansible、配置好ssh免密码登入

ansible:主程序
ansible-doc :模块文档接口程序
ansible-playbook:执行playbook所使用的程序

ansible连接各个主机使用ssh协议连接
(1)必须保证ansible工作站与各个node实现无密码ssh登入

二:ansible核心模块介绍
(一)ping
功能:测试指定主机是否能连接,成功会返回pong
这里写图片描述
(二)command
功能:命令模块,默认模块,用于在远程主机执行命令,缺点:运行的命令中无法使用变量,管道。
Executes a command on a remote node
[root@7 ansible]# ansible webservers -m command -a “ifconfig”

(三)shell (如果命令中有管道使用)
功能:执行的命令中有管道或变量时使用
这里写图片描述
ansible 172.16.19.249 -m shell -a “echo ‘123456’ | passwd –stdin uplooking”

(四)copy

功能:文件的复制 及权限的修改(指定属主属组和权限)

ansible dbservers -m copy -a “src=/root/hello dest=/root/nihao”
ansible 172.16.19.249 -m copy -a “src=/root/hello dest=/root/hello owner=uplooking group=uplooking mode=777”
backup # 创建一个备份文件包括时间戳信息,如果以某种方式重创错了,还可以拿回原始文件
content # 取代src=,表示直接用此处指定的信息生成为目标文件内容;
dest= # 远程节点存放文件的路径,必须是绝对路径
directory_mode # 递归复制设置目录权限,默认为系统默认权限
force # 如果目标主机包含该文件,但内容不同,如果设置为yes,则强制覆盖,如果设置为no,则只有当目标主机的目标位置不存在该文件时,才复制。默认为yes
group # 复制到远程主机后,指定文件或目录的属
mode # 复制到远程主机后,指定文件或目录权限,类似与 `chmod’指明如 0644
owner # 复制到远程主机后,指定文件或目录属主
src # 指定复制的源文件,可以是相对路径或者绝对路径,如果给出的源是目录,那么会把目录下的所有文件都复制过去
(五)cron
动能:设置管理节点生成定时任务
backup # 如果设置,创建一个crontab备份
cron_file #如果指定, 使用这个文件cron.d,而不是单个用户crontab
day # 日应该运行的工作( 1-31, , /2, etc )
hour # 小时 ( 0-23, , /2, etc )
job #指明运行的命令是什么
minute #分钟( 0-59, , /2, etc )
month # 月( 1-12, , /2, etc )
name #定时任务描述
reboot # 任务在重启时运行,不建议使用,建议使用special_time
special_time # 特殊的时间范围,参数:reboot(重启时),annually(每年),monthly(每月),weekly(每周),daily(每天),hourly(每小时)
state #指定状态,prsent表示添加定时任务,也是默认设置,absent表示删除定时任务
user # 以哪个用户的身份执行
weekday # 周 ( 0-6 for Sunday-Saturday, *, etc )
ansible all -m cron -a “minute=*/30 job=’/usr/sbin/ntpdate s2c.time.edu.cn &> /dev/null’”

(六)fetch
从远端节点拉取文件到本地
ansible 172.16.19.249 -m fetch -a “src=/etc/profile dest=/tmp”

(七)file
功能:设置文件属性
创建链接文件 ansible all -m file -a “src=/tmp/fstab path=/tmp/fstab.link state=link”
force # 需要在两种情况下强制创建软连接,一种是源文件不存在但之后会建立的情况下;另一种是目标连接已存在,需要先取消之前的软连接,有两个选项:yes|no
group # 设置文件或目录的属组
mode # 设置文件或目录的权限
owner # 设置文件或目录的属主
path= # 必选项,定义文件或目录的路径
recurse # 递归设置文件的属性,只对目录有效
src # 要被链接到的路径,只应用与state=link的情况
state # directory:如果目录不存在,创建目录;file:即使文件不存在,也不会被创建;link:创建软连接;hard:创建硬连接;touch:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间;absent:删除目录、文件或者取消链接文件
ansible 172.16.19.249 -m file -a “path=/root/nihao owner=uplooking group=uplooking mode=777”
ansible 172.16.19.249 -m file -a “path=/root/ops state=directory”

(八)hostname
管理远程节点的hostname
ansible 172.16.19.248 -m hostname -a “name=node3”

(九)pip
管理Python库依赖(首先确保pip2已经安装,如果没有安装,需要先安装python2-pip)
ansible 172.16.19.249 -m pip -a “name=jinja2”

(十)yum
功能:安装
这里写图片描述
ansible webservers -m yum -a “name=httpd state=latest”
ansible webservers -m yum -a “name=httpd state=absent”

(十一)service
功能:管理服务
arguments # 向服务传递的命令行参数
enabled # 设置服务开机自动启动,参数为yes|no
name= # 控制服务的名称
pattern # 定义一个模式,如果通过status指令来查看服务的状态时,没有响应,就会通过ps指令在进程中根据该模式进行查找,如果匹配到,则认为该服务依然在运行
runlevel # 设置服务自启动级别
sleep # 如果执行了restarted,则在stop和start之间沉睡几秒钟
state # 启动started' 关闭stopped’ 重新启动 restarted' 重载reloaded’
pattern # 定义一个模式,如果通过status指令来查看服务的状态时,没有响应,就会通过ps指令在进程中根据该模式进行查找,如果匹配到,则认为该服务依然在运行

ansible webservers -m service -a “name=httpd state=started enabled=yes”

(十二)user
功能:针对用户
comment # 用户的描述信息
createhome # 是否创建家目录
force # 在使用state=absent'是, 行为与userdel –force’一致.
group # 指定基本组
groups # 指定附加组,如果指定为(‘groups=’)表示删除所有组
home # 指定用户家目录
login_class #可以设置用户的登录类 FreeBSD, OpenBSD and NetBSD系统.
move_home # 如果设置为home='时, 试图将用户主目录移动到指定的目录
name= # 指定用户名
non_unique # 该选项允许改变非唯一的用户ID值
password # 指定用户密码
remove # 在使用
state=absent’时, 行为是与 `userdel –remove’一致.
shell # 指定默认shell
state #设置帐号状态,不指定为创建,指定值为absent表示删除
system # 当创建一个用户,设置这个用户是系统用户。这个设置不能更改现有用户。
uid #指定用户的uid
update_password # 更新用户密码
ansible 172.16.19.246 -m user -a “name=home1 createhome=no uid=4321 system=yes shell=/sbin/nologin”
这里写图片描述
stats指定操作
(十三)group
功能:针对组的
gid # 设置组的GID号
name= # 管理组的名称
state # 指定组状态,默认为创建,设置值为absent为删除
system # 设置值为yes,表示为创建系统组
ansible 172.16.19.246 -m group -a “name=uplooking gid=3120”

(十四)setup
功能:每个被管理节点在接收并运行管理命令之前,会将自己主机相关信息,如操作系统版本、IP地址等报告给远程的ansbile主机;
ansible 172.16.19.246 -m setup

(十五)script
将ansible主机的脚本传递到被管控的主机上执行
ansible 172.16.19.246 -m script -a “/root/useradd.sh”

(十六)template
基于模板的方式生成一个文件复制到被管理的主机,这样就可以在模板语言中判断,不同的系统复制不同的配置文件,这就是template模块区别与copy模块的地方

(十七)unarchive
将本地的归档文件拷贝到被管控的主机上,解压归档文件
ansible 172.16.19.246 -m unarchive -a “src=/root/wordpress.zip dest=/tmp”

(十八)mount
挂载和卸载模块,path指定本地挂载点,src指定需要挂载的设备
ansible 172.16.19.246 -m mount -a “path=/var/www/html src=172.16.19.246:/data/static fstype=nfs state=mounted”

三:playbook详解—YAML格式的文本

在playbook中有一些核心的指令

hosts:指明命令运行在哪个node之上
remote_user:在远端的node之上以什么用户的身份运行命令
var:给模板传递变量值
tasks:指明需要执行的命令
template:指明模板文件
handler:指明句柄所在文件目录名
Roles:指明需要运行的角色名

(一)检查playbook是否正确
ansible-playbook –syntax-check first.yml

(二)预执行playbook
ansible-playbook –check first.yml

(三)查看执行命令的node节点的IP地址或主机名
ansible-playbook –list-hosts first.yml

first.yml

  • hosts: webservers
    remote_user: root
    tasks:

    • name: install httpd
      yum: name=httpd state=latest
    • name: distribute config file
      copy: src=config/httpd.conf dest=/etc/httpd/conf/httpd.conf
      tags: redistibute
      notify: restart httpd
    • name: start httpd
      service: name=httpd state=started enabled=yes
    • name: check httpd started
      shell: netstat -lntup | grep 80

    handlers:

    • name: restart httpd
      service: name=httpd state=restarted enabled=yes

(四)使用vars定义变量
- hosts: dbservers
remote_user: root
vars:
- username: home
- groupname: uplooking
tasks:
- name: add group
group: name={{ groupname }}
- name: add user
user: name={{ username }} state=present
Ansible

一:ansible介绍

自动化运维工具
(1)puppet(ruby) 各个节点上运行一个agent端的程序
(2)saltstack(Python)
(3)ansible(Python)

作用:在不同的节点上,安装不同的服务,且能够配置不同服务的配置文件
能够基于playbook,实现自动化安装,自动化配置 ,自动化管理

运维工作的发展历程:

1:写脚本
2:使用自动化运维工具
3:自动化运维平台

ansible workplace 实现各个节点的管理。

zabbix: agent端,通过执行对于的操作,在节点的本地获取需要采集的数据
将数据返回给zabbix server

ansible是通过ssh服务连接到各个node,各个node而言,不需要有任何的agent端,只需要开启
sshd服务,ansible可以通过ssh协议,连接到各个node,基于免密码登入

ansible服务端:ansible、配置好ssh免密码登入

ansible:主程序
ansible-doc :模块文档接口程序
ansible-playbook:执行playbook所使用的程序

ansible连接各个主机使用ssh协议连接
(1)必须保证ansible工作站与各个node实现无密码ssh登入

二:ansible核心模块介绍
(一)ping

(二)command
Executes a command on a remote node
[root@7 ansible]# ansible webservers -m command -a “ifconfig”

(三)shell
ansible 172.16.19.249 -m shell -a “echo ‘123456’ | passwd –stdin uplooking”

(四)copy
ansible dbservers -m copy -a “src=/root/hello dest=/root/nihao”
ansible 172.16.19.249 -m copy -a “src=/root/hello dest=/root/hello owner=uplooking group=uplooking mode=777”

(五)cron
ansible all -m cron -a “minute=*/30 job=’/usr/sbin/ntpdate s2c.time.edu.cn &> /dev/null’”

(六)fetch

ansible 172.16.19.249 -m fetch -a “src=/etc/profile dest=/tmp”

(七)file
ansible 172.16.19.249 -m file -a “path=/root/nihao owner=uplooking group=uplooking mode=777”
ansible 172.16.19.249 -m file -a “path=/root/ops state=directory”

(八)hostname

ansible 172.16.19.248 -m hostname -a “name=node3”

(九)pip
ansible 172.16.19.249 -m pip -a “name=jinja2”

(十)yum

ansible webservers -m yum -a “name=httpd state=latest”
ansible webservers -m yum -a “name=httpd state=absent”

(十一)service

ansible webservers -m service -a “name=httpd state=started enabled=yes”

(十二)user
ansible 172.16.19.246 -m user -a “name=home1 createhome=no uid=4321 system=yes shell=/sbin/nologin”

(十三)group

ansible 172.16.19.246 -m group -a “name=uplooking gid=3120”

(十四)setup
ansible 172.16.19.246 -m setup

(十五)script

ansible 172.16.19.246 -m script -a “/root/useradd.sh”

(十六)template

(十七)unarchive
ansible 172.16.19.246 -m unarchive -a “src=/root/wordpress.zip dest=/tmp”

(十八)mount

ansible 172.16.19.246 -m mount -a “path=/var/www/html src=172.16.19.246:/data/static fstype=nfs state=mounted”

三:playbook详解—YAML格式的文本

在playbook中有一些核心的指令

hosts:指明命令运行在哪个node之上
remote_user:在远端的node之上以什么用户的身份运行命令
var:给模板传递变量值
tasks:指明需要执行的命令
template:指明模板文件
handler:指明句柄所在文件目录名
Roles:指明需要运行的角色名

(一)检查playbook是否正确
ansible-playbook –syntax-check first.yml

(二)预执行playbook
ansible-playbook –check first.yml

(三)查看执行命令的node节点的IP地址或主机名
ansible-playbook –list-hosts first.yml

first.yml

  • hosts: webservers
    remote_user: root
    tasks:

    • name: install httpd
      yum: name=httpd state=latest
    • name: distribute config file
      copy: src=config/httpd.conf dest=/etc/httpd/conf/httpd.conf
      tags: redistibute
      notify: restart httpd
    • name: start httpd
      service: name=httpd state=started enabled=yes
    • name: check httpd started
      shell: netstat -lntup | grep 80

    handlers:

    • name: restart httpd
      service: name=httpd state=restarted enabled=yes

(四)使用vars定义变量
- hosts: dbservers
remote_user: root
vars:
- username: home
- groupname: uplooking
tasks:
- name: add group
group: name={{ groupname }}
- name: add user
user: name={{ username }} state=present
Ansible

一:ansible介绍

自动化运维工具
(1)puppet(ruby) 各个节点上运行一个agent端的程序
(2)saltstack(Python)
(3)ansible(Python)

作用:在不同的节点上,安装不同的服务,且能够配置不同服务的配置文件
能够基于playbook,实现自动化安装,自动化配置 ,自动化管理

运维工作的发展历程:

1:写脚本
2:使用自动化运维工具
3:自动化运维平台

ansible workplace 实现各个节点的管理。

zabbix: agent端,通过执行对于的操作,在节点的本地获取需要采集的数据
将数据返回给zabbix server

ansible是通过ssh服务连接到各个node,各个node而言,不需要有任何的agent端,只需要开启
sshd服务,ansible可以通过ssh协议,连接到各个node,基于免密码登入

ansible服务端:ansible、配置好ssh免密码登入

ansible:主程序
ansible-doc :模块文档接口程序
ansible-playbook:执行playbook所使用的程序

ansible连接各个主机使用ssh协议连接
(1)必须保证ansible工作站与各个node实现无密码ssh登入

二:ansible核心模块介绍
(一)ping

(二)command
Executes a command on a remote node
[root@7 ansible]# ansible webservers -m command -a “ifconfig”

(三)shell
ansible 172.16.19.249 -m shell -a “echo ‘123456’ | passwd –stdin uplooking”

(四)copy
ansible dbservers -m copy -a “src=/root/hello dest=/root/nihao”
ansible 172.16.19.249 -m copy -a “src=/root/hello dest=/root/hello owner=uplooking group=uplooking mode=777”

(五)cron
ansible all -m cron -a “minute=*/30 job=’/usr/sbin/ntpdate s2c.time.edu.cn &> /dev/null’”

(六)fetch

ansible 172.16.19.249 -m fetch -a “src=/etc/profile dest=/tmp”

(七)file
ansible 172.16.19.249 -m file -a “path=/root/nihao owner=uplooking group=uplooking mode=777”
ansible 172.16.19.249 -m file -a “path=/root/ops state=directory”

(八)hostname

ansible 172.16.19.248 -m hostname -a “name=node3”

(九)pip
ansible 172.16.19.249 -m pip -a “name=jinja2”

(十)yum

ansible webservers -m yum -a “name=httpd state=latest”
ansible webservers -m yum -a “name=httpd state=absent”

(十一)service

ansible webservers -m service -a “name=httpd state=started enabled=yes”

(十二)user
ansible 172.16.19.246 -m user -a “name=home1 createhome=no uid=4321 system=yes shell=/sbin/nologin”

(十三)group

ansible 172.16.19.246 -m group -a “name=uplooking gid=3120”

(十四)setup
ansible 172.16.19.246 -m setup

(十五)script

ansible 172.16.19.246 -m script -a “/root/useradd.sh”

(十六)template

(十七)unarchive
ansible 172.16.19.246 -m unarchive -a “src=/root/wordpress.zip dest=/tmp”

(十八)mount

ansible 172.16.19.246 -m mount -a “path=/var/www/html src=172.16.19.246:/data/static fstype=nfs state=mounted”

三:playbook详解—YAML格式的文本

在playbook中有一些核心的指令

hosts:指明命令运行在哪个node之上
remote_user:在远端的node之上以什么用户的身份运行命令
var:给模板传递变量值
tasks:指明需要执行的命令
template:指明模板文件
handler:指明句柄所在文件目录名
Roles:指明需要运行的角色名

(一)检查playbook是否正确
ansible-playbook –syntax-check first.yml

(二)预执行playbook
ansible-playbook –check first.yml

(三)查看执行命令的node节点的IP地址或主机名
ansible-playbook –list-hosts first.yml

first.yml

  • hosts: webservers
    remote_user: root
    tasks:

    • name: install httpd
      yum: name=httpd state=latest
    • name: distribute config file
      copy: src=config/httpd.conf dest=/etc/httpd/conf/httpd.conf
      tags: redistibute
      notify: restart httpd
    • name: start httpd
      service: name=httpd state=started enabled=yes
    • name: check httpd started
      shell: netstat -lntup | grep 80

    handlers:

    • name: restart httpd
      service: name=httpd state=restarted enabled=yes

(四)使用vars定义变量
- hosts: dbservers
remote_user: root
vars:
- username: home
- groupname: uplooking
tasks:
- name: add group
group: name={{ groupname }}
- name: add user
user: name={{ username }} state=present