Enterprise-level automated operation and maintenance artifact Ansible common module

ad hoc fact, perform a simple command - a command. For complex command was playbook.

帮助文档:
列出ansible支持的模块:
-l:获取列表
-s module_name:获取指定模块的使用信息
看所有模块(A10,华为,docker,EC2,aws等等广大厂商设备)
[root@ansible-server ~]# ansible-doc -l
查看模块使用信息,了解其功能:
[root@ansible-server ~]# ansible-doc -s yum

Common Module

1.远程复制备份模块:copy
模块参数详解:  
src=:指定源文件路径
dest=:目标地址(拷贝到哪里)
owner:指定属主
group:指定属组
mode:指定权限,可以以数字指定比如0644
backup:在覆盖之前将原文件备份,备份文件包含时间信息。有两个选项:yes|no
[root@ansible-server ~]# vim a.txt  #创建一个测试文件
123123
[root@ansible-server ~]# ansible weball -m copy -a 'src=/root/a.txt dest=/opt owner=root group=root mode=644' -o
[root@ansible-server ~]# vim a.txt  #追加如下内容
123123
234234
[root@ansible-server ~]# ansible weball -m copy -a 'src=/root/a.txt dest=/opt/ owner=root group=root mode=644 backup=yes' -o
注释:如果文件没有变化,不会备份。只有文件内容不同,才会做备份。

登录被控制机器其中一台查看
[root@ansible-web1 ~]# cat /opt/a.txt.15301.2019-09-01\@00\:35\:18~

[root@ansible-server ~]# ansible weball -m shell -a 'mv /mnt/qf.txt /tmp' -o
移动被控制节点的文件
1.远程复制备份模块:copy
模块参数详解:  
src=:指定源文件路径
dest=:目标地址(拷贝到哪里)
owner:指定属主
group:指定属组
mode:指定权限,可以以数字指定比如0644
backup:在覆盖之前将原文件备份,备份文件包含时间信息。有两个选项:yes|no
[root@ansible-server ~]# vim a.txt  #创建一个测试文件
123123
[root@ansible-server ~]# ansible weball -m copy -a 'src=/root/a.txt dest=/opt owner=root group=root mode=644' -o
[root@ansible-server ~]# vim a.txt  #追加如下内容
123123
234234
[root@ansible-server ~]# ansible weball -m copy -a 'src=/root/a.txt dest=/opt/ owner=root group=root mode=644 backup=yes' -o
注释:如果文件没有变化,不会备份。只有文件内容不同,才会做备份。

登录被控制机器其中一台查看
[root@ansible-web1 ~]# cat /opt/a.txt.15301.2019-09-01\@00\:35\:18~

[root@ansible-server ~]# ansible weball -m shell -a 'mv /mnt/qf.txt /tmp' -o
移动被控制节点的文件
3.服务管理service模块
[root@ansible-server ~]# ansible webservers1 -m service -a "name=httpd state=started" #启动
[root@ansible-server ~]# ansible webservers1 -m service -a "name=httpd state=stopped" #停止
[root@ansible-server ~]# ansible webservers1 -m service -a "name=httpd state=restarted" #重启
[root@ansible-server ~]# ansible webservers1 -m service -a "name=httpd state=started enabled=yes" #开机启动
[root@ansible-server ~]# ansible webservers1 -m service -a "name=httpd state=started enabled=no"  #开机关闭
4.文件模块file
模块参数详解:  
owner:修改属主
group:修改属组
mode:修改权限
path=:要修改文件的路径
recurse:递归的设置文件的属性,只对目录有效
        yes:表示使用递归设置
state:
touch:创建一个新的空文件
directory:创建一个新的目录,当目录存在时不会进行修改
#创建一个文件
[root@ansible-server ~]# ansible webservers1 -m file -a 'path=/tmp/youngfit1.txt mode=777 state=touch'
[root@ansible-server ~]#  ansible ansible-web2 -m file -a 'path=/tmp/youngfit2.txt mode=777 owner=nginx state=touch'
#创建一个目录
[root@ansible-server ~]# ansible webservers1 -m file -a 'path=/tmp/qf mode=777 state=directory' 

被控节点ansible-web2操作:
[root@ansible-web2 opt]# ll haha
total 0
-rw-r--r--. 1 root  root 0 Sep 12 09:41 haha2.txt
-rw-r--r--. 1 nginx root 0 Sep 12 09:41 haha.txt

[root@ansible-server ~]# ansible ansible-web2 -m file -a "path=/opt/haha owner=nginx group=nginx  state=directory recurse=yes"

被控节点操作:
[root@ansible-web2 opt]# ll haha
total 0
-rw-r--r--. 1 nginx nginx 0 Sep 12 09:41 haha2.txt
-rw-r--r--. 1 nginx nginx 0 Sep 12 09:41 haha.txt
5.收集信息模块setup
[root@ansible-server ~]# ansible webservers1 -m setup  #收集所有信息
[root@ansible-server ~]# ansible webservers1 -m setup -a 'filter=ansible_all_ipv4_addresses' #只查询ipv4的地址
filter:过滤
group模块参数:
name参数:必须参数,用于指定组名称。
state参数:用于指定组的状态,两个值可选,present(新增),absent,默认为 present,设置为absent 表示删除组。
gid参数:用于指定组的gid。如果不指定为随机
system参数:如果是yes为系统组。--可选

1.创建多个play
[root@ansible ~]# cd /etc/ansible/
[root@ansible ansible]# vim play.yml
- hosts: webservers1
  user: root
  tasks:
  - name: create a group
    group: name=mygrp gid=2003 system=true
  - name: create a user
    user: name=tom group=mygrp system=true

- hosts: webservers2
  user: root
  tasks:
  - name: install apache
    yum: name=httpd state=latest
  - name: start httpd service
    service: name=httpd state=started
    

Here Insert Picture Description

2.条件执行when模块
先判断when条件是否成立
[root@ansible ansible]# cat /etc/ansible/hosts
[webservers1]
ansible-web1
ansible-web2

[root@ansible ansible]# vim when.yml
- hosts: webservers1
  user: root
  tasks:
  - name: use when
    file: state=touch path=/tmp/when.txt
  - name: insert data
    shell: echo 123 >> /tmp/when.txt          #2在执行这个模块命令
    when: ansible_hostname == "ansible-web1"  #1.先条件执行,先判断when是否成立,如果成立则执行上面命令,ansible-web1指的是被控节点上真正的主机名称

Here Insert Picture Description

3.使用变量并不显示搜集主机相关信息
gather_facts参数:指定了在任务部分执行前,是否先执行setup模块获取主机相关信息,默认值为true,改成false之后在执行过程中不会搜集主机相关信息。 
==========================================
[root@ansible an sible]# vim create_user.yml
- hosts: ansible-web1
  user: root
  gather_facts: false  #是否执行setup模块,搜集对方机器的信息
  vars:                #自定义变量
  - user: "jack"       #user是自定义变量名称,“jack”是变量值
  - src_path: "/root/a.txt"    #同上
  - dest_path: "/mnt/"
  tasks:
  - name: create user
    user: name={{ user }}
  - name: copy file
    copy: src={{ src_path }} dest={{ dest_path }}
[root@ansible ansible]# vim /root/a.txt  #创建测试文件
123

Here Insert Picture Description

执行:
[root@ansible ansible]# ansible-playbook create_user.yml
Published 48 original articles · won praise 18 · views 3640

Guess you like

Origin blog.csdn.net/wx912820/article/details/104994532