ansible相关模块

ansible-doc command 相关信息

[root@localhost ~]# ansible-doc command
> COMMAND    (/usr/lib/python2.7/site-packages/ansible-2.3.0-py2.7.egg/ansible/modules/commands/command.py)

  The `command' module takes the command name followed by a list of space-delimited arguments.
  The given command will be executed on all selected nodes. It will not be processed through the
  shell, so variables like `$HOME' and operations like `"<"', `">"', `"|"', `";"' and `"&"' will
  not work (use the [shell] module if you need these features).

Options (= is mandatory):

- chdir
        cd into this directory before running the command
        [Default: None]
- creates
        a filename or (since 2.0) glob pattern, when it already exists, this step will *not* be
        run.
        [Default: None]
- executable
        change the shell used to execute the command. Should be an absolute path to the
        executable.
        [Default: None]
= free_form
        the command module takes a free form command to run.  There is no parameter actually
        named 'free form'. See the examples!
        [Default: None]
- removes
        a filename or (since 2.0) glob pattern, when it does not exist, this step will *not* be
        run.
        [Default: None]
- warn
        if command warnings are on in ansible.cfg, do not warn about this particular line if set
        to no/false.
        [Default: True]
Notes:
  * If you want to run a command through the shell (say you are using `<', `>', `|', etc),
        you actually want the [shell] module instead. The `command' module is much more
        secure as it's not affected by the user's environment.
  *  `creates', `removes', and `chdir' can be specified after the command. For instance,
        if you only want to run a command if a certain file does not exist, use this.
EXAMPLES:
- name: return motd to registered var
  command: cat /etc/motd
  register: mymotd

- name: Run the command if the specified file does not exist.
  command: /usr/bin/make_database.sh arg1 arg2 creates=/path/to/database

# You can also use the 'args' form to provide the options.
- name: This command will change the working directory to somedir/ and will only run when /path/to/database doesn't exis  command: /usr/bin/make_database.sh arg1 arg2
  args:
    chdir: somedir/
    creates: /path/to/database

- name: safely use templated variable to run command. Always use the quote filter to avoid injection issues.
  command: cat {{ myfile|quote }}
  register: myoutput


MAINTAINERS: Ansible Core Team, Michael DeHaan

METADATA:
        Status: ['stableinterface']
        Version: 1.0
        Supported_by: core

这里command模块是基础模块,而此模块的参数有chdir、creates、executable、register等


command 是基础模块 在ad-hoc模式下使用:

[root@localhost ~]# ansible moppo -m command -a 'date'
192.168.1.3 | SUCCESS | rc=0 >>
Thu Apr 13 16:08:58 CST 2017


[root@localhost ~]# ansible moppo -m command -a 'ls /var/www/html/'
192.168.1.3 | SUCCESS | rc=0 >>
advert
webkit
webkit.tar.gz

这里对shell 模块做另外介绍,因为脚本的执行是工作中必不可少的

EXAMPLES:
- name: Execute the command in remote shell; stdout goes to the specified file on the remote.
  shell: somescript.sh >> somelog.txt

- name: Change the working directory to somedir/ before executing the command.
  shell: somescript.sh >> somelog.txt
  args:
    chdir: somedir/

# You can also use the 'args' form to provide the options.
- name: This command will change the working directory to somedir/ and will only run when somedir/somelog.txt doesn't ex  shell: somescript.sh >> somelog.txt
  args:
    chdir: somedir/
    creates: somelog.txt

- name: Run a command that uses non-posix shell-isms (in this example /bin/sh doesn't handle redirection and wildcards t  shell: cat < /tmp/*txt
  args:
    executable: /bin/bash

- name: Run a command using a templated variable (always use quote filter to avoid injection)
  shell: cat {{ myfile|quote }}

# You can use shell to run other executables to perform actions inline
- name: Run expect to wait for a successful PXE boot via out-of-band CIMC
  shell: |
    set timeout 300
    spawn ssh admin@{{ cimc_host }}

    expect "password:"
    send "{{ cimc_password }}\n"

    expect "\n{{ cimc_name }}"
    send "connect host\n"

    expect "pxeboot.n12"
    send "\n"

    exit 0
  args:
    executable: /usr/bin/expect
  delegate_to: localhost

RETURN VALUES:
msg:
    description: changed
    returned: always
    type: boolean
    sample: True
start:
    description: The command execution start time
    returned: always
    type: string
    sample: '2016-02-25 09:18:26.429568'
end:
    description: The command execution end time
    returned: always
    type: string
    sample: '2016-02-25 09:18:26.755339'
delta:
    description: The command execution delta time
    returned: always
    type: string
    sample: '0:00:00.325771'
stdout:
    description: The command standard output
    returned: always
    type: string
    sample: 'Clustering node rabbit@slave1 with rabbit@master ...'
stderr:
    description: The command standard error
    returned: always
    type: string
    sample: 'ls: cannot access foo: No such file or directory'
cmd:
    description: The command executed by the task
    returned: always
    type: string
    sample: 'rabbitmqctl join_cluster rabbit@master'
rc:
    description: The command return code (0 means success)
    returned: always
    type: int
    sample: 0
stdout_lines:
    description: The command standard output split in lines
    returned: always
    type: list of strings
    sample: [u'Clustering node rabbit@slave1 with rabbit@master ...']


MAINTAINERS: Ansible Core Team, Michael DeHaan

METADATA:
        Status: ['stableinterface']
        Version: 1.0
        Supported_by: core

关于ansbile工具的shell、command、script、raw模块的区别和使用场景


 1. command模块 [执行远程命令]
ansible moppo -m command -a 'uname -n'
 2. script模块 [在远程主机执行主控端的shell/python脚本 ]  (使用相对路径)
ansible moppo -m script -a '/etc/ansible/test.sh
 3. shell模块 [执行远程主机的shell/python脚本]
ansible moppo -m shell -a 'bash /root/test.sh'
 4. raw模块 [类似于command模块、支持管道传递]
ansible moppo -m raw -a "ifconfig eth0 |sed -n 2p |awk '{print \$2}' |awk -F: '{print \$2}'"

在后续文章更新 自动化部署服务,并版本迭代等

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值