Ansible automated operation and maintenance - basic configuration Ansible environment, Inventory file configuration, Ansible and regular, Ansible-doc module

A, Ansible Installation and Configuration

1, are familiar with and use Ansible

lab environment

  • Three virtual machines can access
  • ip information
Host Information (rhel7.5) Host function
server1(172.25.37.1 ) Master node
server2(172.25.37.2 ) Charged node 1
server3(172.25.37.3) Charged node 2

1, the installation configuration of the network ansible yum source (aliyun) on server1 (master node)


[epel]
name=epel
baseurl=https://mirrors.aliyun.com/epel/7/x86_64/
gpgcheck=0

Here Insert Picture Description
Here Insert Picture Description

2, view the ansible profile
Here Insert Picture Description

  • When you run Ansible command, the command will find the configuration file in accordance with a predetermined order

    1) ANSIBLE_CONFIG : First, Ansible command checks the environment variable, and this environment variable to point to a configuration file

    2) ./ansible.cfg : Secondly, it will check the current configuration file in the directory ansible.cfg

    3) ~ / .ansible.cfg : once again, will check the current .ansible.cfg configuration file in the user's home directory

    4) /etc/ansible/ansible.cfg : Finally, check the configuration file will be automatically generated when you install the package management tool with Ansible

3、定义Inventory(主机列表), 在/etc/ansible/hosts中写入被管控主机

Here Insert Picture Description
Here Insert Picture Description
4、调用ping模块来检测网络是否可达

执行命令 ansible test -m ping(参数-m:表示要执行的模块,默认为command)

  • 报错:这是因为没有输入被管控主机的连接密码,因为ansible执行动作都是先ssh到此被管控主机然后执行动作的

Here Insert Picture Description

  • 下列报错是因为用户ssh访问时,会把每个访问过主机的公钥(public key)都记录在/.ssh/known_hosts。当下次访问相同计算机时,OpenSSH会核对公钥。当root用户第一次登陆某主机时, /.ssh/known_host中无此主机的公钥,所以登陆失败。

Here Insert Picture Description

执行命令 ansible test -m ping -k(-k:提示输入ssh登录密码,当使用密码验证的时候用)
Here Insert Picture Description

5、创建普通用户devops,执行分发任务
linux机器都禁止使用root远程登陆,更安全的做法是用普通用户登陆,分发任务。

(1)在ansible主机上和server2和server3上创建普通用户devops,用于ansible任务分发与执行。
保证ansible与hosts主机上devops用户id相同

Here Insert Picture Description

Here Insert Picture Description

(2)在server2和server3上对普通用户devops下方权力 visudo

Here Insert Picture Description
Here Insert Picture Description

(3)在ansible主控节点生成ssh公私钥文件,并分发到其他机器

ssh-keygen  #生成密钥
ls 用户家目录/.ssh/#查看密钥
ssh-copy-id server2
ssh-copy-id server3

Here Insert Picture Description
Here Insert Picture Description

Here Insert Picture Description

Here Insert Picture Description

测试已经可以免密登录
Here Insert Picture Description

也可以通过更改配置文件实现免密

Here Insert Picture Description
Here Insert Picture Description

(4)在ansible主控节点,普通用户devops的家目录下,编辑ansible.cfg配置文件

Here Insert Picture Description

[defaults]
inventory = ./hosts  #inventory文件路径

Here Insert Picture Description

(5)编辑hosts文件
ansible.cfg

Here Insert Picture Description

(6)检测文件配置是否成功:

ansible test -m ping
	#对test组内主机调用ping模块
ansible db -m ping
	#对db组内主机调用ping模块
ansible all -m ping
ansible * -m ping
	#对所有组内主机调用ping模块

Here Insert Picture Description
Here Insert Picture Description

Here Insert Picture Description

(7)使用-b连接hosts主机,拥有hosts主机上root(默认)用户的权限

ansible test -m copy -a "src=test.sh dest=/tmp/test.sh"
Here Insert Picture Description
Here Insert Picture Description

ansible test -m copy -a "src=test.sh dest=/mnt/test.sh"
Here Insert Picture Description
不使用-b进行模块copy时,会因权限问题报错,文件复制不过去,使用-b,文件传输到hosts主机上时切换身份,调用sudo,执行其权力。
Here Insert Picture Description
ansible test -m copy -a “src=test.sh dest=/mnt/test.sh” -b 使用-b连接hosts主机,拥有hosts主机上root(默认)用户的权限

Here Insert Picture Description
ansible test -m copy -a “src=test.sh dest=/mnt/test.sh” -u devops -b -u 指定ssh连接的用户名,连接到hosts主机后,身份为指定用户,指定用户必须存在

当然,每次输入命令-b、-u指定用户权限太麻烦,解决方法:在ansible.cfg文件中添加配置

remote_user: devops      
	#在远程主机上以devops用户身份执行任务

become: True                    
	#是否允许身份切换
become_method: sudo   
	#切换用户身份的方式,有sudo、su、pbrun等方式,默认为sudo,被控主机为centos的话需要设置become_method为su
become_user: root          
	#切换成什么用户身份,默认为root。设置为root账户,相当于我们以普通账户登入到远程主机时,再使用su - root切换为root账户。
ansible_become_pass=False 
	#使用root账户,则这里要写的就是root账户的密码,此处设置为Flase因为已经做过免密登陆

Here Insert Picture Description
测试
在hosts主机上visudo中设置的devops用户权限,是为了当ansible使用devops身份调用hosts主机后在远程主机上以devops用户身份切换到root用户,通过sudo命令,切换身份。
Here Insert Picture Description

二、Ansible命令

  • Ansible命令的使用场景
    非固化需求;临时一次性操作;二次开发接口调用

1、 Ansible命令的简单示例

1)查看被管控主机主机名:ansible test -a hostname -k(-a 指定模块的参数)
Here Insert Picture Description
2)查看被管控主机挂载:ansible test -a df -k

3)查看被管控主机运行时长:ansible test -a uptime -k
Here Insert Picture Description

4)在被管控主机执行脚本test.sh:
Here Insert Picture Description

首先使用copy模块将test.sh文件复制到每个被管控主机:
ansible test -m copy -a “src=test.sh dest=/tmp/test.sh” -k

  • dest:必选项。要将源文件复制到的远程主机的绝对路径,如果源文件是一个目录,那么该路径也必须是个目录。
  • src:被复制到远程主机的本地文件,可以是绝对路径,也可以是相对路径。如果路径是一个目录,它将递归复制。在这种情况下,如果路径使用“/”来结尾,则只复制目录里的内容,如果没有使用“/”来结尾,则包含目录在内的整个内容全部复制,类似于rsync。
  • mode 文件或目录复制到远程主机后,所拥有的权限
  • shell模块:让远程主机在shell进程下执行命令,从而支持shell的特性,如管道等

Here Insert Picture Description
再使用shell模块执行文件 ansible test -m shell -a “/tmp/test.sh” -k
Here Insert Picture Description
发现报错,这是因为复制到 被管控主机的test.sh脚本没有执行权限

Here Insert Picture Description

重新给权限复制:
ansible test -m copy -a “src=test.sh dest=/tmp/test.sh mode=755” -k
Here Insert Picture Description
Here Insert Picture Description

参数 功能
-m 要执行的模块,默认为command
-a 指定模块的参数
-u ssh连接的用户名,默认用root,ansible.cfg中可以配置
-b,–become 变成那个用户身份,不提示密码
-k 提示输入ssh登录密码,当使用密码验证的时候用
-s sudo运行
-U sudo到哪个用户,默认为root
-K 提示输入sudo密码,当不是NOPASSWD模式时使用
-C 只是测试一下会改变什么内容,不会真正去执行
-c 连接类型(default=smart)
-f fork多少进程并发处理,默认为5个
-i 指定hosts文件路径,默认default=/etc/ansible/hosts
-I 指定pattern,对已匹配的主机中再过滤一次
-list-host 只打印有哪些主机会执行这个命令,不会实际执行
-M 要执行的模块路径,默认为/usr/share/ansible
-o 压缩输出,摘要输出
–private-key 私钥路径
-T ssh连接超时时间,默认是10秒
-t 日志输出到该目录,日志文件名以主机命名
-v 显示详细日志

三、Inventory文件配置

Inventory是Ansible管理主机信息的配置文件,相当于系统/etc/hosts文件的功能,默认存放在/etc/ansible/hosts

  • inventory file可以有多个,且也可以通过Dynamic Inventory来动态生成

1、定义主机和组
1)Inventory可以直接为IP地址

172.25.37.2

2)端口号不是默认22端口时,可明确的表示为:

server2:2222
server2 ansible_port=2222 ansible_host=172.25.37.2

3)中括号表示一个组,也可以表示一个范围

[webserver]
www[1:10].example.com
	 #表示主机范围www1~www10
db-[a:f].example.com
	#表示主机范围db-a~db-f

2、定义主机变量
在playbook中使用时对主机进行个性化定制

[webserver]
web1 http_port=8080 maxRequestsPerChild=1024

3、定义组变量

[webserver]
server2
server3
[webserver:vars]
ntp_server=time1.aliyun.com
	#webserver组中的所有主机ntp_server值为time1.aliyun.com

4、定义组嵌套及组变量
组与组之间可以相互调用,并且可以向组中的主机指定变量。不过,这些变量只能在Ansible-playbook中使用,而Ansible不支持

[apache]  
server1
server2
[nginx]  
server3
server4   
[webservers:children]  
apache  
nginx 
[webservers:vars]  
ntp_server=time1.aliyun.com

5、在inventory 主文件中保存所有的变量并不是最佳的方式.还可以保存在独立的文件中

这些独立文件的格式为 YAML

/etc/ansible/group_vars/raleigh
/etc/ansible/group_vars/webservers
/etc/ansible/host_vars/foosball

当变量变得太多时,分文件定义变量更方便进行管理和组织

/etc/ansible/group_vars/raleigh/db_settings
/etc/ansible/group_vars/raleigh/cluster_settings

6、变量的检索位置

Inventory配置文件(默认/etc/ansible/hosts);Playbook中vars定义的区域;Roles中vars目录下的文件;Roles同级目录group_vars和hosts_vars目录下的文件
大家设置变量时尽量沿用同一种方式,以方便维护人员管理.

四、Ansible与正则

1、匹配所有主机,all或*号功能相同

ansible all -m ping
ansible * -m ping

2、对多台主机或多个组同时执行,相互之间用冒号分隔即可

ansible "web1:web2" -m ping

3、在webserver组但不在database组的主机,用感叹号表示

ansible  "webserver:!database" -m ping

4、在webserver组和database组中同时存在的主机,用&符号表示

ansible  "webserver:&database" -m ping

5、模糊匹配

*.example.com
www*.com:database

Here Insert Picture Description

Here Insert Picture Description

在test组但不在db组的主机,用感叹号表示 ansible “test:!db” -m ping

  • 注意:使用特殊符号!时,必须使用’'强引用符

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

五、Ansible-doc与模块

ansible-doc是与man功能类似的说明工具

1、Ansible-doc命令

  1. 显示所有可用模块: ansible-doc -l
  2. 获取某模块的用法: ansible-doc 模块名

2、借助ansible-doc命令查看httpd模块用法

1) yum模块

  • 在线安装 ansible test -m yum -a "name=httpd state=present"
  • 卸载软件 ansible webservers -m yum -a "name=httpd state=absent"

2) service模块
Here Insert Picture Description

开启httpd服务: ansible test -m service -a "name=httpd state=started enabled=yes"

Here Insert Picture Description
在server2上查看httpd服务的状态

Here Insert Picture Description

3) firewalld模块

  • 开启firewalld防火墙: ansible test -m service -a "name=firewalld state=started enabled=yes"
  • 在防火墙上添加http服务: ansible test -m firewalld -a "service=http permanent=yes state=enabled"

4)copy模块

Here Insert Picture Description
远程主机apache默认发布目录下传输默认发布文件 ansible test -m copy -a "src=apache/index.html dest=/var/www/html/"
Here Insert Picture Description

Here Insert Picture Description
5)uri模块

uri module is adapted to send an HTTP protocol, by using uri module, allowing target host, such as Get transmitted to the specified site, this HTTP request Post, and can obtain the status code returned.
On the WWW, each has a unified information resource and the only Internet address, the address is called the URL (Uniform Resource Locator, uniform resource locator), which is the uniform resource locator signs WWW, it refers to the network address.

ansible test -m uri -a "url=http://172.25.1.2"Here Insert Picture Description

Execution returns a value ansible test -m uri -a "url=http://172.25.1.2 return_content=yes"
Here Insert Picture Description

Published 102 original articles · won praise 21 · views 5326

Guess you like

Origin blog.csdn.net/ranrancc_/article/details/103116109