微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ansible组件 Ad-Hoc

ad hoc ---临时的,在ansible里需要快速执行,并不用保存命令的执行方式

简单命令

playbook 复杂命令


EXAMPLES: - name: install the latest version of Apache yum: name: httpd state: latest - name: ensure a list of packages installed yum: name: "{{ packages }}" vars: packages: - httpd - httpd-tools - name: remove the Apache package yum: name: httpd state: absent - name: install the latest version of Apache from the testing repo yum: name: httpd enablerepo: testing state: present - name: install one specific version of Apache yum: name: httpd-2.2.29-1.4.amzn1 state: present - name: upgrade all packages yum: name: '*' state: latest - name: upgrade all packages, excluding kernel & foo related packages yum: name: '*' state: latest exclude: kernel*,foo* - name: install the Nginx rpm from a remote repo yum: name: http://Nginx.org/packages/centos/6/noarch/RPMS/Nginx-release-centos-6-0.el6.ngx.noarch.rpm state: present - name: install Nginx rpm from a local file yum: name: /usr/local/src/Nginx-release-centos-6-0.el6.ngx.noarch.rpm state: present - name: install the 'Development tools' package group yum: name: "@Development tools" state: present - name: install the 'Gnome desktop' environment group yum: name: "@^gnome-desktop-environment" state: present - name: List ansible packages and register result to print with debug later. yum: list: ansible register: result - name: Install package with multiple repos enabled yum: name: sos enablerepo: "epel,ol7_latest" - name: Install package with multiple repos disabled yum: name: sos disablerepo: "epel,ol7_latest" - name: Install a list of packages yum: name: - Nginx - postgresql - postgresql-server state: present - name: Download the Nginx package but do not install it yum: name: - Nginx state: latest download_only: true

  

 

EXAMPLES:

- name: example copying file with owner and permissions
  copy:
    src: /srv/myfiles/foo.conf
    dest: /etc/foo.conf
    owner: foo
    group: foo
    mode: 0644

- name: The same example as above, but using a symbolic mode equivalent to 0644
  copy:
    src: /srv/myfiles/foo.conf
    dest: /etc/foo.conf
    owner: foo
    group: foo
    mode: u=rw,g=r,o=r

- name: Another symbolic mode example, adding some permissions and removing others
  copy:
    src: /srv/myfiles/foo.conf
    dest: /etc/foo.conf
    owner: foo
    group: foo
    mode: u+rw,g-wx,o-rwx

- name: copy a new "ntp.conf file into place, backing up the original if it differs from the copied version
  copy:
    src: /mine/ntp.conf
    dest: /etc/ntp.conf
    owner: root
    group: root
    mode: 0644
    backup: yes

- name: copy a new "sudoers" file into place, after passing validation with visudo
  copy:
    src: /mine/sudoers
    dest: /etc/sudoers
    validate: /usr/sbin/visudo -cf %s

- name: copy a "sudoers" file on the remote machine for editing
  copy:
    src: /etc/sudoers
    dest: /etc/sudoers.edit
    remote_src: yes
    validate: /usr/sbin/visudo -cf %s

- name: copy using the 'content' for inline data
  copy:
    content: '# This file was moved to /etc/other.conf'
    dest: /etc/mine.conf'


RETURN VALUES:

dest:
    description: destination file/path
    returned: success
    type: string
    sample: /path/to/file.txt
src:
    description: source file used for the copy on the target machine
    returned: changed
    type: string
    sample: /home/httpd/.ansible/tmp/ansible-tmp-1423796390.97-147729857856000/source
md5sum:
    description: md5 checksum of the file after running copy
    returned: when supported
    type: string
    sample: 2a5aeecc61dc98c4d780b14b330e3282
checksum:
    description: sha1 checksum of the file after running copy
    returned: success
    type: string
    sample: 6e642bb8dd5c2e027bf21dd923337cbb4214f827
backup_file:
    description: name of backup file created
    returned: changed and if backup=yes
    type: string
    sample: /path/to/file.txt.2015-02-12@22:09~
gid:
    description: group id of the file, after execution
    returned: success
    type: int
    sample: 100
group:
    description: group of the file, after execution
    returned: success
    type: string
    sample: httpd
owner:
    description: owner of the file, after execution
    returned: success
    type: string
    sample: httpd
uid:
    description: owner id of the file, after execution
    returned: success
    type: int
    sample: 100
mode:
    description: permissions of the target, after execution
    returned: success
    type: string
    sample: 0644
size:
    description: size of the target, after execution
    returned: success
    type: int
    sample: 1220
state:
    description: state of the target, after execution
    returned: success
    type: string
    sample: file

 

 

 

 

[root@ftp:/root]
> ansible webservers -m shell -a 'uptime' -o 
ansible03 | CHANGED | rc=0 | (stdout)  17:34:22 up  4:12,  2 users,  load average: 0.00, 0.01, 0.05
ansible04 | CHANGED | rc=0 | (stdout)  17:34:22 up  3:45,  2 users,  load average: 0.16, 0.05, 0.05
ansible02 | CHANGED | rc=0 | (stdout)  17:34:22 up  3:41,  1 user,  load average: 0.00, 0.01, 0.05
ansible01 | CHANGED | rc=0 | (stdout)  17:34:22 up  4:11,  2 users,  load average: 0.00, 0.01, 0.05

[root@ftp:/root]
> ansible webservers -m shell -a 'uptime' -o -f 10
ansible03 | CHANGED | rc=0 | (stdout)  17:34:56 up  4:12,  2 users,  load average: 0.00, 0.01, 0.05
ansible01 | CHANGED | rc=0 | (stdout)  17:34:56 up  4:12,  2 users,  load average: 0.24, 0.06, 0.06
ansible04 | CHANGED | rc=0 | (stdout)  17:34:56 up  3:45,  2 users,  load average: 0.10, 0.04, 0.05
ansible02 | CHANGED | rc=0 | (stdout)  17:34:56 up  3:41,  1 user,  load average: 0.00, 0.01, 0.05

[root@ftp:/root]
>

 

 

[root@ftp:/root]
> ansible webservers -m copy -a 'src=/etc/hosts dest=/tmp/hosts owner=root group=root mode=777'
ansible04 | CHANGED => {
    "changed": true, 
    "checksum": "19aad245261f1772bffdabf3a6f5347312b20b46", 
    "dest": "/tmp/hosts", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "39c0b18a971b35e3888758a2494b6083", 
    "mode": "0777", 
    "owner": "root", 
    "size": 346, 
    "src": "/root/.ansible/tmp/ansible-tmp-1552815757.85-214840644098444/source", 
    "state": "file", 
    "uid": 0
}
ansible03 | CHANGED => {
    "changed": true, 
    "checksum": "19aad245261f1772bffdabf3a6f5347312b20b46", 
    "dest": "/tmp/hosts", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "39c0b18a971b35e3888758a2494b6083", 
    "mode": "0777", 
    "owner": "root", 
    "size": 346, 
    "src": "/root/.ansible/tmp/ansible-tmp-1552815757.82-188384435660718/source", 
    "state": "file", 
    "uid": 0
}
ansible01 | CHANGED => {
    "changed": true, 
    "checksum": "19aad245261f1772bffdabf3a6f5347312b20b46", 
    "dest": "/tmp/hosts", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "39c0b18a971b35e3888758a2494b6083", 
    "mode": "0777", 
    "owner": "root", 
    "size": 346, 
    "src": "/root/.ansible/tmp/ansible-tmp-1552815757.71-273021490189009/source", 
    "state": "file", 
    "uid": 0
}
ansible02 | CHANGED => {
    "changed": true, 
    "checksum": "19aad245261f1772bffdabf3a6f5347312b20b46", 
    "dest": "/tmp/hosts", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "39c0b18a971b35e3888758a2494b6083", 
    "mode": "0777", 
    "owner": "root", 
    "size": 346, 
    "src": "/root/.ansible/tmp/ansible-tmp-1552815757.77-7921352044895/source", 
    "state": "file", 
    "uid": 0
}

[root@ftp:/root]
> 

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐