Linux 第五本书 第二单元--------Ansible中常用模块

vim test.yml
【- name: test
   hosts: westos
   tasks:
      - name: check hostname
        shell: hostname

    ansible-playbook test.yml
     


     ansible westos --list                     ---显示主机列表
    

 

   ansible westos -m shell -a 'hostname' -k  ---提示输入ssh连接密码,默认key认证
     ansible westos -m shell -a 'touch /mnt/file' -k
     ansible westos -m shell -a 'touch /mnt/test' -C    ---预执行
     ansible westos -m shell -a 'touch /mnt/test' -T 2  ---执行命令超时时间
    
     ansible westos -m shell -a 'hostname'
    

     ansible-doc  -s shell
    

    cd .ansible/
 
     vim ansible.cfg
       【注释掉true,检测完需要恢复】     
     ansible westos -m shell -a 'whoami' -u westos -K    ----提示输入sudo密码
     
     ansible westos -m shell -a 'whoami' -u devops -K
     
     ansible westos -m shell -a 'whoami' -u devops -k -b ---执行sudo切换身份操作
     ansible westos -m shell -a 'whoami' -u devops -k -b   --become-user=root   ---指定sudo用户身份


====================================================
模块

1、command

 

ansible westos -m command -a 'hostname'         ---不用写cmd,就可执行

ansible westos -m command -a 'touch /mnt/file1'  ----创建file1

ansible westos -m command -a ' hostname creates=/mnt/file1'---如果存在则不运行
ansible westos -m command -a ' hostname removes=/mnt/file1'---如果存在则运行
ansible westos -m command -a 'chdir=/mnt pwd'  -----执行命令前提前进入指定目录

2、shell(和command类似)
 

不同点:
ansible westos -m shell -a 'rm -fr /mnt/*'     ---只有shell里可以删除command不能删除
ansible westos -m shell -a 'ps'     ----查看执行环境,默认为sh
ansible westos -m shell -a 'executable=/bin/bash ps ax | grep $$' --指定执行环境,

3、script(受控主机,远程执行脚本)


vim westos.sh        ---编写一个脚本
sh westos.sh         ---执行脚本
 ansible westos -m script -a './westos.sh'   ---远程运行脚本到受控机204上

 


4、copy(从主控机复制到受控机)

 

 ansible westos -m copy -a 'src=./westos.sh dest=/mnt/westos' 把westos.sh复制到受控机的/mnt 起名为westos

 ansible westos -m copy -a 'src=./westos.sh dest=/mnt/westos mode=700'
把westos.sh复制到受控机的/mnt 起名为westos 并赋予权限700
 
 ansible westos -m copy -a 'src=./westos.sh dest=/mnt/westos mode=700 group=westos owner=westos'
把westos.sh复制到受控机的/mnt 起名为westos 并赋予权限700,组改为westos 文件所有人为westos

 ansible westos -m copy -a 'content="hello westos"  dest=/mnt/westos backup=yes'
直接编写内容给到受控机/mnt,起名westos,在受控主机备份一份

 


5、fetch(从受控机复制到主控机)


ansible westos -m fetch -a 'src=/mnt/westos dest=/tmp'
从受控机复制westos到主控机/tmp
ansible westos -m fetch -a 'src=/mnt/westos dest=/tmp/www flat=yes'
从受控机复制westos到主控机/tmp起名为www,取消绝对路径

 

 

6、file


 
ansible westos -m file -a 'path=/mnt/westos1 state=touch'
在受控机建立文件westos1
ansible westos -m file -a 'path=/mnt/westos1 state=absent'
删除受控机里文件westos1
ansible westos -m file -a 'path=/mnt/westos1 state=directory'
在受控机建立目录westos1
ansible westos -m file -a 'path=/mnt/westos1 state=directory mode=777 owner=westos group=westos'
在受控机里建立一个满权限的目录westos1,拥有者和组均为westos

 

ansible westos -m file -a 'path=/mnt/westos1 state=directory mode=700 recurse=yes'
在受控机里建立一个递归目录westos1,其权限为700

 

 ansible westos -m file -a 'path=/mnt/westos1 state=touch'
在受控机里建立文件westos1
 ansible westos -m file -a 'src=/mnt/westos1 dest=/mnt/westos2 state=hard'
建立westos1和westos2的硬链接
 ansible westos -m file -a 'src=/mnt/westos1 dest=/mnt/westos3 state=link'
建立westos1和westos3的软链接

 

7、unarchive/archive(解压/压缩)

ansible westos -m archive -a 'path=/etc dest=/mnt/etc.tar.gz format=gz owner=westos mode=777'
主控机将/etc压缩到受控机的/mnt 拥有者为westos 满权限
tar jcf usr.tar.bz2 /usr/sbin/
将主控机/usr/sbin压缩
ansible westos -m unarchive -a 'src=./usr.tar.bz2 dest=/opt owner=westos copy=yes'
将主控机 usr.tar.bz2解压到受控机的/opt

 
ansible westos -m unarchive -a 'src=/mnt/etc.tar.gz dest=/opt remote_src=yes'
将受控机etc.tar.gz解压到受控机的/opt


8、 cron


 ansible westos -m hostname -a 'name=nodea.westos.org'
更改受控机主机名为nodea
 ansible westos -m cron -a 'job="touch /mnt/westosfile" name=test  minute=*/2 hour=14 '
 在受控机里设置计划任务14点每分钟建立一个westosfile
 ansible westos -m cron -a 'job="touch /mnt/westosfile" name=test   disabled=yes'
  在受控机里关闭计划任务


 ansible westos -m cron -a 'job="touch /mnt/westosfile" name=test   state=absent'
 删除计划任务

 

 


9、软件仓库源
 ansible westos -m file -a 'path=/etc/yum.repos.d/westos.repo state=absent'
 把原先的仓库删除
 ansible westos -m yum_repository -a 'file=name name=AppStream description="rhel8.2AppStream" baseurl=http://172.25.254.250/rhel8.2/AppStream gpgcheck=no enabled=yes gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release '
  给受控机里配置AppStream


 ansible westos -m yum_repository -a 'file=name name=BaseOS description="rhel8.2BaseOS" baseurl=http://172.25.254.250/rhel8.2/BaseOS gpgcheck=no enabled=yes gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release '
  给受控机里配置BaseOS
 ansible westos -m dnf -a 'name=httpd state=present'
  给受控机安装httpd
 ansible westos -m dnf -a 'name=httpd state=latest'
  删除受控机的httpd

 


 ansible westos -m dnf -a 'name=httpd state=absent autoremove=yes'
  删除受控机httpd的依赖性
 ansible westos -m dnf -a 'name=httpd state=latest enablerepo=AppStream'
 从AppStream中删除受控机httpd的依赖性

 

 要从网址上下载受控机也需要网络
ip route add default via 172.25.254.250
echo nameserver 114.114.114.114 > /etc/resolv.conf

查看隐藏组件  dnf group list --hidden

10、service/firewalld

 ansible westos -m service -a 'name=httpd state=started enabled=yes'
   把受控机安装好的httpd开启   


 ansible westos -m service -a 'name=httpd state=restarted enabled=yes'
   把受控机安装好的httpd重启
state:    started     开启
          stoped      停止
          restarted   重启
          reloaded    重新加载

 

 

 ansible westos -m firewalld -a 'zone=public service=http permanent=yes state=enabled immediate=yes'
   把受控机的火墙开启
  zone           火墙的域
  sevice          服务名
  permanent      永久生效
  state           状态
  immediate      立即生效

11、user

 

ansible westos -m user -a 'name=test'
 建立用户test
ansible westos -m user -a 'name=test uid=6666'
 使用户test的uid为6666
ansible westos -m user -a 'name=test group=westos'
 使用户test的组改为westos
ansible westos -m user -a 'name=test groups=westos'
 使用户test的附加组改为westos
ansible westos -m user -a 'name=test groups=72'
 更改test的附加组为72
ansible westos -m user -a 'name=test groups=westos append=yes'
 在附加组72再加附加组westos
ansible westos -m user -a 'name=test comment="test user"'
 给用户test加解释

openssl passwd -6
 生成密码密钥
ansible westos -m user -a "name=test password='\$6\$DifJmZTR.7w6DFOX$/K8xYETJhQtdnAtMmNXMfX.M30p04uGhExWlp2N7aVQJA4JzZKhwm3GsKoTBN4r4FpG/0l6qlAH/gkFSfXEAP1' generate_ssh_key=yes"
给用户密码生成密钥

ansible westos -m user -a 'name=test state=absent'
删除用户

12、group
 ansible westos -m group -a 'name=test'
 给受控机新建组名为test


 ansible westos -m group -a 'name=test gid=6666'
 改组id为6666

 

13、lineinfile

在受控机里/mnt建立文件内容如下:
hello westos
hello test
hello linux

 

主控机:
ansible westos -m lineinfile  -a 'path=/mnt/westos line="hello westos" '
给文件里加hello westos,因为文件里有,所以不执行

ansible westos -m lineinfile  -a 'path=/mnt/westos  regexp="^hello" line="hello westos" '
替换带有hello为hello westos ,这里替换了最后一行

 

ansible westos -m lineinfile  -a 'path=/mnt/westos  regexp="test" line="westos lee" '
把带有test的替换为westos lee

ansible westos -m lineinfile  -a 'path=/mnt/westos regexp="(h.{4}).*(w.{5})" line="\1" backrefs=yes '
将h后有四个字符w后 有五个字符的内容 只保留第一段

 

ansible westos -m lineinfile  -a 'path=/mnt/westos line="#########ok########"'
给最后一行添加#########ok########

ansible westos -m lineinfile  -a 'path=/mnt/westos  line="#########ok########" insertafter=EOF'
给最后一行添加#########ok########

ansible westos -m lineinfile  -a 'path=/mnt/westos  line="#########ok########" insertbefore=BOF'
给第一行添加#########ok########

ansible westos -m lineinfile  -a 'path=/mnt/westos  line="#########ok########" insertbefore=test'
给test前加#########ok########

playbook
vim test.yml
{
 - name: test
   hosts: westos
   tasks:
        - lineinfile:
                   path: /mnt/westos     -----传递到/mnt/westos
                   line: |+             -----换行
                      123
                      456
                      789

}
ansible-playbook test.yml


14、 replace/setup/debug

ansible westos -m replace -a 'path=/mnt/westos regexp="hello" replace="who" backup=yes'
 把hello代替为who
ansible westos -m setup
 查看远程主机的基本信息


ansible westos -m setup -a 'filter="ansible_all_ipv4_addresses"'
 筛选远程主机ip
ansible westos -m debug -a 'msg=hello'
调试“hello”输出信息
 vim test.yml
{
 - name: test
   hosts: westos
   tasks:
       - name: debug
         debug:
          var: ansible_all_ipv4_addresses  ----执行的输出作为变量传递到debug模块
          verbosity: 3                     ----debug优先级(默认是0)

 

}
ansible-playbook test.yml


 
ansible westos -m debug -a 'var="ansible_all_ipv4_addresses" '

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值