首页 > 其他分享 >ansible剧本

ansible剧本

时间:2023-08-15 21:37:21浏览次数:32  
标签:name tags ip ansible nfs user 剧本

ansible剧本

简单剧本

---
- name: 这是一个安装nginx的剧本
hosts: web
tasks: 
- name: 01 安装nginx
yum:
name: nginx
state: installed
- name: 02 启动nginx
systemd:
name: nginx
state: started

剧本的高级特性

高级特性是为了简化剧本

比如,创建10个系统的用户

不用循环

ansible test -m user  -a "name=cc01"
ansible test -m user  -a "name=cc02"
ansible test -m user  -a "name=cc03"
ansible test -m user  -a "name=cc04"
...

loop循环特性

---
- name: create user
hosts: test
tasks:
- name: create user
user:
name: "{{ item }}"
state: present
loop:
  - test1
  - test2
  - test3
  - test4
  - test5
- name: set passwd
shell: echo "yuchao666" |passwd --stdin "{{ item }}"
loop:
  - test1
  - test2
  - test3
  - test4
  - test5

loop循环这上面这个场景也会多次定义用户列表,可以通过vars关键字定义循环变量

---
- name: craete user
hosts: test
vars: 
users_list:
  - test1
  - test2
  - test3
  - test4
  - test5
tasks: 
- name: create user 
user: 
name: "{{item}}"
state: present
loop: "{{users_list}}"
- name: set passwd
shell: echo "yuchao666" | passwd --stdin "{{item}}"
loop: "{{users_list}}"

循环优化写法(利用loop循环的知识点,简化一些操作)

前提条件是,原本的动作是重复的,不是重复,就没办法

循环创建用户,且设置用户uid

---
- name: craete user
hosts: test
vars: 
users_info:
  - {user: 't1',uid: '2000'}
  - {user: 't2',uid: '2001'}
  - {user: 't3',uid: '2002'}
  - {user: 't4',uid: '2003'}
  - {user: 't5',uid: '2004'}
tasks:
- name: create user and uid
user:
name: "{{item.user}}"
uid: "{{item.uid}}"
loop: "{{users_info}}"

vars自定义变量

---
- name: copy file
hosts: test
vars: 
data_path: /data
dest_path: /etc
config_path: /ect/rsync.passwd
tasks:
- name: 01 mkdir data dir 
file: 
path: "{{data_path}}"
state: directory
- name: 02 copy config file
copy: 
src: "{{config_path}}"
dest: "{{dest_path}}"
ansible默认提供了一个模块,setup模块,

master-61在通过ssh远程连接,操作目标机器的时候,ansible会默认收集这个机器的所有信息,放入到一个setup模块中,这个机器的 主机名,ip地址,mac地址,磁盘数量,是否是虚拟化,cpu核数所有的这些静态数据

ansible test -m setup

这个模块会默认采集目标机器的所有静态信息 

1. ansible提供了大量的内置变量,去获取机器的属性,(setup模块去采集客户端机器的属性,然后放入到了json数据中,存储为键值对形式,ansible默认输出的结果不是json格式)

---
- hosts: test
tasks:
- name: 01 get ip address
debug: msg="该web组机器,ip是  {{ ansible_all_ipv4_addresses }}" 
- name: 02 get hostname
debug: msg="该机器的主机名是 {{ ansible_hostname }}"
- name: 03 单ip
debug: msg="{{ansible_default_ipv4.address}}"
- name: 04 eth0的ip地址
debug: msg="eth0的ip地址是 {{ansible_facts.eth0.ipv4.address}}"
- name: 05 eth1的ip地址
debug: msg="eth1的ip地址是 {{ansible_facts.eth1.ipv4.address}}"

register注册变量

ansible的模块在运行之后,其实都会返回一些"返回值",只是默认情况下,这些"返回值"并不会显示而已。我们可以把这些返回值写入到某个变量中,这样我们就能够通过引用对应的变量从而获取到这些返回值了,这种将模块的返回值写入到变量中的方法被称为"注册变量"
注册一个变量
---
- name: register_get_ip
hosts: test
tasks
- name: echo ip address
shell: "echo {{ansible_default_ipv4.address}} >> /tmp/ip.log"
- name: cat ip.log
shell: "cat /tmp/ip.log"
register: ip_log
- name: debug ip_log
debug:
msg: "{{ip_log.stdout_lines}}"
注册多个变量
---
- name: register_getinfo.yaml
hosts: test
tasks:
- name: 01 get ip
shell: "echo {{ansible_default_ipv4.address}} > /tmp/ip.log"
- name: 02 get hostname
shell: "echo {{ansible_hostname}} > /tmp/hostname.log"
- name: 03 echo ip
shell: "cat /tmp/ip.log"
register: ip_log
- name: 04 echo hostname
shell: "cat /tmp/hostname.log"
register: hostname_log
- debug:
msg: "{{item}}"
loop:
    - "{{ip_log.stdout_lines}}"
    - "{{hostname_log.stdout_lines}}"

也可以写成    
---
- name: register_getinfo.yaml
hosts: test
tasks:
- name: 01 get ip
shell: "echo {{ansible_default_ipv4.address}} > /tmp/ip.log;cat /tmp/ip.log"
register: ip_log
- name: 02 get hostname
shell: "echo {{ansible_hostname}} > /tmp/hostname.log;cat /tmp/hostname.log"
register: hostname_log
- debug:
msg: "{{item}}"
loop:
    - "{{ip_log.stdout_lines}}"
    - "{{hostname_log.stdout_lines}}"
想知道关于注册变量的返回值,可以用哪些方法,看这个官网即可
https://docs.ansible.com/ansible/latest/reference_appendices/common_return_values.html
register+when

示例1:
判断当配置文件变化后,就重启服务
我们重启配置服务的标准是,修改了配置文件,否则无须重启

---
- name: when_register
hosts: test
tasks:
- name: 01 copy rsync.conf
copy:
src: /script/rsync.conf
dest: /etc
register: conf_status
- name: 02 restart rsync
systemd:
name: rsyncd
state: restarted
when: conf_status.changed

示例2:
当nfs配置文件存在,就显示其内容,不存在则提示不存在

---
- name: when_register_nfs
hosts: test
vars:
nfs_file: /etc/exports
tasks:
- name: 01 check nfs_config
shell: "cat {{nfs_file}}"
register: nfs_result
ignore_errors: ture
- name: 02 debug nfs_config
debug:
msg: "{{ansible_hostname}} has {{nfs_file}},file content is {{nfs_result.stdout_lines}}"
when: nfs_result is success
- name: 03 debug nfs not exists
debug: msg="{{nfs_file}} is not exists."
when: nfs_result is failed

handle + notify

1. handler关键字必须写在剧本结尾
2. handler是定义事件列表,可以定义多个要执行的事件,给每一个事件起好名字
如何调用这个事件,通过notify关键字。notify是写在tasks任务列表里的,(当某一个任务的确执行了,发生了change更改状态,就会触发notify的执行,执行notify指定的名称的handler事件)

---
- name: handler_notify_rsync
hosts: test
tasks:
- name: 01 copy rsyncd.conf
copy:
src: /tmp/rsyncd.conf
dest: /etc/
notify:
  - restart rsyncd
handlers:
- name: restart rsyncd
systemd:
name: rsyncd
state: restarted

tags 标签

---
- name: 安装nfs服务器
hosts: test
tasks:
- name: 安装nfs
yum:
name: nfs-utils
state: installed
tags: 01_install_nfs_service
- name: 安装rpcbind
yum:
name: rpcbind
state: installed
tags: 02_install_rpcbind_service
- name: 创建用户组
group:
name: www
gid: 666
tags: 03_add_group
- name: 创建用户
user:
name: www
uid: 666
group: www
create_home: no
shell: /sbin/nologin
tags: 04_add_user
- name: 创建共享目录
file:
path: /nfs-data
state: directory
owner: www
group: www
tags: 05_create_data_dir
- name: 拷贝配置文件
copy:
src: /tmp/exports
dest: /etc/exports
tags: 06_copy_nfs_exports
- name: 创建关于rsync密码文件
copy:
content: "yuchao666"
dest: /etc/rsync.passwd
mode: 600
tags: 07_create_rsync_passwd
- name: 启动rpcbind
systemd:
name: rpcbind
state: started
enabled: yes
tags: 08_start_rpcbind
- name: 启动nfs
systemd:
name: nfs
state: started
enabled: yes
tags: 09_start_nfs


查看tags命令
ansible-playbook --help |grep tags
[--skip-tags SKIP_TAGS] [-C] [--syntax-check] [-D]
[--list-tags] [--step] [--start-at-task START_AT_TASK]
--list-tags           list all available tags  
--skip-tags SKIP_TAGS only run plays and tasks whose tags do not match these
-t TAGS, --tags TAGS  only run plays and tasks tagged with these values


#列出全部tags
ansible-playbook --list-tags install_nfs.yaml 

playbook: install_nfs.yaml
play #1 (test): 安装nfs服务器	TAGS: []
TASK TAGS: [01_install_nfs_service, 02_install_rpcbind_service, 03_add_group, 04_add_user, 05_create_data_dir, 06_copy_nfs_exports, 07_create_rsync_passwd, 08_start_rpcbind, 09_start_nfs]

# 跳过某些tags
ansible-playbook --skip-tags 03_add_group,04_add_user  install_nfs.yaml

# 只执行某些tags
ansible-playbook -t 03_add_group,04_add_user install_nfs.yaml

选择tasks执行

1.列出当前剧本有多少个任务(查看任务列表)
--list-tasks  list all tasks that would be executed
查看有多少个任务需要执行,以及该任务是否有tag标签
# ansible-playbook --list-tasks install_nfs.yaml 
playbook: install_nfs.yaml
play #1 (test): 安装nfs服务器	TAGS: []
tasks:
 安装nfs	TAGS: [01_install_nfs_service]
 安装rpcbind	TAGS: [02_install_rpcbind_service]
 创建用户组	TAGS: [03_add_group]
 创建用户	TAGS: [04_add_user]
 创建共享目录	TAGS: [05_create_data_dir]
 拷贝配置文件	TAGS: [06_copy_nfs_exports]
 创建关于rsync密码文件	TAGS: [07_create_rsync_passwd]
 启动rpcbind	TAGS: [08_start_rpcbind]
 启动nfs	TAGS: [09_start_nfs]

2.指定从哪个tasks开始运行
--start-at-task START_AT_TASK 
ansible-playbook --start-at-task  "创建关于rsync密码文件"  install_nfs.yaml

标签:name,tags,ip,ansible,nfs,user,剧本
From: https://www.cnblogs.com/chunjeh/p/17632488.html

相关文章

  • Ansible 批量100台服务器添加 Crontab
    Ansible 是使用Python开发的自动化运维工具。它可以配置管理,部署软件并编排更高级的任务,例如持续部署或零停机滚动升级。Ansible可以用来管理crontab。Crontab 是一个用于在Unix和Unix-like操作系统上执行定期任务的工具,它允许用户在预定的时间间隔内自动运行命令或脚本......
  • Ansible快速复习
    本文章是上一篇ansible自动化运维的快速复习以及项目上用到的一些命令,针对长时间未使用ansible,导致命令忘记的同学,帮助快速回忆。详细内容还是看上一章‘ansible自动化运维’。查看版本ansible--version工作目录/etc/ansible/ansibel.cfg && /root/.ansible.cfg && .......
  • Ansible自动化运维
    一、什么是ansible1、介绍Ansible是一款开源自动化平台。是一种简单的自动化语言,能够在AnsiblePlaybook中完美地描述IT应用基础架构。也是一个自动化引擎,可运行AnsiblePlaybook。2、逻辑图3、ansible特点简单明了  Ansibleplaybook提供人类可读的自动化。功......
  • YAML语法搞定ansible playbook
    这个页面提供一个正确的YAML语法的基本概述,它被用来描述一个playbooks(我们的配置管理语言).我们使用YAML是因为它像XML或JSON是一种利于人们读写的数据格式.此外在大多数变成语言中有使用YAML的库.你可能希望读Playbooks实践中如何使用的.基本的YAML对于Ansibl......
  • Ansible的安装和全面介绍
    Ansible简介官网https://www.ansible.com/Ansible介绍视频https://www.youtube.com/watch?v=iVWmbStE1MMAnsible中文权威指南http://ansible-tran.readthedocs.io/en/latest/Ansible自动化运维教程https://www.w3cschool.cn/automate_with_ansible/官方的title是“AnsibleisS......
  • 运维管理工具的对比Puppet、Chef、Ansible和SaltStack、Fabric
    我们发现分布式是一个发展的趋势,无论是大型网站的负载均衡架构还是大数据框架部署,以及云存储计算系统搭建都离不开多台服务器的连续部署和环境搭建。当我们的基础架构是分散式或者基于云的,并且我们经常需要处理在大部分相同的服务器上频繁部署大致相同的服务时,我们就应该考虑自动化......
  • ansible批量修改主机名
    [root@ansibleansible]#cathost_name.sh#!/bin/bashHOSTIP=`ipasens33|awk-F"[/]+"'NR==3{print$3}'`NAMEIP=`grep"$HOSTIP"host_name|awk'{print$2}'`hostnamectlset-hostname$NAMEIP 三台主机的主机名已经用不到了,全部修改......
  • Kolla-ansible自动化部署openstack
     Kolla-ansible自动化部署openstack一、准备工作(模拟all-in-one部署)1、配置好网卡IP(至少2张网卡)vm模拟环境(1张nat+1张桥接网卡)nat网卡(ens32):192.168.108.10桥接网卡(ens33):10.51.40.2112、修改主机名hostnamectlset-hostname+主机名3、关闭防火墙、NM服务、selinuxs......
  • ansible一时脑抽使用file模块操作文件导致远程主机bin目录软连接被更改,系统无法登录
    如题,二者有相同的参数,脑抽执行后就悲剧了,造成多个主机无法远程登录。故障起因:错误操作:使用ansilbe远程往目标主机/bin目录拷贝文件的时候,使用错误模块,本该使用copy模块,而使用了file模块,造成远程主机/bin这个软连接被连接到了不存在的文件,并且权限被设置为了644。影响:造成远程主......
  • 使用python调用ansible 的Playbook
    使用Python调用Ansible的Playbook作为一名经验丰富的开发者,我将帮助你了解如何使用Python调用Ansible的Playbook。这将帮助你自动化和简化部署过程,提高工作效率。整体流程下面是整个过程的流程图:步骤描述1安装Ansible2创建Ansible的Inventory文件3创建Ansibl......