标签:may directory esc rect 路径 cts fstab 设置 test
# 查看所有配置主机
ansible all --list-hosts
#查看tt主机组主机
ansible tt --list-hosts
[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
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‘"
#创建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‘
ansible 192.168.1.103 -m hostname -a ‘name=node103‘
# 设置名为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‘
ansible tt -m script -a ‘/usr/local/src/1.sh‘
# 示例: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
标签:may directory esc rect 路径 cts fstab 设置 test
原文地址:https://www.cnblogs.com/ly447742/p/14061411.html