ansible剧本playbook

Palybook组层部分

tasks 任务 包含要在目标主机上执行的操作,使用模块定义这些操作,每个任务都是一个模块的调用
variables 变量:存储和传递数据,变量可以自定义,可以在palybook当中定义为全局变量,也可以在外部传参
templates 模版:用于生产配置文件,模版是包含占位符的文件,占位符由ansible在执行时转换为变量值
handlers 处理器:当需要有变更的时候,可以执行触发器
roles 角色:是一种组织和封装palybook的,允许把相关的任务变量,模版和处理器组织成一个可复用的单元
- name: first play
#一个name就是一个任务名,名字可以不写,
  gather_facts: false
#是否收集目标主机的系统信息,false就是不收集,最好不写。
  hosts: 192.168.233.12
#执行的目标主机
  remote_user: root
#在目标主机执行的用户
  tasks:
   - name: ping test
     ping:
   - name: close selinux
     command: '/sbin/setenforce 0'
     ignore_errors: True
   - name: close firewalld
     service: name=firewalld state=stopped
   - name: install httpd
     yum: name=httpd
   - name: start httpd
     service: enabled=true name=httpd state=started
   - name: editon index.html
     shell: echo "this is httpd" > /var/www/html/index.html
     notify: restart httpd
  handlers:
   - name: restart httpd
     service: name=httpd state=restarted

[root@docker1 opt]# ansible-playbook test1.yaml --syntax-check
#检查配置文件是否有错误
[root@docker1 opt]# ansible-playbook test1.yaml --list-task
#检查生效的目标主机
[root@docker1 opt]# ansible-playbook test1.yaml
#运行剧本文件
[root@docker1 opt]# ansible-playbook test1.yaml --start-at-task='install httpd'
#指定运行剧本第几行

如需要切换用户在配置文件中写入
remote_user: dn
become: yes
become_ser: root
vim /etc/ansible/ansible.cfg
71行取消注释
vim /etc/ansible/hosts
[dbservers]
192.168.233.12 ansible_user=root ansible_password=123
需要声明ip地址与主机名

ansible-playbook test1.yaml -u root -k
#密码需要手动输入
- hosts: 192.168.233.12
  remote_user: root
  vars:
    groupname: guoqi
    username: wangdefu
  tasks:
    - name: create group
      group:
        name: "{
  { groupname }}"
        system: yes
        gid: 111
    - name: create user
      user:
        name: "{
  { username }}"
        uid: 1011
        group: "{
  { groupname }}"
        shell: /sbin/nologin
    - name: copy file
      copy:
        content: "{
  { hostvars[inventory_hostname]['ansible_default_ipv4']['address']}}"
        dest: /opt/ky32.txt
#获取目标主机的ip地址,然后打印出来,这里是否获取主机的信息否被删除掉,如果无法获取主机的信息,就会报错
[root@docker1 opt]# ansible-playbook test2.yaml -e 'username=yst groupname=ymr'
#在外面传参
playbook的条件判断
when是一个比较常见的应用场景,实现满足条件即执行,不满足条件即跳过的任务
when是满足条件即执行,不满足不执行

格式
- hosts: 192.168.233.12
#可以用主机的ip地址,也可以是用组名,也可以用all
  remote_user: root
  tasks:
   - name: test when
     debug:
       msg: '位置判断'
     when: ansible_default_ipv4.address == '192.168.233.20'
    #when: inventory_hostname !== '192.168.233.20'
#作用相同
#debug=echo  msg=输出的内容,用于脚本的调试,在正式脚本中可以去除

练习
条件1 ip=10安装nginx ,条件2 ip=20安装httpd
版本1

- hosts: all
  remote_user: root
  tasks:
   - name: nginx
     yum: name=nginx
     when: ansible_default_ipv4.address == '192.168.233.12'
   - name: httpd
     yum: name=httpd
     when: ansible_default_ipv4.address == '192.168.233.13'

版本2

- hosts: all
  remote_user: root
  tasks:
   - name: nginx
     yum: name=nginx
   - name: nginx ifo
     debug:
       msg: "安装nginx"
     when: ansible_default_ipv4.address == '192.168.233.12'
   - name: httpd
     yum: name=httpd
   - name: httpd info
     debug:
       msy: "安装httpd"
     when: ansible_default_ipv4.address == '192.168.233.13'


ansible有多种循环格式,with_items 循环遍历
- hosts: 192.168.233.12
  remote_user: root
  gather_facts: false
  tasks:
   - debug:
       msg: "{
  { item }}"
     with_items: [a,b,c,d]
#声明变量item,playbook的内置变量,with_item,会把item的值,遍历列表当中的a,b,c,d


- hosts: 192.168.233.12
  remote_user: root
  gather_facts: false
  tasks:
   - debug:
       msg: "{
  { item }}"
     with_items:
       - [a,b,c,d]
       - [1,2,3,4]
#这里会被当成一个整体,虽然声明的列表是两个,但是wiith——items还是把两个列表当成整体进行遍历


- hosts: 192.168.233.12
  remote_user: root
  gather_facts: false
  tasks:
   - debug:
       msg: "{
  { item }}"
     with_list:
       - [a,b,c,d]
       - [1,2,3,4]
#这里会被分组打印,一个列表打印一组

- hosts: 192.168.233.12
  remote_user: root
  gather_facts: false
  tasks:
   - name: create file
     file:
       path: "{
  { item }}"
       state: touch
     with_items:
       - [/opt/a,/opt/b,/opt/c,/opt/d]
       - [/opt/1,/opt/2,/opt/3,/opt/4]
#分组创建文件


- hosts: 192.168.233.12
  remote_user: root
  gather_facts: false
  tasks:
   - debug:
       msg: "{
  { item }}"
     with_together:
      - [a,b,c,d]
      - [1,2,3,4]
#组合输出,一一对应,列表组循环,如果没有组合会输出null
#列表里面的元素定义了循环的次数,第二层列表,相当于内循环
with_items:最常用的
with_list:列别分组循环
with_together:列表对应的列,数据结合的方式循环
with_nested:相当于双层循环,第一层定义了循环的次数,第二层表示第一次的每个元素会循环几次
#基于循环,创建文件,目录,和用户组
- name: play1
  hosts: 192.168.233.12
  gather_facts: false
  tasks:
   - name: create group
     group:
       name: "{
  { item }}"
       state: present
     with_items:
       - 'dn1'
       - 'dn2'
   - name: create user
     user:
       name: "{
  { item.name }}"
       state: present
       groups: "{
  { item.groups }}"
     with_items:
       - {name: 'test1', groups: 'dn1'}
       - {name: 'test2', groups: 'dn2'}
yum 一键安装多个软件  tree sl nginx httpd vsftpd dhcp

- name: play2
  hosts: 192.168.233.12
  gather_facts: false
  tasks:
   - name: create tree sl nginx httpd vsftpd dhcp
     yum:
       name: "{
  { item }}"
     with_list:
       - tree
       - sl
       - nginx
       - httpd
       - vsftpd
       - dhcp

相关推荐

  1. ansible剧本playbook

    2023-12-21 18:00:03       58 阅读
  2. Ansible剧本playbooks

    2023-12-21 18:00:03       43 阅读
  3. Ansible剧本playbooks详解

    2023-12-21 18:00:03       35 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-21 18:00:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-21 18:00:03       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-21 18:00:03       82 阅读
  4. Python语言-面向对象

    2023-12-21 18:00:03       91 阅读

热门阅读

  1. 【力扣100】108.将有序数组转化为二叉搜索树

    2023-12-21 18:00:03       66 阅读
  2. 数据结构的基本概念

    2023-12-21 18:00:03       52 阅读
  3. win10 golang下载安装,及环境变量配置

    2023-12-21 18:00:03       67 阅读
  4. TypeScript常用的内置工具

    2023-12-21 18:00:03       58 阅读
  5. 客户服务常用的ChatGPT通用提示词模板

    2023-12-21 18:00:03       65 阅读
  6. 11、Qt:创建/删除文件夹、拷贝文件

    2023-12-21 18:00:03       45 阅读
  7. copilot运用技巧和实战经验分享

    2023-12-21 18:00:03       51 阅读
  8. Golang leetcode977 有序数组的平方 双指针法

    2023-12-21 18:00:03       77 阅读