ansible模块与基础命令

ansible基础命令,与常用模块介绍

基础命令

​ 常用命令有ansible、ansible-doc、ansible-playbook、ansible-vault、ansible-console、ansible-galaxy、ansible-pull

ansible-doc

ansible-doc -l
列出所有模块

ansible-doc ping
查看ping模块的用户
ansible-doc -s ping
查看ping模块的

[root@host2 ~]# ansible-doc -l |grep yum
yum                                                           Manages packages with the...
yum_repository                                                Add or remove YUM reposit...
[root@host2 ~]# ansible-doc -s yum

ansible-galaxy

连接https://galaxy.ansible.com下载官方模版的roles

ansible-galaxy list
列出所有已经安装的galaxy

ansible-galaxy install geerlingguy.redis
安装特定的role

ansible-galaxy remove geerlingguy.redis
删除特定的role

[root@host2 ~]# ansible-galaxy install geerlingguy.redis
- downloading role 'redis', owned by geerlingguy
- downloading role from https://github.com/geerlingguy/ansible-role-redis/archive/1.6.0.tar.gz
- extracting geerlingguy.redis to /root/.ansible/roles/geerlingguy.redis
- geerlingguy.redis (1.6.0) was installed successfully

[root@host2 ~]# ansible-galaxy list
# /root/.ansible/roles
- geerlingguy.redis, 1.6.0
# /usr/share/ansible/roles
# /etc/ansible/roles
[root@host2 ~]# ansible-galaxy remove geerlingguy.redis
- successfully removed geerlingguy.redis

ansible-pull

[root@host2 ~]# ansible-pull -h

pulls playbooks from a VCS repo and executes them for the local host

ansible-playbook

支持playbook的命令
ansible-playbook hello.yaml

#示例:
[root@host2 ~]# ansible-playbook hello.yaml 

PLAY [192.168.80.102] **********************************************************************

TASK [Gathering Facts] *********************************************************************
ok: [192.168.80.102]

TASK [hello] *******************************************************************************
changed: [192.168.80.102]

PLAY RECAP *********************************************************************************
192.168.80.102             : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@host2 ~]# cat hello.yaml 
# hello yaml file for test
- hosts: 192.168.80.102
  remote_user: root
  tasks:
   - name: hello 
     command: /usr/bin/wall hello

ansible-vault

#加密,解密 yaml文件

[root@host2 ~]# ansible-vault -h
usage: ansible-vault [-h] [--version] [-v]
                     {create,decrypt,edit,view,encrypt,encrypt_string,rekey}
                     ...

encryption/decryption utility for Ansible data files
#加密,解密hello.yaml
[root@host2 ~]# ansible-vault encrypt hello.yaml 
New Vault password: 
Confirm New Vault password: 
Encryption successful
[root@host2 ~]# cat hello.yaml 
$ANSIBLE_VAULT;1.1;AES256
34626336666535313962643166333633633238393130373036313563626263666439643930643666
3962623939393332373939396435643766613935346166340a653230333165373562333333613637
64393738396539356266613933333738353565313038333734336363326637666565393437303964
3330626562303732380a313366396461326563633736323033383534636630356465323133376361
33633964633533646262333430333030663534303133636131643539663937303566373363393930
36363362383363333361616632613264323934663530663532333465303138313361643539393332
39366430333839336662336266363864313130663062613631353565633134613430396232383266
37323737363465356633313664306165326165366533623065636131333632633335613436356131
34313334616264313265396161323137306335346437303939393261613538353564356237366231
31333635343262616262636232366435396631343033393031326631313439653461313163613132
393332363334356634666633323030383861

[root@host2 ~]# ansible-vault decrypt hello.yaml 
Vault password: 
Decryption successful
[root@host2 ~]# cat hello.yaml 
# hello yaml file for test
- hosts: 192.168.80.102
  remote_user: root
  tasks:
   - name: hello 
     command: /usr/bin/wall hello

ansible-console

2.0后新增的交互式窗口

[root@host2 ~]#  ansible-console
Welcome to the ansible console.
Type help or ? to list commands.
#分别表示为
当前执行任务的身份@主机组	当前主机数 并发数5 

#修改并发为10
root@all (2)[f:5]$ forks 10

#切换主机组为all
root@all (2)[f:10]$ cd all

#列出所有主机
root@all (2)[f:10]$ list
192.168.80.102
192.168.80.103

#查看帮助
root@all (2)[f:10]$ ?

交互式窗口,执行ansible任务,以root身份,对所有主机,执行安装httpd操作,并发为10
root@all (2)[f:10]$ yum name=httpd state=present

ansible

ansible通过ssh服务连接各个被管理端,实现配置管理、应用部署、任务执行等,因此各个被管理端应该做ansible节点的免密登陆

ansible --help

-m 指定使用的模块,默认为command
--list-hosts 列出主机列表
-C ,检查,不实际执行
-u 执行远程命令的用户
-b sudo切换的用户身份

1、ansible的host-pattern

all表示主机清单中所有主机
ansible all -m ping
#对主机清单中所有主机用命令模块的ping测试连通性

*通配符
ansible “*” -m ping
ansible 192.168.80.* -m ping
ansible "*webservers" -m ping
#利用通配符匹配一批主机

或
ansible "web1:app1" -m ping
ansible "192.168.80.1:192.168.80.2" -m ping

与:
ansible "webs:&dbs" -m ping
在webs组和在dbs组的主机

非:
ansible "webs:!dbs" -m ping
在webs组,但不在dbs组的机器

正则:
ansible "~(web|db).*\.magedu\.com" -m ping

ansible命令执行过程

  1. 加载自己的配置文件,默认/etc/ansible/ansible.cfg
  2. 加载对应模块文件,如command
  3. 通过ansible将模块生成的临时py文件,将将其传输到远程服务器的对应执行用户的目录:$HOME/.ansible/tmp/ansible-tmp-数字/xxx.py文件
  4. 给文件+x执行权限
  5. 执行并返回结果
  6. 删除临时py文件,退出

执行状态

  • 绿色:执行成功,不需要改变目标主机
  • 黄色:执行成功,需要改变目标主机状态
  • 红色:执行失败

ansible命令行示例

1、对all所有主机做ping探测,以root用户身份,若以其他身份如wang,则需要各个主机上存在该用户

# 添加2台主机:
[root@host2 ~]# vim /etc/ansible/hosts 
[test]
192.168.80.102
192.168.80.103


[root@host2 ~]# ansible all -m ping -u root
192.168.80.102 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
192.168.80.103 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

2、以wang sudo到root身份,做ping检测

[root@host2 ~]# ansible all -m ping -u wang -k -b

#节点上要有wang用户,且设置了密码,且设置了sudo权限,和sudo密码 -k是提示输入密码,-b是sudo,默认为root用户

3、以wang身份,连接80.102并sudo为root,执行ls命令

[root@host2 ~]# ansible 192.168.80.102 -m command -u wang -a "ls /root" -b --become-user=root -k -K
SSH password: 
先问的是wang用户的ssh登陆密码,(不能免密是因为只做root间的信任,wang用户没做)
BECOME password[defaults to SSH password]: 
再问的是wang sudo到root用户的密码
192.168.80.102 | CHANGED | rc=0 >>
anaconda-ks.cfg
ansible-1.5.4.tar.gz

#-k是wang用户的ssh连接密码,-K是问wang用户sudo到root用户的密码,(一般一样)

#对80.102的wang用户做了信任后,-k选项即可省略
root@host2 ~]# ssh-copy-id wang@192.168.80.102

[root@host2 ~]# ansible 192.168.80.102 -m command -u wang -a "ls /root" -b --become-user=root  -K
BECOME password: 
192.168.80.102 | CHANGED | rc=0 >>
anaconda-ks.cfg
ansible-1.5.4.tar.gz

ansible常用模块

  • command:在远程主机上执行linux命令,默认模块

    • ansible web1 -m command -a "service mysqld restart"
          
      ansible web1 -m command -a "echo wang |passwd --stdin wang"
      
  • shell:和command类似,远程主机用shell执行命令

  • shell和comman模块,不具备幂等性

    • ansible web1 -m shell -a "ls /root"
          
      复杂命令,一般写为脚本,分发到各个节点上,再执行,将结果拉回本地即可
      
  • script:远程主机上执行ansible服务器上的脚本

    • ansible web1 -m script -a /path/to/1.sh
      
  • copy模块:将文件从ansible节点复制到其他主机

    • ansible web1 -m copy -a "src=/root/1.sh dest=/tmp/2.sh owner=wang mode=600 backup=yes"
          
      若2.sh存在,则先备份,默认会覆盖
          
      ansible web1 -m copy -a "content= 'hellow\n' dest=/tmp/1.txt"
      源为一段字符串
      
  • fetch模块:从远程主机将文件拉取回本地

    • ansible web1 -m fetch -a "src=/root/1.sh dest=/data/1.sh"
          
      
  • file模块:设置文件属性

    • ansible web1 -m file -a "path=/root/1.sh owner=wang mode=777"
          
      ansible web1 -m file -a "src=/root/1.sh dest=/roto/1.sh state=link"
      
  • unarchive模块:

    • copy=yes将ansible主机上的压缩比解压缩后传到远程主机上,默认为yes
          
      copy=no将远程主机的某个压缩包解压到某路径
          
      src源路径,可以是ansible主机,可以是远程 主机,远程主机时搭配copy=no
          
      dest解压的目的路径
          
      mode解压缩后权限
          
      ansible srv -m unarchive -a "src=1.tar.gz dest=/var/lib/ copy=yes mode=777"
          
      ansible srv -m unarchive -a "src=1.tar.gz dest=/tmp copy=no mode=0777"
          
      ansible srv -m unarchive -a "src=https://www.b.com/1.tar.gz dest=/tmp copy=no mode=0777"
      
  • archive模块

    • ansible all -m archive -a "path=/etc/sysconfig dest=/tmp/sysconfig.tar.gz format=gz owner =wang mode=0777"
          
          
      
  • hostname,管理主机名

    • ansible server1 -m hostname -a "name=node1"
      
  • cron计划任务

    • ansible all -m cron -a "minute=*/5 job='/usr/sbin/ntpdate 172.16.0.1 &> /dev/null name=synctime'"
      创建任务
          
      ansible all -m cron -a "state=absent name=synctime"
      删除任务
          
      
  • yum等包管理模块

    • ansible all -m yum -a 'name=httpd state=present'
          
      ansible all -m yum -a 'name=httpd state=absent'
      
  • service模块

    • ansible all -m service -a 'name=httpd state=stopped'
          
      ansible all  -m service -a 'name=httpd state=started enabled=yes'
          
      ansible all -m service -a 'name=httpd state=restarted'
      
  • user管理用户

    • ansible all -m user -a 'name=user1 comment="test user" uid=1000 home=/app/user1 group=root'
          
      ansible all -m user -a 'name=user1 state=absent remove=yes'
          
      ansible all -m user -a 'name=user1 system=yes home=/home/user1'
      创建系统账户
      
  • group

    • ansible all -m group -a 'name=group1 system=yes'
          
      ansible all -m group -a 'name=group1 state=absent'
      
  • debug:输出自定义信息,变量值

  • fetch:从远端主机抓取模块

  • cron:计划任务定义

  • system:systemd风格服务管理

  • wait_for:用于判断某些任务执行前提的,如端口是否开始,文件是否存在等条件;

  • get_url:下载http或ftp的文件

  • scripts:将本地的脚本,复制到远端,再执行,利用的是远端的shell环境;

  • ansible -l |grep 你需要的模块
    # 可根据关键词过滤需要的模块
      
    ansible -s 你需要的模块
    # 模块语法,支持的参数值
    
updatedupdated2021-03-092021-03-09
加载评论