ansible

一、ansible的安装

1、yum源在线安装

在网易镜像上下载.repo文件,添加到本地的yum源目录下

http://mirrors.163.com/.help/centos.html

2、检测是否安装正确:ansible --version

3、配置文件的查看

/etc/ansible/ansible.cfg  //ansible的主配置文件

/etc/ansible/hosts   //这个配置文件就是默认主机清单配置文件

4、除了以上两个重要的配置文件还有三个重要的可执行文件分别是:
ansible 主执行程序,一般用于命令行下执行
ansible-playbook 执行playbook中的任务
ansible-doc 获取各模块的帮助信息


二、基本使用

1、定义主机组

host_key_checking = False    //修改主配置文件,不需要检测key文件,直接登陆

ansible web --list-hosts   //检测web包含哪些主机   

[web]  //定义组名

192.168.4.85 ansible_ssh_user="root" 

ansible_ssh_pass="123456" ansible_ssh_port="22"

192.168.4.86 ansible_ssh_user="root" 

ansible_ssh_pass="123456" ansible_ssh_port="22"

//上面定义了IP地址,远程的用户,远程的密码以及端口

2、基本的命令用法

ansible web -m ping   测试是否通信

ansible all -m command -a 'hostname' -k    

 //远程执行命令,-m调用的模块,-a调用的命令,-k交互式输入密码

3、批量部署密钥  ansible-doc -l | grep auth  //查看安全模块

ansible all -m authorized_key -a "user=root exclusive=true manage_dir=true key='$(< /root/.ssh/id_rsa.pub)'" -k -v


//把生成的公钥推送到所有的主机,  exclusive=true文件强制覆盖掉


三、常用的模块:

   1、ping模块:测试主机是否是通的,用法很简单,不涉及参数:

 ansible web -m ping
2、command 模块 远程执行命令  管道的命令不能执行
ansible all -m command -a "ifconfig"
3、shell模块:由于commnad只能执行裸命令(即系统环境中有支持的命令),至于管道之类的功能不支持,
     shell模块可以做到
    ansible all -m shell -a 'cat /etc/passwd | wc -l' -v
4、script 模块:把本地的脚本传到远端执行;前提是到远端可以执行。
    ansible all -m script -a '/root/a.sh'
5、 copy模块:不适合大的文件
     ansible web -m copy -a 'src=/etc/passwd dest=/root'
6、 lineinfile   |   replace  模块:line配置整行,replace匹配选中的,直接修改文件
     ansible s87 -m lineinfile -a 'path="/etc/sysconfig/network-scripts/ifcfg-eth0" regexp="^BOOTPROTO=" line="BOOTPROTO=static"'
    ansible s87 -m replace -a 'path="/etc/sysconfig/network-scripts/ifcfg-eth0" regexp="^(BOOTPROTO=).*" replace="\1none"'
7、 yum 模块:state(present/安装     absent/卸载   latest/更新)
  ansible web -m yum -a 'name=httpd state=present' -v
8、service 模块:用于管理服务
  state( started,stopped,restarted,reloaded),name=服务名称,enabled=yes|no
  ansible web -m service -a 'name="httpd" enabled=yes state=started'
9、file模块:对远程文件管理的模块
 state:
       touch:创建一个新的空文件
       directory:创建一个新的目录,当目录存在时不会进行修改
       link:创建软连接,结和src一起使用此选项才生效
       hard:创建硬连接
       absent:删除文件,目录,软连接
   ansible web -m file -a 'path=/tmp/test.txt state=touch'
 

四、playbook的基本使用

        playbook 是 ansible 用于配置,部署,和管理托管主机剧本。通过 playbook 的详细描述,执行其中的一系

列 tasks,可以让进端主机达到预期的状态。执行一些简单的任务,使用ad-hoc命令可以方便的解决

问题,但是有时一个设施过于复杂,需要大量的操作时候,执行的 ad-hoc 命令是不适合的,这时最好使用

playbook,就像执行 shell 命令不写 shell 脚本一样,也可以理解为批处理任务

    play 中 hosts,variables,roles,tasks 等对象的表示方法都是键值中间以 ": " 分隔表示

playbook 构成:

    Target: 定义将要执行 playbook 的进程主机组

    Variable: 定义 playbook 运行时需要使用的变量

   Tasks: 定义将要在进程主机上执行的任务列表

   Handler: 定义 task 执行完成以后需要调用的任务

1、第一个playbook,vim myping.yml

---                                  # 第一行,表示开始

- hosts: all

  remote_user: root

 tasks:

  - ping:

ansible-playbook myping.yml -f 5

-f 并发进程数量,默认是 5

hosts 行的内容是一个或多个组或主机的 patterns,以逗号为分隔符

remote_user 就是账户名

2、playbook 执行命令给所有主机添加用户 plj,设置默认密码 123456

     – 要求第一次登录修改密码

---

- hosts: all

  remote_user: root

  tasks:

    - name: create user plj

      user: group=wheel uid=1000 name=plj

    - shell: echo 123456 | passwd --stdin plj

    - shell: chage -d 0 plj

3、编写 playbook 实现以下效果

– 安装 apache      – 修改 apache 监听的端口为 8080

– 为 apache 增加 NameServer 配置   – 设置默认主页 hello world

– 启动服务         – 设置开机自启动

- hosts: web

  remote_user: root

  tasks:

    - yum: name=httpd

    - name: config

      copy:

        src: /root/httpd.conf

        dest: /etc/httpd/conf/httpd.conf

      notify:       //notify模块:触发器

        - restart httpd

  handlers:     // task 执行完成以后需要调用的任务

    - name: restart httpd

      service: name=httpd state=restarted

4、变量

 给所有主机添加用户 plj,设置默认密码 123456

 要求第一次登录修改密码(使用变量)

---

- hosts: web

  remote_user: root

  vars:

   username: haha

 tasks:

   - user: name={{username}} group=users password={{'123456' | password_hash('sha512')}}

   - shell: chage -d 0 {{username}}

 //变量过滤器 password_hash

5、ansible-playbook 对错误的处理

默认情况判断 $?,如果 值 不为 0 就停止执行

但某些情况我们需要忽略错误继续执行

如:

我们要关闭 selinux,如果 selinux 已经是关闭的,返

回 1 ,但我们的目的就是关闭,已经关闭不算错误,

这个情况我们就需要忽略错误继续运行,忽略错误有

两种方法:

shell: /usr/bin/somecommand || /bin/true

- name: run some command

  shell: /usr/bin/somecommand

  ignore_errors: True

完整的例子如下:

---

- hosts: web

  remote_user: root

  vars:

    username: plj

  tasks:

    - name: create user "{{username}}"

      user: group=wheel uid=1010 name={{username}} password={{'123456'|password_hash('sha512')}}

    - shell: setenforce 0

      ignore_errors: true

    - shell: chage -d 0 {{username}}

6、when:某些时候我们可能需要在满足特定的条件后在触发某

一项操作,戒在特定的条件下织止某个行为,这个时候我们就需要迚行条件判断,

when 正是解决这个问题的最佳选择,进程中的系统变量 facts 变量作为

when 的条件,这些 facts 我们可以通过 setup 模块查看

---

- name: Install VIM

  hosts: all

  tasks:

- name: Install VIM via yum

  yum: name=vim-enhanced state=installed

  when: ansible_os_family == "RedHat"

- name: Install VIM via apt

  apt: name=vim state=installed

  when: ansible_os_family == "Debian"

7、register:保存shell命令的执行结果

 有时候我们可能还需要更复杂的例子,比如判断前一

个命令的执行结果,根据结果处理后面的操作,这时候我们就需要 register 

模块来保存前一个命令的返回状态,在后面进行调用

---

- hosts: web

  remote_user: root

  vars:

    username: plj

  tasks:

    - shell: id {{username}}

      register: result

    - name: change "{{username}}" password

      user: name={{username}} password={{'12345678'|password_hash('sha512')}}

      when: result

 

---

  - hosts: db

    remote_user: root

    tasks:

      - shell:  uptime | awk '{printf $(9)}' | cut -b '1-4'

        register: result

      - service: name=httpd state=stopped

        when: result.stdout | float > 0.5

8、with_items 是 playbook 标准循环

为不同用户定义不同组

 ---

 - hosts: db

   remote_user: root

   tasks:

      - user:

        name: "{{item.name}}"

        groups: "{{item.group}}"

     with_items:

       - {name: 'nb', group: 'root'}

       - {name: 'dd', group: 'root'}

       - {name: 'jj', group: 'usetr'}

       - {name: 'hh', group: 'usetr'}

9、tags:给指定的任务定义一个调用标识


---

- hosts: web

  remote_user: root

  vars:

    soft: httpd

  tasks:

    - name: install {{soft}}

      yum: name={{soft}}

    - name: config httpd.conf

      copy: src=/root/playbook/httpd.conf dest=/etc/httpd/conf/httpd.conf

    - name: config services

      service: enabled=yes state=restarted name={{soft}}

      tags: restartweb

10、debug:

    ansible-playbook --syntax-check playbook.yaml   //语法检测

   ansible-playbook -C playbook.yaml      //测试运行

























     
































發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章