a filename, when it already exists, this step will not be run.
removes
a filename, when it does not exist, this step will not be run.
### 返回结果
![](https://box.kancloud.cn/d47ca564dff3cabd930636b630c4be33_606x261.png)
### 命令行使用格式
ansible anserver -m command -a 'hostname'
ansible anserver -m shell -a 'hostname'
### 脚本的远程执行
先使用copy模块复制脚本到远程客户端,再使用shell模块执行
# ansible anserver -m shell -a '/tmp/test.sh'
若文件无执行权限,则可使用以下
# ansible anserver -m shell -a '/bin/bash /tmp/test.sh'
### 在playbook中使用格式
- name: shell and command test
hosts: s.hi.com
tasks:
- name: shell test
shell: hostname
- name: command test
command: hostname
### 补充
当不指定模块时,默认使用`command`模块
`ansible hostname -a 'hostname'`
## 示例
# 使用args来执行一些选项
- name: This command will change the working directory to somedir/ and will only run when somedir/somelog.txt doesn't exist.
shell: somescript.sh >> somelog.txt
args:
chdir: somedir/
creates: somelog.txt
executable: /bin/bash
# /bin/sh不能同时处理重定向和通配符操作,使用/bin/bash可以
- name: Run a command that uses non-posix shell-isms (in this example /bin/sh doesn't handle redirection and wildcards together but bash does)
shell: cat < /tmp/*txt
args:
executable: /bin/bash
# 在另一个解释器下执行
- 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
[1]:http://docs.ansible.com/ansible/shell_module.html
ansible基础
ansible简介
ansible安装和测试
ansible配置文件
yaml在ansible中的用法
inventory
变量与facts
when语句
handler模块
ansible模块
assert 模块
copy模块
cron模块
debug模块
django_manage模块
file模块
filesystem模块
git模块
hostname模块
lineinfile模块
mount模块
mysql_user模块
mysql_db模块
pip模块
selinux
setup模块
shell 和 command 模块
stat模块
supervisorctl
systemd
timezone
unarchive模块
user模块
wait_for
yum和service模块
其他模块或者方法
setup模块
url模块
slack 模块
pause 模块
playbooks
复杂的playbook
roles
jinja2