静态invertory
主机纳管:/etc/ansible/hosts
ansible_shell_type
ansible支持多种传输机制,默认值smart表示自能传输模式,ansible_shell_type可以使用sh,fish和
powershell作为该参数的合法值
ansible_python_interpreter
该参数调用被管理节点上的/usr/bin/python作为默认的解释器。现在很多版本的Linux的操作系统已经使用
python3作为系统默认的解释器,就需要修改参数为python3解释器的路径,便于ansilbe调用被管理节点上
的python3解释器作为ansible模块在远端执行的任务解释器
2.组
在实际工作中将任务内容相同的管理节点放在一起管理并执行任务,这就是invertory文件管理
IP相同端口不同,如果给端口去掉就会覆盖掉别的ip,这种情况可以加上别名
3.别名
在invertory文件中修改或增加
4.被管理节点的序列化描述
在前面的invertory文件中有一个特别的主机描述:db-[1:100]-域名。在invertory文件中,这样的配置描述了
域名从db-1-域名到db-100-域名的101台被管理节点主机的信息,这种配置描述方式非常适合有规律的IP地
址和域名有规律的场景。、
数字和字母都可以
域名www.[01:50].cc.com
域名www.[a:v].bb.com
IP192.168.40.[129:140]
5.invertory中的变量定义
前面的行为参数,就是具有特殊意义的ansible变量。可以任意指定变量名对其进行赋值操作。变量名的指定
和赋值可以分为针对被管理节点的主机变量和针对被管理节点组的变量
直接定义 :域名 color=red:中的变量color仅能提供给被管理节点 域名 使用;即使变量名相同,但是不同的被
管理节点访问color变量时得到的值去大不相同
动态invertory(动态的主机纳管)
在工作中需要买很多的数据库服务器,而这些服务器有很大的主机信息,为了节省时间用动态的invertory来
管理,是为了解决静态invertory复制出错和漏掉一些信息。
不同于静态invertory,动态invertory文件需要先设置文件的可执行标志如果需要将多个invertory文件放在一起使用,有可能就需要动态invertory和静态invertory文件任意结合使
用,只需要将这些invertory文件放在一个目录中即可
修改ansible目录
ansible.cfg目录:/etc/ansible/ansible.cfg
ansible all -i ~/project/inventoies/ -m ping
3.2在命令行中执行Ansible
在了解主机资源的配置的方法之后,我们先把/etc/ansible/hosts中的内容清空,加入下面的信息,表明我们
需要使用用户名root,密码为123456,的账号去对ip地址为192.168.40.126的主机进行操作:
192.168.40.126 ansible_ssh_user=root ansible_ssh_pass=123456
配置完成后,输出第一条命令
ansible all -a “echo HelloWord”
我们期望目标主机返回HelloWord的结果,但是Ansible的执行会卡在询问是否记录 SSH 密钥的位置(如
果)
3.2.1指定目标主机
ansible -m <module_name> -a
指定所有主机:ansible all -m shell -a "uptime"
指定特定的主机组:ansible 'webserver:dbserver' -m shell -a "uptime"
排除指定的主机(排除webserver组中所有在phoenix组的主机中
):ansible 'webservers:!phoenix' -m shell -a "uptime"
指定同时存在与两个组中的主机(操作同时存在于webserver和staging两个组中的主机):
ansible 'webservers:&stagting' -m shell -a "uptime"
复合条件:
ansible webserver:dbservers:&staging:!phoenix -m shell -a "uptime"
!(非)
采用正则表达式指定主机
ansible 'one*.com:dbservers' -m shell -a "uptime"
3.2.2 常用命令示例
1.命令执行
重启主机
ansible all -a "/sbin/reboot" -f 10
shell模块
ansible all -m shell -a 'echo $TERM'
底层SSH模块
ansible all -m raw -a "hostame --fqdn"
2.文件操作
下发文件
ansible all -m copy -a "src=/etc/hosts dest=/tmp/hosts"
为文件赋予指定的权限
ansible all -m file -a "dest=b.txt mode=600 owner=demo group=demo"
创建文件夹
ansible all -m file -a "dest=/path/to/c mode=644 owner=mdehann group=mdehann state=directory"
删除文件
ansible all -m file -a "dest=/path/to/c state=absent"
3.包管理模块
使用YUM源进行安装
ansible webservers -m yum -a "name=acme state=installed"
安装指定版本的包
ansible webserver -m yum -a "name=acme-1.5 state=installed"
安装最新版本的安装包
ansible webserver -m yum -a "name=acme state=latest"
卸载安装包
ansible webserver -m yum -a "name=acme state=removed"
4.用户管理
新增用户
ansible all -m user -a "name=dulei password=123456"
删除用户
ansible all -m user -a "name=foo state=absent"
5.版本管理
使用Git拉去文件
ansible all -m git -a "repo=git://demo/repo.git dest=/srv/myapp version=HEAD"
6.服务管理
启动服务
ansible webserver -m service -a "name=httpd state=started"
重启服务
ansible webserver -m service -a "name=httpd state=restarted"
停止服务
ansible webserver -m service -a "name=httpd state=stopped"
7.后台管理
启动一个执行360秒的后台作业
ansible all -B 360 -a "/usr/bin/long-Running_operation --do-stuff"
检查作业状态
ansible all -m async_status -a "jid=1311"
后台运行1800秒,每60秒检查一次作业状态
ansible all -B 1800 -P 60 -a "/usr//bin/long_running_operation --do-stuff"
8.设备状态查询
获取设备的信息列表
ansible all -m setup
3.3 Ansible常用模块
ansible有很多模块可供我们使用,可以使用ansible-doc -1命令查看ansible内置模块
说明:ansible2.10开始就调整了ansible模块和插件的发布方式,引入ansible collections的概念,将单一的
模块库分类归到了collection中
3.3.1 文件管理模块
1.文件组装模块-assemble
ansible模块用于把多份配置文件片段组装成一份配置文件,当我们需要对不同的主机分配不同的配置文件
时,可以考虑用这个模块
例如,将/root/demo 下的片段文件组装后放到/root/target下
ansible all -m assemble -a 'dest=/root/target src=/root/demo'
2.文件复制模块——copy
3.3.2命令执行模块
1.命令执行模块——command
command模块用于给定的主机上执行名临汾。command模块执行的命令是获取不到$HOME这样的环境变
量。
ansible all -m command2.command模块的增强——shell
command模块是不支持运算符的,也不支持管道这样的操作符。
ansible all -m command -a 'ps -ef |grep mysql'
执行这个命令之后ansible会发出不支持操作符的提示,而shell模块就会支持常见的语法相关能力,可以视为
对command模块的功能增强
ansible all -m shell -a 'ps -ef |grep mysq'
3.脚本执行模块——script
可以通过ansible下发到目标主机上,在使用script模块执行脚本。执行的脚本要在管理主机上存在的脚本。
ansible all -M script -a '/root/demo/inspection.sh'
4.SSH命令执行模块——raw
raw是通过ssh的方式而不是通过python的方式对目标主机进行操作
ansible all -m raw -a 'ip a'
3.3.3网络相关模块
1.下载 模块——get_url
ansible all -m get_url -a 'dest=/root url=http://www.python.com.cn'
2.WEB请求模块——uri
uri模块主要用于发送HTTP协议,通过使用uri模块,我们可以让目标主机向指定的网站如Get,Post这样的
HTTP请求,并且能得到返回的状态码。
3.3.4 代码管理模块
1.git
当我们需要将文件集中下发的时候,除了可以用copy这样的方式,其实还可以用源码管理模块来实现集
中式版本管理工具(分布式)
3.3.5包管理模块
ansible all -m apt -a 'name=openjdk-8-jdk state=latest install_recomends=no'
1.yum
ansible all -m yum -a 'name=httpd state=latest'
3.3.6系统管理模块
1.计划任务管理模块——cron模块
在linux中使用crontab实现定时计划任务
在/etc/crontab文件,每一行都代表一个任务,每个字段代表一项设置,有七个字段,前五个字段是时间设
定段,第六个字段是要执行的命令段。
ansible all -m cron -a 'name=demo hour=8 job= mysqldump -uroot -pxxxx demo>demo.sql'
2.用户组的管理模块——group
是由group模块可以对主机进行批量的用户组添加或者删除错走
ansible all -m group -a 'name=zabbix state=present(删除absent)'
3.用户管理模块——user
user模块可以用于对目标主机进行批量的用户管理操作
ansible all -m user -a 'name=zabbix state=present remo=yes'
4.服务管理模块——service
service模块可以用于目标主机进行批量的服务管理操作
ansible all -m service -a 'name=httpd state=restartd'
5.系统信息模块——setup
setup模块可以获取主机的更多信息
ansible all -m setup
3.3.7 文档动态渲染与配置模块
在工作过程中根据主机信息的变化对应的调整在这台主机上部署的应用系统的部分配置信息,实时调整文件
内容。ansible推荐使用template模块对配置文件或信息采集文件进行动态渲染