Ansible
2.模块的使用
authorized_key模块添加sshkey认证
YUM模块的使用
COPY模块的使用
file
cron
firewalld
pam_limits
mysql_db
mysql_user
selinux
sysctl
yum
3.playbooks
初始化新安装CentOS7
模块文档工具ansible-doc
shell+register+when+模块RETURN变量
本文档使用 MrDoc 发布
-
+
首页
shell+register+when+模块RETURN变量
# shell shell模块可以与register和when来结合使用,组合成类似于shell脚本中的 if [ $? -eq 0 ]这种条件判断,根据执行结果来执行playbook中的任务 ## 判断nginx配置文件是否正确 ```yaml - name: 启动nginx shell: "/usr/sbin/nginx -t" register: CONFIGTEST ignore_errors: true - name: 正确则重载nginx when: CONFIGTEST.rc == 0 shell: "nginx -s reload" ``` 从帮助文档来看,shell模块会返回很多种变量,rc只是其中的一个,其他包含类似time command的返回shell执行时间的delta变量;返回标准输出的stdout变量等,结合这些返回的变量可以有更多的高级组合操作。 ## 帮助文档 ``` 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 exist. shell: somescript.sh >> somelog.txt args: chdir: somedir/ creates: somelog.txt # You can also use the 'cmd' parameter instead of free form format. - name: This command will change the working directory to somedir/. shell: cmd: ls -l | grep log chdir: somedir/ - 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 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 # Disabling warnings - name: Using curl to connect to a host via SOCKS proxy (unsupported in uri). Ordinarily this would throw a warning. shell: curl --socks5 localhost:9000 http://www.ansible.com args: warn: no RETURN VALUES: msg: description: changed returned: always type: bool sample: True start: description: The command execution start time returned: always type: str sample: '2016-02-25 09:18:26.429568' end: description: The command execution end time returned: always type: str sample: '2016-02-25 09:18:26.755339' delta: description: The command execution delta time returned: always type: str sample: '0:00:00.325771' stdout: description: The command standard output returned: always type: str sample: 'Clustering node rabbit@slave1 with rabbit@master ...' stderr: description: The command standard error returned: always type: str sample: 'ls: cannot access foo: No such file or directory' cmd: description: The command executed by the task returned: always type: str 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 sample: [u'Clustering node rabbit@slave1 with rabbit@master ...'] ```
zhangky
2021年12月22日 11:02
分享文档
收藏文档
上一篇
下一篇
微信扫一扫
复制链接
手机扫一扫进行分享
复制链接
关于 MrDoc
觅思文档MrDoc
是
州的先生
开发并开源的在线文档系统,其适合作为个人和小型团队的云笔记、文档和知识库管理工具。
如果觅思文档给你或你的团队带来了帮助,欢迎对作者进行一些打赏捐助,这将有力支持作者持续投入精力更新和维护觅思文档,感谢你的捐助!
>>>捐助鸣谢列表
微信
支付宝
QQ
PayPal
Markdown文件
分享
链接
类型
密码
更新密码