码迷,mamicode.com
首页 > 其他好文 > 详细

# ad-hoc篇

时间:2020-12-04 11:29:42      阅读:5      评论:0      收藏:0      [点我收藏+]

标签:may   directory   esc   rect   路径   cts   fstab   设置   test   

1、--list-hosts

# 查看所有配置主机
ansible all --list-hosts
#查看tt主机组主机
ansible tt --list-hosts

2、shell模块

  • 语法: ansible [主机组] -m shell -a ‘[参数]‘
  • 注:复杂命令会有出错得可能,解决方法:写道脚本,copy到远程,执行,将结果拉回执行命令的服务器
  • 示例:
[root@node110 ~]# ansible tt -m shell -a ‘getent passwd nginx‘
192.168.1.103 | CHANGED | rc=0 >>
nginx:x:502:502::/home/nginx:/bin/nologin
192.168.1.104 | CHANGED | rc=0 >>
nginx:x:995:993:nginx user:/var/cache/nginx:/sbin/nologin

3、copy模块

  • 语法一:ansible [主机组] -m copy -a ‘src=[源文件|路径] dest=[目标文件|路径] owner=[用户,非必填] group=[属组,非必填] mode=[权限,非必填] backup=[yes|no,默认为no,非必填]‘
    语法二:ansible [主机组] -m copy -a "content=‘[文本]‘ dest=‘[目标文件|路径]‘"
  • 示例:
ansible 192.168.1.103 -m copy -a ‘src=/usr/local/src/1.sh dest=/usr/local/src/1.sh owner=node103 group=node103 mode=777 backup=yes‘
ansible 192.168.1.104 -m copy -a "content=‘Hello World\n‘ dest=‘/tmp/1.log‘"

4、file模块

  • 语法:ansible [主机组] -m file -a ‘name=[文件|路径] state=[动作] owner=[属主] mode=[权限,如755]‘
  • 示例:
#创建f3文件
ansible tt -m file -a ‘name=/tmp/f3 state=touch‘
#删除f3文件
ansible tt -m file -a ‘name=/tmp/f3 state=absent‘
#创建dir1目录
ansible tt -m file -a ‘name=/tmp/dir1 state=directory‘
#删除dir1目录
ansible tt -m file -a ‘name=/tmp/dir1 state=absent‘
#创建链接
ansible tt -m file -a ‘src=/etc/fstab name=/tmp/fstab.link state=link‘
#删除链接
ansible tt -m file -a ‘name=/tmp/fstab.link state=absent‘
#删除tmp目录
ansible tt -m file -a ‘name=/tmp/ state=absent‘

5、hostname模块

  • 语法:ansible [主机组] -m hostname -a ‘name=[主机名]‘
  • 注:修改主机名,c6系列配置放在/etc/sysconfig/network,c7系列放在/etc/hostname,同时修改/etc/hosts
  • 示例:
ansible 192.168.1.103 -m hostname -a ‘name=node103‘

6、cron模块

  • 示例:
# 设置名为warningcron的计划任务,执行时间每周一、三、五的12:00,执行脚本/usr/bin/wall FBI warning
ansible all -m cron -a ‘minute=00 hour=12 weekday=1,3,5 job="/usr/bin/wall FBI warning" name=warningcron‘
# 名为warningcron的计划任务置为无效
ansible all -m cron -a ‘disabled=yes job="/usr/bin/wall FBI warning" name=warningcron‘
# 恢复名为warningcron的计划任务
ansible all -m cron -a ‘disabled=no job="/usr/bin/wall FBI warning" name=warningcron‘
# 删除名为warningcron的计划任务
ansible all -m cron -a ‘job="/usr/bin/wall FBI warning" name=warningcron state=absent‘

7、script模块

  • 示例:
ansible tt -m script -a ‘/usr/local/src/1.sh‘

8、debug模块

  • debug模块的msg或var一般配合register输出信息
# 示例:register结合msg
[root@node110 yml]# cat 1.yml
---
- hosts: test
  remote_user: root
  tasks:
    - name: install pkg
      shell: df -h
      register: df_out
      when: ansible_default_ipv4.address == "192.168.1.103"
    - name: print df_out
      debug: msg={{ df_out.stdout_lines }}
[root@node110 yml]# ansible-playbook 1.yml 

PLAY [test] *********************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************
ok: [192.168.1.103]
ok: [192.168.1.104]

TASK [install pkg] **************************************************************************************************
skipping: [192.168.1.104]
changed: [192.168.1.103]

TASK [print df_out] *************************************************************************************************
ok: [192.168.1.103] => {
    "msg": [
        "Filesystem                    Size  Used Avail Use% Mounted on", 
        "/dev/mapper/VolGroup-lv_root   18G  2.1G   15G  13% /", 
        "tmpfs                         491M     0  491M   0% /dev/shm", 
        "/dev/sda1                     485M   34M  426M   8% /boot"
    ]
}
fatal: [192.168.1.104]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: ‘dict object‘ has no attribute ‘stdout_lines‘\n\nThe error appears to be in ‘/usr/local/src/ansible/yml/1.yml‘: line 9, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n      when: ansible_default_ipv4.address == \"192.168.1.103\"\n    - name: print df_out\n      ^ here\n"}

PLAY RECAP **********************************************************************************************************
192.168.1.103              : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.1.104              : ok=1    changed=0    unreachable=0    failed=1    skipped=1    rescued=0    ignored=0
# 示例:register结合var
[root@node110 yml]# cat 4.yml 
---
- hosts: test
  remote_user: root
  tasks:
    - name: install pkg
      shell: df -h
      register: df_out
      when: ansible_default_ipv4.address == "192.168.1.103"
    - name: print df_out
      debug: var=df_out.stdout_lines
[root@node110 yml]# ansible-playbook 4.yml 

PLAY [test] *********************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************
ok: [192.168.1.103]
ok: [192.168.1.104]

TASK [install pkg] **************************************************************************************************
skipping: [192.168.1.104]
changed: [192.168.1.103]

TASK [print df_out] *************************************************************************************************
ok: [192.168.1.103] => {
    "df_out.stdout_lines": [
        "Filesystem                    Size  Used Avail Use% Mounted on", 
        "/dev/mapper/VolGroup-lv_root   18G  2.1G   15G  13% /", 
        "tmpfs                         491M     0  491M   0% /dev/shm", 
        "/dev/sda1                     485M   34M  426M   8% /boot"
    ]
}
ok: [192.168.1.104] => {
    "df_out.stdout_lines": "VARIABLE IS NOT DEFINED!"
}

PLAY RECAP **********************************************************************************************************
192.168.1.103              : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.1.104              : ok=2    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0

# ad-hoc篇

标签:may   directory   esc   rect   路径   cts   fstab   设置   test   

原文地址:https://www.cnblogs.com/ly447742/p/14061411.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!