ansible

ansible官方网站
安装部署软件ansible
yum install -y ansible --- 需要依赖epel的yum源
ansible配置文件
/etc/ansible/ansible.cfg --- ansible服务配置文件
/etc/ansible/hosts --- 主机清单文件 定义可以管理的主机信息
/etc/ansible/roles --- 角色目录

主机清单文件的使用方法
第一种基于密码连接
主机+端口+密码
10.0.0.3 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='123456'
通过vars批量配置ansible_ssh_pass
[webservers]
10.0.0.3
10.0.0.4
[webservers:vars]
ansible_ssh_pass='123456'
多组合并
[webservers:children]
[group_name1]
[group_name2]

ansible软件输出颜色说明:
01. 绿色信息: 查看主机信息/对主机未做改动
02. 黄色信息: 对主机数据信息做了修改
03. 红色信息: 命令执行出错了
04. 粉色信息: 忠告信息
05. 蓝色信息: 显示ansible命令执行的过程

ansible Ad-Hoc 临时指令使用方式

语法格式:
ansible 主机名称/主机组名称/主机地址信息/all -m(指定应用的模块信息) 模块名称 -a(指定动作信息) "执行什么动作"
命令 操作对象 模块 动作
例如: ansible all -m command -a "hostname"

命令类型模块:

command (默认模块)在一个远程主机上执行一个命令

扩展用法:

  1. chdir 在执行命令之前对目录进行切换
    例如: ansible 172.16.1.31 -m command -a "chdir=/tmp touch test.txt"
  2. creates 如果文件存在了,不执行命令操作
    例如: ansible 172.16.1.31 -m command -a "creates=/tmp/hosts touch test.txt"
  3. removes 如果文件存在了,这个步骤将执行
    例如:ansible 172.16.1.31 -m command -a "removes=/tmp/hosts chdir=/tmp touch oldboy.txt"
    注意事项:
    有些符号信息无法识别: <", ">", "|", ";" and "&"

如果需要一些管道操作,则使用shell

ansible oldboy -m shell -a "ifconfig|grep eth0" -f 50

-f =forks /etc/ansible/ansible.cfg #结果返回的数量

2.script脚本模块

编写脚本

在本地运行模块,等同于在远程执行,不需要将脚本文件进行推送目标主机执行

例如 ansible oldboy -m script -a "/server/scripts/yum.sh"

yum安装软件模块

[root@m01 ~]# ansible oldboy -m yum -a "name=httpd state=installed"
name #指定要安装的软件包名称
state #指定使用yum的方法
installed,present #安装软件包
removed,absent #移除软件包
latest #安装最新软件包

文件类型模块:
copy – 复制文件到远程主机
例如: ansible 172.16.0.3 -m copy -a "src=/etc/hosts dest=/etc/"
将本机/etc/hosts文件发送到 172.16.1.3的/etc/目录
172.16.0.31 | CHANGED => { --- 对哪台主机进行操作
"changed": true, --- 是否对主机信息进行改变
"checksum": "6ed7f68a1d6b4b36c1418338b2001e421eeba270", --- 生成一个文件校验码==MD5数值
"dest": "/etc/hosts", --- 显示目标路径信息
"gid": 0, --- 显示复制后文件gid信息
"group": "root", --- 显示复制后文件属组信息
"md5sum": "7afd7b74854f0aaab646b3e932f427c0", --- 生成一个文件校验码==MD5数值
"mode": "0644", --- 显示复制后文件权限信息
"owner": "root", --- 显示复制后文件属主信息
"size": 401, --- 显示文件的大小信息
"src": "/root/.ansible/tmp/ansible-tmp-1557804498.23-26487341925325/source",
"state": "file", --- 显示文件的类型信息
"uid": 0 --- 显示复制后文件uid信息
}
扩展用法:
01. 在传输文件时修改文件的属主和属组信息
ansible 172.16.1.31 -m copy -a "src=/etc/ansible/file/rsync/rsync.password dest=/etc/ owner=oldboy group=oldboy"
02. 在传输文件时修改文件的权限信息
ansible 172.16.1.31 -m copy -a "src=/etc/ansible/file/rsync/rsync.password dest=/etc/ mode=1777"
03. 在传输数据文件信息时对远程主机源文件进行备份
ansible 172.16.1.31 -m copy -a "src=/etc/ansible/file/rsync/rsync.password dest=/etc/ backup=yes"
04. 创建一个文件并直接编辑文件的信息
ansible 172.16.1.31 -m copy -a "content='oldboy123' dest=/etc/rsync.password"

自行研究: remote_src  directory_mode local_follow
If no, it will search for src at originating/master machine.
       src参数指定文件信息,会在本地管理端服务进行查找
If yes it will go to the remote/target machine for the src. Default is no.
       src参数指定文件信息,会从远程主机上进行查找

PS: ansible软件copy模块复制目录信息
ansible 172.16.1.31 -m copy -a "src=/oldboy dest=/oldboy"  
src后面目录没有/: 将目录本身以及目录下面的内容都进行远程传输复制
ansible 172.16.1.31 -m copy -a "src=/oldboy/ dest=/oldboy"  
src后面目录有/:   只将目录下面的内容都进行远程传输复制 

file – 设置文件属性信息
基本用法:
ansible 172.16.1.31 -m file -a "dest=/etc/hosts owner=oldboy group=oldboy mode=666"

扩展用法:
1. 可以利用模块创建数据信息 (文件 目录 链接文件)

state 参数
=absent --- 缺席/删除数据信息
=directory --- 创建一个目录信息
=file --- 检查创建的数据信息是否存在 绿色存在 红色不存在
=hard --- 创建一个硬链接文件
=link --- 创建一个软链接文件
=touch --- 创建一个文件信息

创建目录信息:
ansible 172.16.1.31 -m file -a "dest=/oldboy/ state=directory"
创建文件信息:
ansible 172.16.1.31 -m file -a "dest=/oldboy/oldboy.txt state=touch"
创建链接文件信息:
ansible 172.16.1.31 -m file -a "src=/oldboy/oldboy.txt dest=/oldboy/oldboy_hard.txt state=hard"
ansible 172.16.1.31 -m file -a "src=/oldboy/oldboy.txt dest=/oldboy/oldboy_link.txt state=link"
删除数据信息
ansible 172.16.1.31 -m file -a "dest=/oldboy/oldboy.txt state=absent"
ansible 172.16.1.31 -m file -a "dest=/oldboy/ state=absent"

service模块: 管理服务器的运行状态 停止 开启 重启
name: --- 指定管理的服务名称
state: --- 指定服务状态
started 启动
restarted 重启
stopped 停止
enabled --- 指定服务是否开机自启动
ansible 172.16.1.31 -m service -a "name=nfs state=started enabled=yes"

cron模块: 批量设置多个主机的定时任务信息

minute:设置分钟信息( 0-59, *, */2, etc )
hour:设置小时信息
day:设置日期信息
month:设置月份信息
weekday:周信息

基本用法:
ansible 172.16.1.31 -m cron -a "minute=0 hour=2 job='/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1'" 

扩展用法:
01. 给定时任务设置注释信息
ansible 172.16.1.31 -m cron -a "name='time sync' minute=0 hour=2 job='/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1'"
02. 删除指定定时任务
ansible 172.16.1.31 -m cron -a "name='time sync01' state=absent"
PS: ansible可以删除的定时任务,只能是ansible设置好的定时任务
03. 批量注释定时任务
ansible 172.16.1.31 -m cron -a "name='time sync' job='/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1' disabled=yes"

mount: 批量进行挂载操作
       src:  需要挂载的存储设备或文件信息
       path: 指定目标挂载点目录
       fstype: 指定挂载时的文件系统类型
       state
       present/mounted     --- 进行挂载
       present: 不会实现立即挂载,修改fstab文件,实现开机自动挂载
       mounted: 会实现立即挂载, 并且会修改fstab文件,实现开机自动挂载 *****
       
       absent/unmounted    --- 进行卸载
       absent:     会实现立即卸载, 并且会删除fstab文件信息,禁止开机自动挂载
       unmounted:  会实现立即卸载, 但是不会会删除fstab文件信息  *****

user模块: 实现批量创建用户
基本用法:
ansible 172.16.1.31 -m user -a "name=oldboy01"

扩展用法:
1) 指定用户uid信息
ansible 172.16.1.31 -m user -a "name=oldboy02 uid=6666"

2) 指定用户组信息
ansible 172.16.1.31 -m user -a "name=oldboy03 group=oldboy02"
ansible 172.16.1.31 -m user -a "name=oldboy04 groups=oldboy02"

3) 批量创建虚拟用户
ansible 172.16.1.31 -m user -a "name=rsync create_home=no  shell=/sbin/nologin"

4) 给指定用户创建密码
PS: 利用ansible程序user模块设置用户密码信息,需要将密码明文信息转换为密文信息进行设置
生成密文密码信息方法:
方法1:

ansible all -i localhost, -m debug -a "msg={{ '123456' | password_hash('sha512', 'oldboy') }}"
方法2:
yum install -y python-pip
pip install passlib
python -c "from passlib.hash import sha512_crypt; import getpass; print(sha512_crypt.using(rounds=5000).hash(getpass.getpass()))"
Password:
6rJJeiIerQ8p2eR82$uE2701X7vY44voF4j4tIQuUawmTNHEZhs26nKOL0z39LWyvIvZrHPM52Ivu9FgExlTFgz1VTOCSG7KhxJ9Tqk.

ansible 172.16.1.31 -m user -a 'name=oldboy08 password=$6$oldboy$MVd3DevkLcimrBLdMICrBY8HF82Wtau5cI8D2w4Zs6P1cCfMTcnnyAmmJc7mQaE9zuHxk8JFTRgYMGv9uKW7j1'
  1. 剧本的编写方法
    剧本的作用: 可以一键化完成多个任务
    自动化部署rsync服务:
    服务端的操作
    第一个历程安装软件:
    ansible 172.16.1.41 -m yum -a "name=rsync state=installed"

    第二个历程编写文件:
    ansible 172.16.1.41 -m copy -a "src=/xxx/rsyncd.conf dest=/etc/"

    第三个历程创建用户
    ansible 172.16.1.41 -m user -a "name=rsync create_home=no shell=/sbin/nologin"

    第四个历程创建目录
    ansible 172.16.1.41 -m file -a "dest=/backup state=directory owner=rsync group=rsync"

    第五个历程创建密码文件
    ansible 172.16.1.41 -m copy -a "content='rsync_backup:oldboy123' dest=/etc/rsync.password mode=600"

    第六个历程启动服务
    ansible 172.16.1.41 -m service -a "name=rsyncd state=started enabled=yes"

    客户端的操作:
    第一个历程: 创建密码文件
    ansible 客户端地址 -m copy -a "content='rsync_backup:oldboy123' dest=/etc/rsync.password mode=600"

    剧本的做成部分:
    演员信息: 男一号 hosts
    干的事情: 吻戏 tasks

    演员信息: 男二号
    干的事情: 看着

    剧本编写规范: pyyaml -- 三点要求

    1. 合理的信息缩进 两个空格表示一个缩进关系
      标题一
      标题二
      标题三
      PS: 在ansible中一定不能用tab进行缩进

    2. 冒号的使用方法
      hosts: 172.16.1.41
      tasks:
      yum: name=xx
      PS: 使用冒号时后面要有空格信息
      以冒号结尾,冒号信息出现在注释说明中,后面不需要加上空格

    3. 短横线应用 -(列表功能)

      • 张三

        • 打游戏
        • 运动
      • 李四

        学习
        湖南

      • 王五

        运动
        深圳
        PS: 使用短横线构成列表信息,短横线后面需要有空格

    开始编写剧本
    mkdir /etc/ansible/ansible-playbook
    vim rsync_server.ymal
    说明: 剧本文件扩展名尽量写为yaml

    1. 方便识别文件是一个剧本文件
    2. 文件编写时会有颜色提示
  • hosts: 172.16.1.41
    tasks:
    yum: name=rsync state=installed
    copy: src=/tmp/rsyncd.conf dest=/etc/

    如何执行剧本:
    第一个步骤: 检查剧本的语法格式
    ansible-playbook --syntax-check rsync_server.yaml
    第二个步骤: 模拟执行剧本
    ansible-playbook -C rsync_server.yaml
    第三个步骤: 直接执行剧本
    ansible-playbook rsync_server.yaml

  • hosts: 172.16.1.41
    tasks:

    • name: 01-install rsync
      yum: name=rsync state=installed
    • name: 02-push conf file
      copy: src=/tmp/rsyncd.conf dest=/etc/
  1. 利用剧本完成服务一键化部署:
    rsync 服务部署
    nfs 服务部署
    sersync 服务部署
    全网备份项目

    rsync服务剧本编写:
    准备工作:

    1. 熟悉软件部署流程
    2. 熟悉ansible软件模块使用
    3. 熟悉ansible剧本编写规范
      ansible:
      ad-hoc 临时实现批量管理功能(模块) --- 命令
      playbook 永久实现批量管理功能(剧本) --- 脚本

    剧本编写常见错误:

    1. 剧本语法规范是否符合(空格 冒号 短横线)
    2. 剧本中模块使用是否正确
    3. 剧本中一个name标识下面只能写一个模块任务信息
    4. 剧本中尽量不要大量使用shell模块
[root@m01 ansible-playbook]# cat rsync_server.yaml 
  • hosts: rsync_server
    tasks:

    • name: 01-install rsync
      yum: name=rsync state=installed
    • name: 02-push conf file
      copy: src=/etc/ansible/server_file/rsync_server/rsyncd.conf dest=/etc/
    • name: 03-create user
      user: name=rsync create_home=no shell=/sbin/nologin

      shell: useradd rsync -M -s /sbin/nologin

    • name: 04-create backup dir
      file: path=/backup state=directory owner=rsync group=rsync
    • name: 05-create password file
      copy: content=rsync_backup:oldboy123 dest=/etc/rsync.password mode=600
    • name: 06-start rsync server
      service: name=rsyncd state=started enabled=yes
  • hosts: rsync_clients
    tasks:

    • name: 01-install rsync
      yum: name=rsync state=installed
    • name: 02-create password file
      copy: content=oldboy123 dest=/etc/rsync.password mode=600
    • name: 03-create test file
      file: dest=/tmp/test.txt state=touch
    • name: 04-check test
      shell: rsync -avz /tmp/test.txt rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
  1. 如何配置主机清单
    第一种方式: 分组配置主机信息
    [web]
    172.16.1.7
    172.16.1.8
    172.16.1.9

    [data]
    172.16.1.31
    172.16.1.41
    操作过程
    [root@m01 ansible-playbook]# ansible data -a "hostname"
    172.16.1.31 | CHANGED | rc=0 >>
    nfs01

    172.16.1.41 | CHANGED | rc=0 >>
    backup

    [root@m01 ansible-playbook]# ansible web -a "hostname"
    172.16.1.7 | CHANGED | rc=0 >>
    web01

    第二种方式: 主机名符号匹配配置
    [web]
    172.16.1.[7:9]
    [web]
    web[01:03]

    第三种方式: 跟上非标准远程端口
    [web]
    web01:52113
    172.16.1.7:52113

    第四种方式: 主机使用特殊的变量
    [web]
    172.16.1.7 ansible_ssh_port=52113 ansible_ssh_user=root ansible_ssh_pass=123456
    [web]
    web01 ansible_ssh_host=172.16.1.7 ansible_ssh_port=52113 ansible_ssh_user=root ansible_ssh_pass=123456

    第五种方式: 主机组名嵌入配置
    [rsync:children] --- 嵌入子组信息
    rsync_server
    rsync_client

    [rsync_server]
    172.16.1.41

    [rsync_client]
    172.16.1.31
    172.16.1.7

    [web:vars] --- 嵌入式变量信息
    ansible_ssh_host=172.16.1.7
    ansible_ssh_port=52113
    ansible_ssh_user=root
    ansible_ssh_pass=123456
    [web]
    web01

    主机清单的配置方法:
    https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html

  2. 在剧本中设置变量信息
    方式一:直接在剧本文件编写
    vars:
    oldboy01: data01
    oldboy02: data02

    方式二:在命令行中进行指定
    ansible-playbook --extra-vars=oldboy01=data01

    方式三:在主机清单文件编写
    [oldboy]
    oldboy01=data01
    oldboy02=data02

    三种变量设置方式都配置了,三种方式的优先级???
    最优先: 命令行变量设置
    次优先: 剧本中变量设置
    最后: 主机清单变量设置

    如何全局设置变量: roles 剧本整合

  1. 在剧本中设置注册信息

    • hosts: oldboy
      tasks:
      • name: check server port
        shell: netstat -lntup --- 端口信息
        register: get_server_port<--端口信息

      • name: display port info
        debug: msg={{ get_server_port.stdout_lines }}
        显示进程信息,表示服务已经正常启动
        PS: 设置变量不能有空格信息

  2. 在剧本中设置判断信息
    如何指定判断条件:
    (ansible_hostname == "nfs01")
    (ansible_hostname == "web01")
    setup模块中显示被管理主机系统的详细信息

    • hosts: oldboy
      remote_user: root
      tasks:
      • name: Check File
        file: path=/tmp/this_is_{{ ansible_hostname }}_file state=touch
        when: (ansible_hostname == "nfs") or (ansible_hostname == "backup")

      • name: install httpd
        yum: name=httpd state=installed
        when: (系统情况 == "CentOS")

      • name: install httpd2
        yum: name=httpd2 state=installed
        when: (系统情况 == "ubuntu")

    获取内置变量方法:
    ansible oldboy -m setup -a "filter=ansible_hostname"
    常见主机信息:
    ansible_all_ipv4_addresses: 仅显示ipv4的信息。
    ansible_devices: 仅显示磁盘设备信息。
    ansible_distribution: 显示是什么系统,例:centos,suse等。
    ansible_distribution_major_version: 显示是系统主版本。
    ansible_distribution_version: 仅显示系统版本。
    ansible_machine: 显示系统类型,例:32位,还是64位。
    ansible_eth0: 仅显示eth0的信息。
    ansible_hostname: 仅显示主机名。
    ansible_kernel: 仅显示内核版本。
    ansible_lvm: 显示lvm相关信息。
    ansible_memtotal_mb: 显示系统总内存。
    ansible_memfree_mb: 显示可用系统内存。
    ansible_memory_mb: 详细显示内存情况。
    ansible_swaptotal_mb: 显示总的swap内存。
    ansible_swapfree_mb: 显示swap内存的可用内存。
    ansible_mounts: 显示系统磁盘挂载情况。
    ansible_processor: 显示cpu个数(具体显示每个cpu的型号)。
    ansible_processor_vcpus: 显示cpu个数(只显示总的个数)。

    获取子信息方法:
    ansible_eth0[ipv4]

  3. 在剧本中设置循环信息
    vim test04.yml

    • hosts: all
      remote_user: root
      tasks:
      • name: Add Users
        user: name={{ item.name }} groups={{ item.groups }} state=present
        with_items:
        • { name: 'testuser1', groups: 'bin' }
        • { name: 'testuser2', groups: 'root' }

    vim test05.yml

    • hosts: all
      remote_user: root
      tasks:
      • name: Installed Pkg
        yum: name={{ item }} state=present
        with_items:
        • wget
        • tree
        • lrzsz
  4. 在剧本中设置忽略错误
    默认playbook会检查命令和模块的返回状态,如遇到错误就中断playbook的执行
    可以加入ignore_errors: yes忽略错误
    vim test06.yml

    • hosts: all
      remote_user: root
      tasks:
      • name: Ignore False
        command: /bin/false
        ignore_errors: yes
      • name: touch new file
        file: path=/tmp/oldboy_ignore state=touch
  5. 在剧本中设置标签功能

    • hosts: oldboy
      ignore_errors: yes
      remote_user: root
      tasks:
      • name: Check File
        file: path=/tmp/this_is_{{ ansible_hostname }}_file state=touch
        when: (ansible_hostname == "nfs01") or (ansible_hostname == "backup")
        tags: t1

      • name: bad thing
        command: ech 123

        ignore_errors: yes

        tags: t2

      • name: install httpd
        yum: name=httpd state=installed
        when: (ansible_all_ipv4_addresses == ["172.16.1.7","10.0.0.7"])
        tags: t3

      • name: install httpd2
        yum: name=httpd2 state=installed
        when: (ansible_distribution == "ubuntu")
        tags: t4

    指定执行哪个标签任务: ansible-playbook --tags=t2 test05.yml
    跳过指定标签任务: ansible-playbook --skip-tags=t2 test05.yml

  6. 在剧本中设置触发功能

    • hosts: backup
      remote_user: root
      tasks:

      • name: 01 Install rsync
        yum: name=rsync state=present

      • name: 02 push config file
        copy: src=./file/{{ item.src }} dest=/etc/{{ item.dest }} mode={{ item.mode }}
        with_items:

        • { src: "rsyncd.conf", dest: "rsyncd.conf", mode: "0644" }
        • { src: "rsync.password", dest: "rsync.password", mode: "0600" }
          notify: restart rsync server

      handlers:

      • name: restart rsync server
        service: name=rsyncd state=restarted
  1. 将多个剧本进行整合
    方式一:include_tasks: f1.yml

    • hosts: all
      remote_user: root
      tasks:
      • include_tasks: f1.yml
      • include_tasks: f2.yml

    方式二:include: f1.yml

    • include:f1.yml
    • include:f2.yml

    方式三:- import_playbook:
    [root@m01 ansible-playbook]# cat main.yml

    • import_playbook: base.yml
    • import_playbook: rsync.yml
    • import_playbook: nfs.yml
    • import_playbook: oxxx.yml
    • import_playbook: rsync.yml
    • import_playbook: nfs.yml
  1. 在剧本中设置变量信息
    方式一:直接在剧本文件编写
    vars:
    oldboy01: data01
    oldboy02: data02

    方式二:在命令行中进行指定
    ansible-playbook --extra-vars=oldboy01=data01

    方式三:在主机清单文件编写
    [oldboy]
    oldboy01=data01
    oldboy02=data02

    三种变量设置方式都配置了,三种方式的优先级???
    最优先: 命令行变量设置
    次优先: 剧本中变量设置
    最后: 主机清单变量设置

    如何全局设置变量: roles 剧本整合

  1. 在剧本中设置注册信息

    • hosts: oldboy
      tasks:
      • name: check server port
        shell: netstat -lntup --- 端口信息
        register: get_server_port<--端口信息

      • name: display port info
        debug: msg={{ get_server_port.stdout_lines }}
        显示进程信息,表示服务已经正常启动
        PS: 设置变量不能有空格信息

  2. 在剧本中设置判断信息
    如何指定判断条件:
    (ansible_hostname == "nfs01")
    (ansible_hostname == "web01")
    setup模块中显示被管理主机系统的详细信息

    • hosts: oldboy
      remote_user: root
      tasks:
      • name: Check File
        file: path=/tmp/this_is_{{ ansible_hostname }}_file state=touch
        when: (ansible_hostname == "nfs") or (ansible_hostname == "backup")

      • name: install httpd
        yum: name=httpd state=installed
        when: (系统情况 == "CentOS")

      • name: install httpd2
        yum: name=httpd2 state=installed
        when: (系统情况 == "ubuntu")

    获取内置变量方法:
    ansible oldboy -m setup -a "filter=ansible_hostname"
    常见主机信息:
    ansible_all_ipv4_addresses: 仅显示ipv4的信息。
    ansible_devices: 仅显示磁盘设备信息。
    ansible_distribution: 显示是什么系统,例:centos,suse等。
    ansible_distribution_major_version: 显示是系统主版本。
    ansible_distribution_version: 仅显示系统版本。
    ansible_machine: 显示系统类型,例:32位,还是64位。
    ansible_eth0: 仅显示eth0的信息。
    ansible_hostname: 仅显示主机名。
    ansible_kernel: 仅显示内核版本。
    ansible_lvm: 显示lvm相关信息。
    ansible_memtotal_mb: 显示系统总内存。
    ansible_memfree_mb: 显示可用系统内存。
    ansible_memory_mb: 详细显示内存情况。
    ansible_swaptotal_mb: 显示总的swap内存。
    ansible_swapfree_mb: 显示swap内存的可用内存。
    ansible_mounts: 显示系统磁盘挂载情况。
    ansible_processor: 显示cpu个数(具体显示每个cpu的型号)。
    ansible_processor_vcpus: 显示cpu个数(只显示总的个数)。

    获取子信息方法:
    ansible_eth0[ipv4]

  3. 在剧本中设置循环信息
    vim test04.yml

    • hosts: all
      remote_user: root
      tasks:
      • name: Add Users
        user: name={{ item.name }} groups={{ item.groups }} state=present
        with_items:
        • { name: 'testuser1', groups: 'bin' }
        • { name: 'testuser2', groups: 'root' }

    vim test05.yml

    • hosts: all
      remote_user: root
      tasks:
      • name: Installed Pkg
        yum: name={{ item }} state=present
        with_items:
        • wget
        • tree
        • lrzsz

    剧本执行出现错误排查思路/步骤:

    1. 找到剧本中出现问题关键点
    2. 将剧本中的操作转换成模块进行操作
    3. 将模块的功能操作转换成linux命令
      本地管理主机上执行命令测试
      远程被管理主机上执行命令测试
    • name: 01-install rsync
      yum:
      name: ['rsync', 'tree', 'wget'] --- saltstack
      state: installed

    • name: xxx
      yum: name=xxx state=installed --- ansible

  1. 在剧本中设置忽略错误
    默认playbook会检查命令和模块的返回状态,如遇到错误就中断playbook的执行
    可以加入ignore_errors: yes忽略错误
    vim test06.yml

    • hosts: all
      remote_user: root
      tasks:
      • name: Ignore False
        command: /bin/false
        ignore_errors: yes
      • name: touch new file
        file: path=/tmp/oldboy_ignore state=touch
  2. 在剧本中设置标签功能

    • hosts: oldboy
      ignore_errors: yes
      remote_user: root
      tasks:
      • name: Check File
        file: path=/tmp/this_is_{{ ansible_hostname }}_file state=touch
        when: (ansible_hostname == "nfs01") or (ansible_hostname == "backup")
        tags: t1

      • name: bad thing
        command: ech 123

        ignore_errors: yes

        tags: t2

      • name: install httpd
        yum: name=httpd state=installed
        when: (ansible_all_ipv4_addresses == ["172.16.1.7","10.0.0.7"])
        tags: t3

      • name: install httpd2
        yum: name=httpd2 state=installed
        when: (ansible_distribution == "ubuntu")
        tags: t4

    指定执行哪个标签任务: ansible-playbook --tags=t2 test05.yml
    跳过指定标签任务: ansible-playbook --skip-tags=t2 test05.yml

  3. 在剧本中设置触发功能

    • hosts: backup
      remote_user: root
      tasks:

      • name: 01 Install rsync
        yum: name=rsync state=present

      • name: 02 push config file
        copy: src=./file/{{ item.src }} dest=/etc/{{ item.dest }} mode={{ item.mode }}
        with_items:

        • { src: "rsyncd.conf", dest: "rsyncd.conf", mode: "0644" }
        • { src: "rsync.password", dest: "rsync.password", mode: "0600" }
          notify: restart rsync server

      handlers:

      • name: restart rsync server
        service: name=rsyncd state=restarted
  1. 将多个剧本进行整合
    方式一:include_tasks: f1.yml

    • hosts: all
      remote_user: root
      tasks:
      • include_tasks: f1.yml
      • include_tasks: f2.yml

    方式二:include: f1.yml

    • include:f1.yml
    • include:f2.yml

    方式三:- import_playbook:
    [root@m01 ansible-playbook]# cat main.yml

    • import_playbook: base.yml
    • import_playbook: rsync.yml
    • import_playbook: nfs.yml
    • import_playbook: oxxx.yml
    • import_playbook: rsync.yml
    • import_playbook: nfs.yml

09 编写NFS服务剧本
第一个历程: 创建几个目录
[root@m01 ansible-playbook]# tree nfs-file/
nfs-file/
├── nfs-client
└── nfs-server

第二个历程: 编写剧本信息
主机清单:
[nfs:children]
nfs_server
nfs_client
[nfs_server]
172.16.1.31
[nfs_client]
172.16.1.7

172.16.1.8

172.16.1.9

  • hosts: nfs
    tasks:

    • name: 01-install nfs software
      yum:
      name: ['nfs-utils','rpcbind']
      state: installed
  • hosts: nfs_server

    vars:

    Data_dir: /data

    tasks:

    • name: 01-copy conf file
      copy: src=/etc/ansible/ansible-playbook/nfs-file/nfs-server/exports dest=/etc
      notify: restart nfs server
    • name: 02-create data dir
      file: path={{ Data_dir }} state=directory owner=nfsnobody group=nfsnobody

      path: ['data01','data02','data03']

      state: directory

      owner: nfsnobody

      group: nfsnobody

    • name: 03-boot server

      service: name=rpcbind state=started enabled=yes

      service: name=nfs state=started enabled=yes

      service: name={{ item }} state=started enabled=yes
      with_items:
      • rpcbind
      • nfs

    handlers:

    • name: restart nfs server
      service: name=nfs state=restarted
  • hosts: nfs_client

    vars:

    Data_dir: /data

    tasks:

    • name: 01-mount
      mount: src=172.16.1.31:{{ Data_dir }} path=/mnt fstype=nfs state=mounted
    • name: 02-check mount info
      shell: df -h|grep /data
      register: mount_info
    • name: display mount info
      debug: msg={{ mount_info.stdout_lines }}
      第三个历程: 进行剧本测试
  1. ansible程序roles --- 规范
    剧本编写完问题:

    1. 目录结构不够规范 OK
    2. 编写好的任务如何重复调用
    3. 服务端配置文件改动,客户端参数信息也自动变化
    4. 汇总剧本中没有显示主机角色信息
    5. 一个剧本内容信息过多,不容易进行阅读,如何进行拆分 OK

    第一个历程: 规范目录结构
    cd /etc/ansible/roles
    mkdir {rsync,nfs} --- 创建相应角色目录
    mkdir {nfs,rsync}/{vars,tasks,templates,handlers,files} --- 创建角色目录下面的子目录
    [root@m01 roles]# tree
    .
    ├── nfs
    │ ├── files --- 保存需要分发文件目录
    │ ├── handlers --- 保存触发器配置文件信息
    │ ├── tasks --- 保存要执行的动作信息文件 ok
    │ ├── templates --- 保存需要分发模板文件 模板文件中可以设置变量信息
    │ └── vars --- 保存变量信息文件
    └── rsync
    ├── files
    ├── handlers
    ├── tasks
    ├── templates
    └── vars

    第二个历程: 在roles目录中创建相关文件
    编写文件流程图:

    1. 编写tasks目录中的main.yml文件
    • name: 01-copy conf file
      copy: src=exports dest=/etc
      notify: restart nfs server
    • name: 02-create data dir
      file: path={{ Data_dir }} state=directory owner=nfsnobody group=nfsnobody

      path: ['data01','data02','data03']

      state: directory

      owner: nfsnobody

      group: nfsnobody

    • name: 03-boot server
      service: name={{ item }} state=started enabled=yes
      with_items:
      • rpcbind
      • nfs

    vim main.yml

    • include_tasks: copy_info.yml
    • include_tasks: create_dir.yml
    • include_tasks: boot_server.yml

    vim copy_info.yml

    • name: 01-copy conf file
      copy: src=exports dest=/etc
      notify: restart nfs server

    vim create_dir.yml

    • name: 02-create data dir
      file: path={{ Data_dir }} state=directory owner=nfsnobody group=nfsnobody

    vim boot_server.yml

    • name: 03-boot server
      service: name={{ item }} state=started enabled=yes
      with_items:
      • rpcbind
      • nfs
    1. 编写vars目录中的main.yml文件
      [root@m01 vars]# vim main.yml
      Data_dir: /data

    2. 编写files目录中的文件
      [root@m01 files]# ll
      total 4
      -rw-r--r-- 1 root root 29 May 17 15:23 exports

    3. 编写handlers目录中的main.yml文件
      vim main.yml

    • name: restart nfs server
      service: name=nfs state=restarted

    目录中文件编写好汇总结构
    [root@m01 nfs]# tree
    .
    ├── files
    │ └── exports
    ├── handlers
    │ └── main.yml
    ├── tasks
    │ └── main.yml
    ├── templates
    └── vars
    └── main.yml

    第三个历程: 编写一个主剧本文件
    [root@m01 roles]# cat site.yml

    • hosts: nfs_server
      roles:

      • nfs-server
    • hosts: rsync_server
      roles:

      • rsync
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 230,527评论 6 544
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 99,687评论 3 429
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 178,640评论 0 383
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 63,957评论 1 318
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 72,682评论 6 413
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 56,011评论 1 329
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 44,009评论 3 449
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 43,183评论 0 290
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 49,714评论 1 336
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 41,435评论 3 359
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 43,665评论 1 374
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 39,148评论 5 365
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 44,838评论 3 350
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 35,251评论 0 28
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 36,588评论 1 295
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 52,379评论 3 400
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 48,627评论 2 380

推荐阅读更多精彩内容