ansible常用操作

2023-06-17  本文已影响0人  何亮hook_8285

ansible命令格式:

ansible [匹配主机] [-i 指定hosts文件路径] -m 指定模块 -a "模块参数" [其他选项]
其他选择 -v/-vv/-vvv  --diff --check -f -e -P -B -b|-become

ansible常用命令

#发送ping命令
ansible all -i host -m ping
#向远端发送一个shell命令,center表示hosts中的标识
ansible center -i hosts -m shell -a "whoami" -become
#比对本地的hosts和远端host差别,center表示hosts中的标识
ansible center -i hosts -m copy-a "src=/etc/hosts dest=/etc/hosts"  --diff --check
#向远端发送一个ping的命令,并且将返回值data改成hello,center表示hosts中的标识
ansible center -i hosts -m ping -a "data={{hello_data}}" -e "hello_data='hello'"

ansible的hosts文件内容示例

[center]  定义组
master 定义组内节点
[nodes]
ceph-[1:3] 组名称连续的值
[nodes:vars] 定义组的变量
ansible_ssh_user=root
ansible_ssh_pass=123
ansible_ssh_connection=ssh

[center:vars]
ansible_connection=local

ansible常用模块

最频繁使用 ping  shell(远程执行命令) copy(文件服务到远程) script(远程执行脚本) file(文件管理)
常用模块 systemd(服务管理) linefile(上传内容) temlpate(模板文件)
好用模块 cron(远程定时任务) hostname(远程给主机改名) get_url(下载一个url地址文件)

ansible的playbook语法

playbook是yml文件格式

hosts: #ceph-1 这个杠表示数组
gather_facts:false #不用执行setup模块,不会采集设备信息
vars: #设置初始化变量
test_item_vars:

- "hello"
- "hello2"
tasks: 指定任务级
- name: test simple for one
debug:
    msg: "item={{item}}"一个任务ansible
with_list:
  - "hello"
  - "world"

playbook语法组成

playbook语法-角色->(变量设置、register、判断语法、循环语句、delegate_to、等待语句)->过滤器 模块

角色

roles  角色根目录
|-test-variable 对应角色目录
  |-defaults
    |- main.yml
  |-task
    |-main.yml
  |-vars
    |-main.yml
角色目录说明
defaults:存放改角色的变量
tasks:存放task任务的yaml
templates:存放template文件
vars:存放变量
handler:存放通知任务

ansible文档

https://docs.ansible.com/ansible/latest/dev_guide/sidecar.html

ansible参考项目ceph-ansible

执行命令

host文件
[webs]
center
sp-3

[nginx]
sp-1
sp2

[nodes1]
sp-[1:10] #生成规则1-10  ,sp-1,sp-2
sp-[001:200] #生成规则sp-001 sp-002
sp-[1:10]-[1:2] #生成规则sp-1-1

[nodes:vars] #分组下的变量
ansible_ssh_port=22
ansible_ssh_user=root
ansible_ssh_pass=qqqqqq

[webserver] 主机变量
www.mm.com http port=80

[webserver:children]  组嵌套
nodes1
webs

[tbj]
center

[tbj:vars] #分组下所有节点变量
ansible_connection=local

ansible webs,nginx -i hosts -m shell -a "whoami"
#查看ansible变量
ansible sp-1,sp-2 -i hosts -m debug -a "msg={{ansible_ssh_port}}"

ansible [pattern] -i hosts
pattern 匹配执行的ansible节点 组名、主机名、all、多个组或主机
-i hosts:指定hosts文件存在哪里
-m 模块,默认command
-a 模块参数 copy


###############执行命令###############
#执行命令
ansible nodes -i hosts -m shell -a "ls /root/"

#执行复制命令,复制到远程机会比对文件是否更新,如果比对结果是更新则覆盖文件
ansible nodes -i hosts -m copy -a "src=hosts dest=/root/host"

#state=absent 表示放弃文件,就是删除
ansible nodes -i hosts -m file -a "path=/root/hosts state=absent"

#--diff --check 比对差异,并不会上传
ansible nodes -i hosts -m copy -a "src=hosts desct=/root/hosts" --diff --check


###########剧本级playbook,简化重复操作###########
- hosts: nodes #hosts文件组名称,表示这个组执行这些命令,剧本1
  gather_facts: false
  tasks:
   - name: upload hosts file
     copy:
       src: hosts
       dest: /root/hosts
   - name: run command on remote
     shell: pwd
     args:
       chdir: /tmp #命令执行目录
       creates: /root/hosts #该文件存在则不执行,removes这个命令存在执行
   - name: debug a message
     debug:
       msg: "hello"
- hosts: center #剧本2
  gether_facts: false
  tasks:
     - name: run command on remote
       shell: hostname
- hosts: nginx #剧本2
  gether_facts: false
  vars:
   msg: "hello
  tasks:
     - name: run command on remote
       shell: "{{msg}}---aaa"
#执行playbook命令 
ansible-playbook -i hosts test.yml

#测试creates参数 该文件存在则不执行
ansible nodes -i hosts -m shell -a "pwd creates=/root/hosts"
#测试removes 该文件存在执行
ansible nodes -i hosts -m shell -a "pwd removes=/root/hosts"
#chdir 是指定pwd命令运行路径
ansible nodes -i hosts -m shell -a "pwd removes=/root/hosts chdir=/tmp"

#查看匹配的节点
ansible nodes -i hosts --list-hosts
#指定最大进程数 -f --fork 默认5个
ansible nodes -i hosts -f 25
##hosts的文件内置变量
ansible_connection=local
ansible_ssh_port=22
ansible_ssh_user=test
ansible_ssh_pass=test.1234!
ansible_sudo_pass=test.1234!

变量定义和优先级(命令>hosts文件>playbook剧本文件)

1.使用-e 传递变量

ansible center -i hosts  -m shell -a "pwd" -e "msg=hello"

2.使用hosts文件传递变量

[all:vars]

msg=hello

3.剧本文件

vars:

  msg: "hello

roles目录结构

image.png
ansible roles 目录规范 - 明天OoO你好 - 博客园 (cnblogs.com)

模块概览:

命令和脚本模块:
command模块,ans默认的模块,执行简单命令,不支持特殊符号;
shell模块,执行命令,支持特殊符号;
script模块,分发脚本并执行;
文件模块:
file,创建目录,文件,软链接;
copy,远程分发文件,修改权限,所有者,备份;
服务模块:
systemd,服务管理;
service,服务管理(老系统用);
软件包管理模块:
yum源,yum_repository,
yum命令,
get_url下载软件;
系统管理模块:
mount模块,挂载;
cron模块,定时任务;
用户管理模块:
group模块,管理用户组;
user模块,管理用户;
用于调试模块:ping 模块检查 ,ansible与其他节点连通性。
debug模块,用于检查/显示 变量
其他Linux模块:压缩解压(unarchive),rsync模块(synchronize),数据库模块(mysql_db,mysql_user)...
其他工具模块:ansible管理docker | k8s | zabbix | grafana...

在Ansible中,编写Playbook时,支持给task定义一个或多个Tag标签,我们在执行Playbook时,可以使用-t参数,根据Tag来确定具体执行什么任务。

---
- hosts: exp
  remote_user: root
  tasks:
  - name: Install Apache
    yum: name=httpd state=installed
    tags: httpd
  - name: Install Nginx
    yum: name=nginx state=installed
    tags: nginx
  - name: Stop firewalld & iptables & selinux
    shell: systemctl stop firewalld ; iptables -F ; setenforce 0
    tags:
    - httpd
    - nginx
ansible-playbook tag.yml -t httpd

二、Ansible中Handler的定义和调用
Ansible的Playbook还支持使用Handler,所谓Handler,就是一种特殊的task,这种task总是在所有的task执行完成后再执行。Ansible中带有Handler的Playbook示例如下:

---
- hosts: exp
  remote_user: root
  tasks:
  - name: Install Apache
    yum: name=httpd state=installed
  - name: Config Apache
    copy: src=httpd.conf dest=/etc/httpd/httpd.cong
    notify: Restart Apache
  - name: Stop firewalld
    shell: systemctl stop firewalld;iptables -F;setenforce 0;
  handlers:
  - name: Restart Apache
    service: name=httpd state=restarted

vi /etc/ansible/ansible.cfg
host_key_checking = False
上一篇 下一篇

猜你喜欢

热点阅读