day50-Ansible基础知识

1.什么是ansible

可以通过一个命令行完成一系列的操作

2.ansible优点和特点

(1)优点:
①批量执行远程命令
②批量配置软件服务
③实现软件开发功能
④编排高级的IT任务
(2)特点
①容易学习,无代理模式
②操作灵活
③简单易用
④安全可用
⑤移植性高

3.ansible 基础架构---控制端 被控端 inventory ad-hoc playbook 连接协


day50-Ansible基础知识_第1张图片
Image 了.png

4.ansible 配置文件 优先级

ANSIBLE_CONFIG
ansible.cfg ---当前项目目录中
.ansible.cfg ---当前执行用户的家目录

/etc/ansible/ansible.cfg
例题:

[root@manager ~]# export
ANSIBLE_CONFIG="/tmp/ansible.cfg"
[root@manager ~]# touch /tmp/ansible.cfg
[root@manager ~]# mkdir /project1
[root@manager ~]# cd /project1/
[root@manager project1]# touch ansible.cfg
[root@manager project2]# ansible --version
ansible 2.8.5
config file = /project1/ansible.cfg
[root@manager /]# mkdir /project2
[root@manager /]# cd /project2/
[root@manager project2]# touch ansible.cfg
[root@manager project1]# ansible --version
ansible 2.8.5
config file = /project2/ansible.cfg
[root@manager tmp]# touch ~/.ansible.cfg
[root@manager tmp]# ansible --version
ansible 2.8.5
config file = /root/.ansible.cfg

5.ansible inventory主机清单

(1)基于ip地址+密码的方式

yum install ansible -y
mkdir projectl 
cd projectl 
cp /etc/ansible/ansible.cfg ansible.cfg
vi ansible.cfg
host_key_checking = False
vi hosts
[webservers]
172.16.1.7 ansible_ssh_user='root'
ansible_ssh_pass='1'
172.16.1.8 ansible_ssh_user='root'
ansible_ssh_pass='1'

(2)基于密钥连接,需要先创建公钥和私钥,并下发公钥至被
控端

ssh-keygen t rsa -C"111"
[root@manager ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub
[email protected]
[root@manager ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub
[email protected]

①主机+端口+密钥

[root@manager ~]# cat hosts
[webservers]
172.16.1.7
172.16.1.8
ansible webservers --list-hosts  -i hosts(查看成员)
例如:[root@manager project1]# ansible webservers --listhosts -i hosts
hosts (2):
172.16.1.7

ansible webservers -m ping -i hosts(ping客户机通不通)

(3)主机组使用方式

[lbservers] #定义lbservers组
172.16.1.5
172.16.1.6
[webservers] #定义webserver组
172.16.1.7
172.16.1.8
[servers:children] #定义servers组包括两个子组
[lbservers,webserver]
lbservers
webserver

Ansible Ad-Hoc

6.ansible ad-Hoc 单条命令

Image ansible.png

(1)模块名称

command (执行命令 默认 不支持管道)
shell (执行命令 支持管道)
yum_reposity (yum仓库配置)
yum (yum安装软件)
get_url (和linux的wget一致)
copy (拷贝配置文件)
service|systemd (启动服务)
user(创建用户)
group(创建组)
file (创建目录 创建文件 递归授权)
mount (挂载)
cron (定时任务)
firewalld (防火墙)
selinux (selinuix)

7.command

ansible webservers -a "ps axu|grep nginx" -i hosts
#不支持管道(简单命令)

8.shell

ansible webservers -m shell -a "ps axu|grep nginx" -
i hosts #支持管道

9.yum

state:
present 安装
absent 卸载
latest 最新
enablerepo #指定使用按个仓库
disablerepo #排除使用哪个仓库

①安装最新的httpd服务

[root@manager project1]# ansible webservers -m yum
-a "name=httpd state=latest disablerepo=webtaticphp" -i hosts

②移除httpd服务

[root@manager project1]# ansible webservers -m yum
-a "name=httpd state=absent disablerepo=webtaticphp" -i hosts

③安装httpd指定从按个仓库安装

- name: install the latest version of Apache from
the testing repo
[root@manager project1]# ansible webservers -m yum
-a "name=httpd state=latest enablerepo=testing" -i
hosts

④通过URL方式进行安装

[root@manager project1]# ansible webservers -m yum
-a
"name=https://mirrors.aliyun.com/zabbix/zabbix/3.0/
rhel/7/x86_64/zabbix-agent-3.0.0-1.el7.x86_64.rpm
state=present disablerepo=webtatic-php" -i hosts

⑤软件包必须在被控端主机

- name: install nginx rpm from a local file (软件包
必须在被控端主机)
[root@manager project1]# ansible webservers -m yum
-a "name=/root/zabbix-agent-4.0.0-2.el7.x86_64.rpm
state=present disablerepo=webtatic-php" -i hosts

10.copy

src ---本地路径,可以是相对,可以是绝对
dest ---目标位置
owner ---属主
group ---属组
mode ---权限
backup ---备份

例题:

①[root@manager project1]# ansible webservers -m copy
-a "src=./file/ansible.oldxu.com.conf
dest=/etc/nginx/conf.d/ansible.oldxu.com.conf
owner=root group=root mode=644" -i hosts
②[root@manager project1]# ansible webservers -m copy
-a "src=./file/ansible.oldxu.com.conf
dest=/etc/nginx/conf.d/ansible.oldxu.com.conf
owner=root group=root mode=644 backup=yes" -i hosts

11.service|systemd

state
started #启动
stopped #停止
restarted #重启
reloaded #重载
enabled #是否开机自启
yes #是
no #否

例题:

[root@manager project1]# ansible webservers -m
systemd -a "name=nginx state=restarted enabled=yes"
-i hosts

12.file

* 创建 /code/ansible
path ---路径
state
touch ---创建文件
directory ---创建目录
owner ---属主
group ---属组
mode ---权限

例题:
①准备站点

[root@manager project1]# ansible webservers -m file
-a "path=/code/ansible state=directory mode=755
owner=www group=www" -i hosts

②准备站点代码

[root@manager project1]# ansible webservers -m copy
-a "src=./file/index.html
dest=/code/ansible/index.html owner=www group=www
mode=644" -i hosts

13.user group

(1)group 整数int 小数 flot dasdsa str 真|假
bool
例题:

[root@manager project1]# ansible webservers -m
group -a "name=www gid=666 state=present" -i hosts

(2)user

name #名称
uid #uid
group #组名或gid
create_home #是否创建家目录
system #是否作为系统组
shell #指定登录shell
state
present
absent
remove
groups
append
password

例题:
① 程序使用 www 666 666 /sbin/nologin /home
-->无

[root@manager project1]# ansible webservers -m user
-a "name=www uid=666 group=666 create_home=no
shell=/sbin/nologin state=present" -i hosts

②正常用户 oldxu 1000 1000 /bin/bash
/home/oldxu

[root@manager project1]# ansible webservers -m user
-a "name=oldxu" -i hosts

③移除oldxu用户,并删除家目录所有内容.

[root@manager project1]# ansible webservers -m user
-a "name=oldxu state=absent remove=yes" -i hosts

④ 创建 other用户.有两个附加组root bin,创建家目录,指定登录
shell,设定密码123

生成一个密码
ansible all -i localhost, -m debug -a "msg={{ '123'
| password_hash('sha512', 'mysecretsalt') }}"
[root@manager project1]# ansible webservers -m user
-a 'name=other groups='root,bin' create_home=yes
shell=/bin/bash
password="$6$mysecretsalt$gIIYs0Xgc7sSQkH.zKaz8/Afa
MomYzR1QZYtccwmJcUt8VpLq4D055UCCX4MlwgePOP80ZRwhppv
BF72RIAVi/"' -i hosts

14. mount

例题:
(1)提前准备好nfs服务端

[root@web01 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data/zrlog 172.16.1.0/24
/data/zh 172.16.1.0/24
/data/edu 172.16.1.0/24
/data/blog 172.16.1.0/24

(2)用管理端操作被控端,让被控端挂载nfs存储数据

present #写入/etc/fstab
absent #卸载/etc/fstab
mounted #临时挂载
unmounted #卸载当前挂载

①挂载过程中,如果目录不存在,则会创建该目录

[root@manager project1]# ansible webservers -m
mount -a "src=172.16.1.31:/data/zrlog
path=/test_zrlog fstype=nfs opts=defaults
state=mounted" -i hosts
[root@manager project1]# ansible webservers -m
mount -a "src=172.16.1.31:/data/zrlog
path=/test_zrlog fstype=nfs opts=defaults
state=unmounted" -i hosts

15.cron

minute --分
hour ---时
day --日
month ---月
week ---周
job 

例题:

[root@manager project1]# ansible webservers -m cron
-a 'name=test_job minute=00 hour=02 job="/bin/bash
/server/scripts/client_to_data_server.sh
&>/dev/null"' -i hosts
[root@manager project1]# ansible webservers -m cron
-a 'name=test job="/bin/bash
/server/scripts/test.sh &>/dev/null"' -i hosts
[root@manager project1]# ansible webservers -m
cron -a 'name=test job="/bin/bash
/server/scripts/test.sh &>/dev/null" state=absent'
-i hosts

16.firewalld

例题:

[root@manager project1]# ansible webservers -m
systemd -a "name=firewalld state=started" -i hosts

①针对服务

[root@manager project1]# ansible webservers -m
firewalld -a "service=http state=enabled" -i hosts

②针对端口

[root@manager project1]# ansible webservers -m
firewalld -a "port=9999/tcp state=enabled" -i hosts

17.selinux

例题:

[root@manager project1]# ansible webservers -m
selinux -a "state=disabled" -i hosts

你可能感兴趣的:(day50-Ansible基础知识)