ansible的脚本

playbook剧本

1、playbook的组成

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

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

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

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

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

2、yaml文件的语法

vim test1.yaml

- name: first play
#定义剧本的名称,可以省略
  gather_facts: false
#表示在执行剧本之前是否收集目标主机的信息,false是不收集,可以加快执行速度。如果不写,默认就是收集
  hosts: 192.168.230.20
#指定目标主机,可以是组名也可以是ip地址
  remote_user: root
#在目标主机的执行用户
  tasks: 
    - name: test connection
#定义一个任务的名称,可以自定义
      ping:
#ping是模块名称
    - name: close selinux
      command: '/sbin/setenforce 0'
      ignore_errors: True
#如果在任务执行中报错,返回码非0报错,task就会停止,ignore_errors: True就会忽略错误,继续执行下一个任务
    - name: close firewalld
      service: name=firewalld state=stopped 
#调用service模块,关闭防火墙
    - name: install httpd
      yum: name=httpd state=latest
#latest表示安装当前库中的最新版本的软件
    - 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 

#运行
ansible-playbook test1.yaml

3、安装nginx

安装方式为yum,传一个配置文件到目标主机,修改默认端口为8080,访问页面内容this is nginx

vim test2.yaml

- name: first play
  gather_facts: false
  hosts: 192.168.230.30
  remote_user: root
  tasks:
    - name: test connection
      ping:
    - name: close selinux
      command: '/sbin/setenforce 0'
      ignore_errors: True
    - name: close firewalld
      service: name=firewalld state=stopped
    - name: install nginx
      yum: name=nginx state=latest
    - name: interview
      shell: echo "this is nginx" > /usr/share/nginx/html/index.html
    - name:
      copy: 'src=/opt/nginx.conf dest=/etc/nginx/'
      notify: restart nginx
  handlers:
    - name: restart nginx
      service: name=nginx state=restarted

#运行
ansible-playbook test2.yaml

4、定义变量,引用变量

脚本当中定义,以及脚本外传参

vim test3.yaml

- name: second play
  hosts: 192.168.230.30
  remote_user: root
  vars: 
    groupname: mysql
    username: nginx1
#定义变量
  tasks: 
    - name: create group
      group: 
        name: "{{ groupname }}"
        system: yes
        gid: 306
    - name: create user
      user:
        name: "{{ username }}"
        uid: 306
        group: "{{ groupname }}"

#运行
ansible-playbook test3.yaml

#往脚本里传参
ansible-playbook test1.yaml -e 'groupname=test1 username=test2'

#检查脚本语法是否有错
ansible-playbook test1.yaml --syntax-check

#检查脚本中有几个任务
ansible-playbook test1.yaml --list-task

#查看对哪些主机生效
ansible-playbook test1.yaml --list-hosts

#指定从哪个任务开始运行
ansible-playbook test1.yaml --start-at-task='create user' -e 'username=test3 groupname=test4'


#切换用户
- name: second play
  hosts: 192.168.230.30
  remote_user: dn
  become: yes
#先用普通用户执行,但是需要切换到其他的用户。例如切换到管理员
  become_user: root

5、在脚本中实现条件判断

when 满足条件的主机执行,不满足的跳过

vim test4.yaml

- name: this is if
  hosts: all
  remote_user: root
  tasks:
    - name: test when
      debug: msg='条件满足'
#debug相当于echo
      when: ansible_default_ipv4.address == "192.168.230.30"
      
ansible_default_ipv4.address != "192.168.230.30"   
#取反

#运行
ansible-playbook test4.yaml

6、循环结构

ansible有多种循环方式,一般都命名为with_items,定义循环的内容

#with_items 单循环输出

- name: item test
  hosts: 192.168.230.20
  remote_user: root
  gather_facts: false
  tasks:
    - debug:
        msg: "{{item}}"
      with_items:
        - [a,b,c,d]
        - [1,2,3,4]
      
输出item的值,with_items:a b c d 依次传入
with_list:整个列表作为一个整体进行输出
with_together:作为整体两两配对输出
with_nested:每一层都会遍历执行一遍输出结果

7、创建递归目录

条件判断,主机的ip=192.168.230.20才会执行,一次性创建4个文件 /opt/a /opt/b /opt/c /opt/d

vim test5.yaml
方法一:
- name:      
  hosts: 192.168.230.20
  gather_facts: false
  vars: 
    test:
      - /opt/test1
      - /opt/test2
      - /opt/test3
      - /opt/test4
  tasks:
    - name: create mulu
      file:
        path: "{{item}}"
        state: directory
      with_items: "{{test}}"

方法二:
- name:      
  hosts: 192.168.230.20
  gather_facts: false
  tasks:
    - name: create mulu
      file:
        path: "{{item}}"
        state: directory
      with_items: [/opt/test1,/opt/test2,/opt/test3/opt/test4]
      
#运行
ansible-playbook test5.yaml
### 回答1: 在Docker中执行Ansible脚本是一种使用Docker容器来运行Ansible脚本的方法。Docker是一个开源的容器化平台,可以帮助我们构建、部署和运行应用程序。Ansible是一个自动化工具,用于配置和管理组件、服务和网络。 要在Docker中执行Ansible脚本,需要按照以下步骤进行操作: 1. 首先,我们需要在Docker中安装Ansible。我们可以使用Dockerfile来定义镜像,其中包含Ansible的安装和配置。在Dockerfile中,指定Ansible的安装命令,例如使用pip安装: ``` FROM debian:latest RUN apt-get update && apt-get install -y \ python \ python-pip RUN pip install ansible ``` 2. 构建Docker镜像。在终端中执行以下命令,将Dockerfile构建为镜像: ``` docker build -t ansible-docker . ``` 3. 运行Docker容器。执行以下命令以运行刚刚构建的镜像: ``` docker run -it ansible-docker /bin/bash ``` 这将启动一个交互式终端,我们可以在其中执行Ansible脚本。 4. 在Docker容器中执行Ansible脚本。在容器中,我们可以使用ansible-playbook命令执行Ansible脚本。首先,将Ansible脚本和相关文件复制到容器中,然后执行ansible-playbook命令。 ``` docker cp playbook.yml <container_id>:/playbook.yml docker exec -it <container_id> ansible-playbook /playbook.yml ``` 其中,<container_id>是容器的唯一标识符,可以使用docker ps命令查找。 通过以上步骤,我们可以在Docker容器中运行Ansible脚本。使用Docker来运行Ansible脚本可以确保环境的一致性,并且可以更好地管理和部署应用程序。 ### 回答2: 在Docker中执行Ansible脚本是一种便捷的方式,能够快速部署和管理多个虚拟环境。 首先,我们需要在Docker中安装Ansible。可以通过在Dockerfile中添加相关指令来实现。例如,使用以下命令安装Ansible和相应的依赖: RUN apt-get update && \ apt-get install -y ansible 接下来,我们可以创建一个Ansible的剧本(playbook),指定我们要在Docker中执行的任务。 剧本可以使用yaml格式编写,其中包含了由Ansible执行的一系列任务。例如,假设我们的剧本需要在Docker容器中安装Nginx服务器: --- - name: Install Nginx on Docker hosts: all tasks: - name: Install Nginx apt: name: nginx state: present become: true 然后,我们可以使用ansible-playbook命令来在Docker中执行该剧本。在运行该命令之前,我们需要确保Docker容器已经启动。 可以使用以下命令在Docker容器中执行Ansible剧本: docker exec <container_id> ansible-playbook <playbook_file> 其中,<container_id>表示Docker容器的ID,<playbook_file>表示Ansible剧本的文件路径。 执行该命令后,Ansible将连接到指定的Docker容器,并执行剧本中定义的任务。在我们的例子中,Ansible将在Docker容器中安装Nginx服务器。 总结来说,通过在Docker中执行Ansible脚本,我们可以轻松地部署和管理多个虚拟环境,提高工作的效率和便捷性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值