ansible自己练习,有点乱,ansible练习有点乱


一片比较简单但是比较完成的ansible介绍文档,大家可以看一下http://www.mamicode.com/info-detail-1428476.html

 

配置被管理主机的分组名称,IP地址,端口号,秘钥文件,ssh连接用户

[testhost]        //分组名称

10.200.7.59:9999  //IP地址,端口号,  ansible_ssh_private_key_file=/etc/ansible/sshkey/common/10.200.7.59/sysadmin ansible_ssh_user=sysadmin //秘钥文件,ssh连接用户

  

模块synchronize 从被控制主机拉取文件到控制机,  -l  --limit限制,只管理主机

ansible hostIP-m synchronize -a ‘mode=pull src=/tmp/a dest=/root/‘  -l  1.1.1.1

 

使用定时模块

ansible testhost -m cron -a "name='test_cron' job='/bin/touch /tmp/test.txt'  hour='1,5,10' weekday=1"

 

Ansible-playbook

YAML最关键的部分为列表 字段 和映射

列表的所有元素均使用“-”打头,例如:
# A list of tasty fruits
- Apple
- Orange

一定要做好缩进,通过空格进行排序,排列整齐的认为是同一级别的

 

参数交互

ansible-playbooke33_var_in_command.yml --extra-vars "{'hosts':'vm-rhel7-1', 'user':'root'}"

 

gather_facts参数指定了在以下任务部分执行前,是否先执行setup模块获取主机相关信息,这在后面的task会使用到setup获取的信息时用到;

创建用户

- name: create_user

  hosts: testhost

  user: root

  gather_facts: false

  vars:

    - user: "msiyuetian"

 

0.user模块

  tasks:

    - name: create user

      user: name="{{ user }}"

1.debug模块

debug模块使用很简单.它具有两个参数,msg和fail.msg就是打印出来的信息,而当fail参数设置为yes时,会发送失败通知给ansible,然后ansible会停止运行任务.

- name: debug

  Debug:

    Msg: {{ message.stdout }}

2.check模式

ansible还提供了check模式和diff模式.只需要执行playbook时添加参数–check和–diff.check模式运行时,ansible不会真正控制远程机器发生变更.这能够让你获得这次playbook任务中,将会发生changed事件的列表.


3.设置使用系统变量

- name:设置ROOT用户的SU密码

      set_fact:

        ansible_become_pass:abc


4yaml中包含另一个yaml表格

[root@testA httpd]# cat httpd.yaml

- name: conf

  remote_user: root

  hosts: a

  tasks:

    include: tasks/main.yaml

  handlers:

    include: handlers/main.yaml

 

5.service 模块及定义handlers(内容和正常书写一直)

[root@testA handlers]# vim main.yaml

内容如下:

  - name: restart httpd

    service: name=httpd enabled=yes state=restarted

 

6.tags模块

在playbook中 其中一个选项 -t,-t表示可以执行某些特定标签对应的tasks

如果第二次执行的时候就没有必要去重新yum安装

ansible-playbook httpd.yaml -t conf

  - name: conf file

    copy: src=/opt/test/httpd/httpd.conf dest=/etc/httpd/conf/httpd.conf

    tags: conf

notify: restart httpd

    

7.定义yum模块的使用,并且使用条件when,即 命令为yum

  - name: install     #定义第一个任务名

    yum: name=httpd state=present

      when: ansible_pkg_mgr == "yum"

       when: ansible_os_family == "RedHat"

 

8.notigy 和 handlers模块方法的使用

    - name: test copy

      copy: src=/etc/passwd dest=/tmp/handlers.txt

      notify: test handlers

  handlers:

    - name: test handlers

      shell: echo "msiyuetian.blog.51cto.com" >> /tmp/handlers.txt

说明:只有 copy 模块真正执行后,才会去调用下面的 handlers 相关的操作,追加内容。也就是说如果 src 和 dest 内容是一样的,并不会去执行 handlers 里面的 shell 相关命令。所以这种比较适合配置文件发生更改后,需要重启服务的操作。我理解notify是在做比较。

 

9.Ansible-playbook使用sudo

- hosts: webnodes

  tasks:

    - name: test ping connection:

    remote_user: test

    sudo: yes

 

10.参数的使用

    - name: change mode for files

      file: path=/tmp/{{ item }} mode=600 owner=root group=root

      with_items:

        - 1.txt

        - 2.txt

这里变量必须是以{{item}}进行标记,而调用变量必须是with_items:进行赋值

 

补充:在使用with_items的时候还可以使用hashes

user:name={{item,name}} state=present groups={{item.group}}

with_items:

  -{name:'test1',gourp: 'wheel'}

  -{name:'test2',gourp: 'root'}

 

11.Register模块保存结果

   - name: Get /tmp info

     file: dest=/tmp state=directory

     register: tmp   //获取源文件的一些属性信息

   - name: Set mode on /var/tmp

     file: dest=/tmp/subtmp mode={{ tmp.mode }} state=directory  // 创建文件夹

 

12.prompt交互方式获取变量值

- hosts: server

  vars_prompt:

    - name: web

      prompt: 'Please input the web server:'

      private: no

  tasks:

    - name: concent of the file test

      template: src=/root/test dest=/tmp/test

查看/root/test文件

 

在一个主机上面只执行一次一个任务.”run_once”来实现:run_once: true

在本地运行程序:delegate_to:localhost  

 

 

 

 

 

 

 

 

相关内容

    暂无相关文章