ansible

11、主机上脚本监控目标主机
[root@test41 opt]# vim test.sh 
#!/bin/bash
echo "hello eord" > /opt/test1.txt
​
[root@test41 opt]# ansible 192.168.65.10 -m script -a "/opt/test.sh"
[root@test1 opt]# cat test1.txt 
hello eord
​
#脚本在本机上执行目标主机
[root@test41 opt]# vim test.sh 
#!/bin/bash
df -h  > /opt/test2.txt
free -h > /opt/test3.txt
[root@test41 opt]# ansible 192.168.65.10 -m script -a "/opt/test.sh"
[root@test1 opt]# cat test2.txt 
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   36G  5.2G   31G   15% /
devtmpfs                 3.8G     0  3.8G    0% /dev
tmpfs                    3.9G     0  3.9G    0% /dev/shm
tmpfs                    3.9G   29M  3.8G    1% /run
tmpfs                    3.9G     0  3.9G    0% /sys/fs/cgroup
/dev/sda1               1014M  179M  836M   18% /boot
/dev/mapper/centos-home   18G   39M   18G    1% /home
tmpfs                    781M  4.0K  781M    1% /run/user/42
tmpfs                    781M   36K  781M    1% /run/user/0
/dev/sr0                 4.3G  4.3G     0  100% /run/media/root/CentOS 7 x86_64
[root@test1 opt]# cat test3.txt 
              total        used        free      shared  buff/cache   available
Mem:           7.6G        905M        4.9G         58M        1.9G        6.3G
Swap:          6.0G          0B        6.0G
​
12、setup模块

查看目标主机的一些信息

ip地址 cpu 内核版本 系统信息

#查看目标主机所有信息
[root@test41 opt]# ansible 192.168.65.10 -m setup 
#查看目标主机的cpu
[root@test41 opt]# ansible 192.168.65.10 -m setup -a 'filter=ansible_processor'
[root@test41 opt]# ansible 192.168.65.10 -m setup -a 'filter=ansible_*processor*'
192.168.65.10 | SUCCESS => {
    "ansible_facts": {
        "ansible_processor": [
            "0", 
            "GenuineIntel", 
            "12th Gen Intel(R) Core(TM) i5-12500H", 
            "1", 
            "GenuineIntel", 
            "12th Gen Intel(R) Core(TM) i5-12500H", 
            "2", 
            "GenuineIntel", 
            "12th Gen Intel(R) Core(TM) i5-12500H", 
            "3", 
            "GenuineIntel", 
            "12th Gen Intel(R) Core(TM) i5-12500H"
        ], 
        "ansible_processor_cores": 2, 
        "ansible_processor_count": 2, 
        "ansible_processor_threads_per_core": 1, 
        "ansible_processor_vcpus": 4, 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}
#查看内核版本
[root@test41 opt]# ansible 192.168.65.10 -m setup -a 'filter=ansible_proc_cmdline' 
192.168.65.10 | SUCCESS => {
    "ansible_facts": {
        "ansible_proc_cmdline": {
            "BOOT_IMAGE": "/vmlinuz-3.10.0-957.el7.x86_64", 
#查看内存
[root@test41 opt]# ansible 192.168.65.10 -m setup -a 'filter=ansible_mem*'
192.168.65.10 | SUCCESS => {
    "ansible_facts": {
        "ansible_memfree_mb": 4976, 
        "ansible_memory_mb": {
            "nocache": {
                "free": 6692, 
                "used": 1111
            }, 
            "real": {
                "free": 4976, 
                "total": 7803, 
                "used": 2827
            }, 
            "swap": {
                "cached": 0, 
                "free": 6143, 
                "total": 6143, 
                "used": 0
            }
        }, 
        "ansible_memtotal_mb": 7803, 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}
#查看系统信息
[root@test41 opt]# ansible 192.168.65.10 -m setup -a 'filter=ansible_system'
192.168.65.10 | SUCCESS => {
    "ansible_facts": {
        "ansible_system": "Linux", 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}
​

总结

command和shell

copy、yum、user

service服务模块,对服务进行管理

file模块文件属性进行修改。

hostname模块 改主机名

ping模块 主要测试能不能ping通

主机清单

主机组:IP地址

默认端口

不需要传密钥对可以在文件中直接写好
192.168.65.51 ansible_port=22 ansible_user=root ansible_password=123
写法二
这样就是对所有组都生效
192.168.65.51 
ansible_port=22 
#目标主机端口
ansible_user=root
#目标主机的用户名
ansible_password=123
#目标主机的密码

对多台主机进行控制(多网段配置)

#对10到50的目标主机进行控制
192.168.65.[1-5][0-9]

ansible的脚本 playbook脚本

playbook的组成:

1、Tasks 任务,每一个Tsaks就是一个模块

2、variables 变量 存储和传递数据,自定义变量,也可以是全局变量,也可以是脚本外传参

3、Templates模块,用于生成配置文件和多任务的编排

4、Handers处理器,满足某些条件时触发的操作,一般用于重启等操作

5、Roles 角色 组织和封装剧本的过程,角色可以把任务、变量、模板、处理器、组合成一个可用单元。

[root@test41 opt]# vim test1.yaml
​
- namea: first play
#定义剧本的名称
  hather_facts: false
#表示在执行剧本之前是否收集目标主机的信息,false就是不收集,加快执行速度不写就收集
  hosts: 192.168.65.20
#指定目标主机,可以是组名,也可以是ip地址
  remote_user: root
#在目标主机的执行用户
  tasks:
    - name: test connection
#定义任务名称
      ping:
#ping就是模块名称
    - name: close selinux
      command: '/sbin/setenforce 0'
      ignore_errors: True
#执行任务中报错,返回码为0,报错tasks就会停止,ignore_errors: True忽略
    - name: close firewalld
      service: name=firewalld state=stopped
#调用service模块关闭防火墙
    - name: install httpd
      yum: name=httpd state=latest
#lastest声明安装最新版的技术软件
    - name: interview
      shell: echo 'this is httpd' > /var/www/html/index.html
#指定shell模块,修改默认访问页面
      notify: restart httpd
#ansible在执行完任务之后不会立即执行这个重启,通过notify指令对应的名称传给触发器,
让触发器在任务的最后执行重启,避免在任务中多次执行重启影响执行的效率
  handlers:
     - name: restart httpd
       service: name=httpd state=restarted
​
#执行脚本
[root@test41 opt]# ansible-playbook test1.yaml 
​

#定义变量,引用变量 #脚本中定义,以及脚本传参

[root@test41 opt]# vim test1.yaml
#定义变量,引用变量
#脚本中定义,以及脚本传参
- name: second play
  hosts: 192.168.65.20
  remote_user: root
  vars:
    groupname: mysql
    username: nginx
#定义变量
  tasks:
    - name: create group
      group:
        name: "{{ groupname }}"
        system: yes
        gid: 306
    - name: create user
      user:
        name: "{{ username }}"
        uid: 306
        group: "{{ groupname }}"
[root@test41 opt]# ansible-playbook test1.yaml 
​

检查脚本是否出错

[root@test41 opt]# ansible-playbook test1.yaml --syntax-check
​
playbook: test1.yaml
​

检查脚本中有几个任务

[root@test41 opt]# ansible-playbook test1.yaml --list-task
​
playbook: test1.yaml
​
  play #1 (192.168.65.20): second play  TAGS: []
    tasks:
      create group  TAGS: []
      create user   TAGS: []
​

检查对哪个主机生效

[root@test41 opt]# ansible-playbook test1.yaml --list-hosts
​
playbook: test1.yaml
​
  play #1 (192.168.65.20): second play  TAGS: []
    pattern: [u'192.168.65.20']
    hosts (1):
      192.168.65.20
​

指定从哪个任务开始

#循环结构:absible有多种循环方式,般都命名为with items,定义循环的内容。
#with item 单循环输出:
name: item test
192.168.233.20hosts:
remote user:root
gather facts:false
tasks:
debug:
msg:"{fitem}}
with nested:
[a,b,c,d][1,2,3,4]#输出item的值,with items:abcd依次传入。#with list,整个列表做为一个整体,进行输出。#wiht together,做为整体,两两配对输出#with nested:每一层都是遍历执行一遍,输出结果。#条件判断,主机的ip#才会执行性 一次性创建4个文件,/opt/a /opt/b /opt/c /opt/d 循环 with items[root@test41 opt]# ansible-playbook test1.yaml --start-at-task='create user' -e 'username=test3 groupname=mysql'
指定从哪个开始,他上面的就会不执行,只执行他下面的
[root@test41 opt]# vim test1.yaml
​
#定义变量,引用变量
#脚本中定义,以及脚本传参
- name: second play
  hosts: 192.168.65.20
  remote_user: root
  vars:
    groupname: mysql
- name: second play
  hosts: 192.168.65.20
  remote_user: wbl
  become: yes
​
  bemote_user: root
  vars:
    groupname: mysql
#定义变量,引用变量
#脚本中定义,以及脚本传参
- name: second play
  hosts: 192.168.65.20
  remote_user: wbl
  become: yes
#先用普通用户再切换管理员
  bemote_user: root
  vars:
    groupname: mysql
    username: nginx1
#定义变量
  tasks:
    - name: create group
      group:
        name: "{{ groupname }}"
        system: yes
        gid: 16
    - name: create user
      user:
        name: "{{ username }}"
        uid: 167
        group: "{{ groupname }}"
​
​

[root@test41 opt]# vim test3.yaml
​
#如何在脚本中实现条件判断
#when  满足条件的主机就执行,不满足的跳过
- name: this is if
  hosts: all
  remote_user: root
  tasks:
    - name: test when
      debug: msg='条件满足'
#debug相当于echo  echo“条件判断”
      when: ansible_default_ipv4.address == "192.168.65.20"
​

#循环结构:absible有多种循环方式,一般都命名为with items,定义循环的内容。

[root@test41 opt]# vim test4.yaml
#with_item 单循环输出
- name: item test
  hosts: 192.168.65.20
  remote_user: root
  gather_facts: false
  tasks:
    - debug:
        msg: "{{item}}"
      with_items: [a,b,c,d]
输出的item值,abcd依次传入
​
[root@test41 opt]# ansible-playbook test4.yaml 
ok: [192.168.65.20] => (item=a) => {
    "msg": "a"
}
ok: [192.168.65.20] => (item=b) => {
    "msg": "b"
}
ok: [192.168.65.20] => (item=c) => {
    "msg": "c"
}
ok: [192.168.65.20] => (item=d) => {
    "msg": "d"
}
​

#循环结构:absible有多种循环方式,般都命名为with items,定义循环的内容。 #with item 单循环输出: name: item test 192.168.233.20hosts: remote user:root gather facts:false tasks: debug: msg:"{fitem}} with nested: [a,b,c,d

#输出item的值,with items:abcd依次传入。

#with list,整个列表做为一个整体,进行输出。

#wiht together,做为整体,两两配对输出

#with nested:每一层都是遍历执行一遍,输出结果。

#条件判断,主机的ip#才会执行性 一次性创建4个文件,/opt/a /opt/b /opt/c /opt/d 循环 with items

[root@test41 opt]# vim test4.yaml
​
- name: this is if
  hosts: all
  remote_user: root
  tasks:
    - name: test when
      file:
        path: "{{ item }}"
        state: touch
      with_items: [/opt/a,/opt/b,/opt/c,/opt/d]
      when: ansible_default_ipv4.address == "192.168.65.20"
~                              
​
[root@test41 opt]# ansible-playbook test4.yaml 
ok: [192.168.65.20]
 __________________
< TASK [test when] >
 ------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
​
changed: [192.168.65.20] => (item=/opt/a)
changed: [192.168.65.20] => (item=/opt/b)
changed: [192.168.65.20] => (item=/opt/c)
changed: [192.168.65.20] => (item=/opt/d)
 ____________
#到20上查看
[root@test2 opt]# ls
a  c  
b  d  
  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值