前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Ansible-playbook 基本语法与实例(学习笔记十九)

Ansible-playbook 基本语法与实例(学习笔记十九)

作者头像
用户5760343
发布2022-05-24 16:39:09
2890
发布2022-05-24 16:39:09
举报
文章被收录于专栏:sktjsktj

1、安装apache,做初始配置,并启动服务:

这个是你选择的主机

  • hosts: webservers

这个是变量

vars:

代码语言:javascript
复制
http_port: 80

max_clients: 200

远端的执行权限

remote_user: root

tasks:

利用yum模块来操作

  • name: ensure apache is at the latest version yum: pkg=httpd state=latest
  • name: write the apache config file template: src=/srv/httpd.j2 dest=/etc/httpd.conf

触发重启服务器

代码语言:javascript
复制
notify:

- restart apache
  • name: ensure apache is running service: name=httpd state=started

这里的restart apache 和上面的触发是配对的。这就是handlers的作用。相当于tag

handlers:

代码语言:javascript
复制
- name: restart apache

  service: name=httpd state=restarted

2、可以同时使用10个进程进行,调用格式为: ansible-playbook test.yml -f 10

3、对于没有把握执行的任务,需要加上 ignore_errors: True,这样即使出错,下一个任务也会继续执行

4、ansible-playbook可以根据上一个任务的执行结果,来判断执行下一个任务,系统参数为when: tasks:

  • shell: /usr/bin/foo register: foo_result ignore_errors: True
  • name: "cmd" action: command touch /root/kkkkk when: foo_result.rc == 127

5、根据操作系统类型,执行不同安装:

  • hosts: all remote_user: root gather_facts:True tasks:
  • name: install apache on CentOS yum: name=httpd state=present when: ansible_os_family =="CentOS"
  • name: install apache on Debian yum: name=apache2 state=present when: ansible_os_family =="Debian"

6、notify和handlers来执行触发,handlers可多次调用:

  • name: template configuration file template: src=template.j2 dest=/etc/foo.conf notify:
    • restart memcached
    • restart apache handlers:
    • name: restart memcached service: name=memcached state=restarted
    • name: restart apache service: name=apache state=restarted

7、执行多个变量,每个变量为一次任务: tasks:

  • name: panduan command: echo {{ item }} with_items: [ 0,2,4,6,8,10 ]

8、特定任务用特定用户执行:

  • hosts: webnodes remote_user: mageedu tasks:
    • name: test connection ping: remote_user: mageedu sudo: yes

9、捕获执行中的错误,并进行补救: tasks:

  • block:
    • name: Create {{ maildir_path }} copy: src: "{{ maildir }}" dest: "{{ maildir_path }}" mode: 0755 register: command_output rescue:
    • name: Install mail packages yum: name: "{{ item }}" state: latest with_items:
      • "{{ mail_package }}"
      • dovecot always:
    • name: start the mail service service: name: "{{ mail_service }}" state: restarted

10、对任务做tag标记,可以只执行某个tag,执行语法为:ansible-playbook -t TAGS_NAME playbook.yaml

  • hosts: all #所有远程主机 remote_user: root #以远程主机上root用户执行 tasks: #任务
    • name: install redis #任务之安装 yum: name=redis state=latest #动作调用yum模块安装
    • name: copy config file #任务之复制同步配置文件到远程目标主机 copy: src=/root/playbooks/redis.conf dest=/etc/redis.conf owner=redis #动作copy模块执行 notify: restart redis #触发的动作 tags: configfile #任务标记名configfile

11、从外部传入变量,ansible-playbook的格式为:ansible-playbook tomcat-install.yml --extra-vars "{'host':'192.168.11.111', 'tomcat_home':'/opt/tomcat-test', 'url':'http://www.apache.com/tomcat-7.0.57.tar.gz'}"


  • name: Tomcat install and configuration hosts: "{{ host }}" user: root

vars:

代码语言:javascript
复制
tomcat_home: "{{ tomcat_home }}" 

tasks:

代码语言:javascript
复制
- name: absent old tomcat

  file: path={{ item }} state=absent

  with_items:

    - "{{ tomcat_home }}"

    - /geelyapp/auto_scripts/tomcat.sh

- name: get tomcat_tar_gz

  get_url: url={{ url }} dest=/tmp/tomcat.tar.gz

- name: Create the dir

  file: path={{ item }} state=directory

  with_items:

    - /geelyapp/gc_log

    - /geelyapp/auto_scripts

    - "{{ tomcat_home }}"
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-05-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 这个是你选择的主机
  • 这个是变量
  • 远端的执行权限
  • 利用yum模块来操作
  • 触发重启服务器
  • 这里的restart apache 和上面的触发是配对的。这就是handlers的作用。相当于tag
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档