User Tools

Site Tools


ansible:ansible_cmd

This is an old revision of the document!


Ansible command examples

cmd

ansible all -m ping 
ansible foo.example.com -m yum -a "name=httpd state=installed"
ansible -i hosts all -m yum -a 'name=ncdu state=present'
ansible -i hosts all -m yum -a 'name=ncdu state=absent'
ansible foo.example.com -a "/usr/sbin/reboot"

Only way to pass a command on target without python installed

# ansible myhost --become -m raw -a "yum -y install python"
ansible-inventory --list
ansible-inventory -i ./sample.yml --graph
ansible-config dump --only-changed
ansible -m module_name -a attributes
ansible-config dump
ansible-config list
ansible-config view
# ansible all -m ping --one-line
lnx80 | SUCCESS => {"changed": false,"ping": "pong"}
lnx81 | SUCCESS => {"changed": false,"ping": "pong"}
aix01 | SUCCESS => {"changed": false,"ping": "pong"}
aix20 | SUCCESS => {"changed": false,"ping": "pong"}

playbook

Copy file

# cat copy_file.yml
---
 - hosts: app
   tasks:
     - name: Fetch the file from the mwiapp01 to master
       run_once: yes
       fetch: src=/tmp/app01-to-app02.jar dest=buffer/ flat=yes
       when: "{{ inventory_hostname == 'mwiapp01' }}"

     - name: Copy the file from master to mwiapp02
       copy: src=buffer/app01-to-app02.jar dest=/tmp/
       when: "{{ inventory_hostname == 'mwiapp02' }}"

Firewalld

# cat fiwalld.yml
---
 - name: FirewallD
   hosts: localhost
   connection: local
   tasks:
    - name: FirewallD rules
      firewalld:
        permanent: yes
        immediate: yes
        service: "{{ item }}"
        state: enabled
      with_items:
       - http
       - https
   tasks:
    - name: FirewallD rules
      firewalld:
        permanent: yes
        immediate: yes
        port: "{{item.port}}/{{item.proto}}"
        state: "{{item.state}}"
        zone: "{{item.zone}}"
      with_items:
       - {port: "8080", proto: "tcp", state: "disabled", zone: "public" }
       - {port: "161-162", proto: "udp", state: "disabled", zone: "internal" }
       - {port: "9001", proto: "tcp", state: "enabled", zone: "public" }
   tasks:
    - name: FirewallD rules
      firewalld:
        permanent: yes
        immediate: yes
        rich_rule: "{{ item }}"
        state: enabled
      with_items:
       - 'rule service name="ftp" audit limit value="1/m" accept'
       - 'rule service name="http" audit limit value="1/m" drop'
   tasks:
    - name: FirewallD rules
      firewalld:
        permanent: yes
        immediate: yes
        rich_rule: "{{ item }}"
        state: enabled
      with_items:
       - 'rule forward-port port=8080 protocol=tcp to-port=80 family=ipv4'
ansible/ansible_cmd.1636553481.txt.gz · Last modified: 2021/11/10 15:11 by manu