This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
ansible:ansible_cmd [2021/11/12 00:57] manu |
ansible:ansible_cmd [2024/04/19 12:26] (current) manu [All ansible commands] |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Ansible command examples ====== | ====== Ansible command examples ====== | ||
| + | |||
| + | ===== All ansible commands ===== | ||
| + | |||
| + | https://docs.ansible.com/ansible/latest/command_guide/command_line_tools.html | ||
| + | |||
| + | **ansible-rulebook** : start a playbook based on a specific event | ||
| + | https://blog.stephane-robert.info/post/ansible-event-driven/ | ||
| + | |||
| + | **ansible-pull** : opposite to ansible is based on the push mode, offering a central management, the pull mode can be use in specific cases | ||
| + | https://blog.octo.com/ansible-pull-killer-feature | ||
| + | | ||
| + | **ansible-galaxy** : get collections of playbooks | ||
| + | |||
| + | **ansible** : Define and run a single task ‘playbook’ against a set of hosts | ||
| + | |||
| + | **ansible-playbook** : Define and run a single task ‘playbook’ against a set of hosts | ||
| + | |||
| + | **ansible-config** : View ansible configuration | ||
| + | |||
| + | **ansible-console** : REPL console for executing Ansible tasks | ||
| + | |||
| + | **ansible-doc** : plugin documentation tool | ||
| + | |||
| + | **ansible-galaxy** : Perform various Role and Collection related operations | ||
| + | |||
| + | **ansible-inventory** : Show Ansible inventory information, by default it uses the inventory script JSON format | ||
| + | |||
| + | **ansible-vault** : encryption/decryption utility for Ansible data files | ||
| + | |||
| + | **ansible-lint** : check playbook syntax | ||
| ===== cmd ===== | ===== cmd ===== | ||
| - | <cli> | + | <cli prompt='#'> |
| - | ansible all -m ping | + | # ansible all -m ping |
| - | ansible foo.example.com -m yum -a "name=httpd state=installed" | + | # ansible -vvv -i "node1," all -u user1 -m ping # debug |
| - | ansible -i hosts all -m yum -a 'name=ncdu state=present' | + | # ansible foo.example.com -m yum -a "name=httpd state=installed" |
| - | ansible -i hosts all -m yum -a 'name=ncdu state=absent' | + | # ansible -i hosts all -m yum -a 'name=ncdu state=present' |
| - | ansible foo.example.com -a "/usr/sbin/reboot" | + | # ansible -i hosts all -m yum -a 'name=ncdu state=absent' |
| + | # ansible foo.example.com -a "/usr/sbin/reboot" | ||
| + | # ansible aix1 -m script -a "./myscript.sh" | ||
| </cli> | </cli> | ||
| + | Commands modules for **ansible** | ||
| + | command | ||
| + | expect | ||
| + | psexec (for windows) | ||
| + | raw | ||
| + | script | ||
| + | shell | ||
| + | telnet | ||
| + | | ||
| + | |||
| Only way to pass a command on target without python installed | Only way to pass a command on target without python installed | ||
| <cli prompt='#'> | <cli prompt='#'> | ||
| Line 35: | Line 78: | ||
| aix01 | SUCCESS => {"changed": false,"ping": "pong"} | aix01 | SUCCESS => {"changed": false,"ping": "pong"} | ||
| aix20 | SUCCESS => {"changed": false,"ping": "pong"} | aix20 | SUCCESS => {"changed": false,"ping": "pong"} | ||
| + | </cli> | ||
| + | |||
| + | <cli prompt='$'> | ||
| + | $ ANSIBLE_REMOTE_USER=user01 ansible all -k -K -m ping | ||
| + | SSH password: | ||
| + | BECOME password[defaults to SSH password]: | ||
| + | 172.16.120.155 | FAILED! => { | ||
| + | "msg": "Using a SSH password instead of a key is not | ||
| + | possible because Host Key checking is enabled and sshpass | ||
| + | does not support this. Please add this host's fingerprint | ||
| + | to your known_hosts file to manage this host." | ||
| + | } | ||
| + | 172.16.120.122 | FAILED! => { | ||
| + | "msg": "Using a SSH password instead of a key is not | ||
| + | possible because Host Key checking is enabled and sshpass | ||
| + | does not support this. Please add this host's fingerprint | ||
| + | to your known_hosts file to manage this host." | ||
| + | } | ||
| + | 172.16.120.123 | SUCCESS => { | ||
| + | "ansible_facts": { | ||
| + | "discovered_interpreter_python": "/usr/libexec/ | ||
| + | platform-python" | ||
| + | }, | ||
| + | "changed": false, | ||
| + | "ping": "pong" | ||
| + | } | ||
| + | </cli> | ||
| + | |||
| + | |||
| + | |||
| + | File conversion: | ||
| + | <cli prompt='#'> | ||
| + | [root@srv1]/usr/bdl/ansible# ansible-inventory -i inventory/hosts --list | ||
| + | { | ||
| + | "ALL": { | ||
| + | "children": [ | ||
| + | "cluster", | ||
| + | "test1" | ||
| + | ] | ||
| + | }, | ||
| + | "_meta": { | ||
| + | "hostvars": {} | ||
| + | }, | ||
| + | "all": { | ||
| + | "children": [ | ||
| + | "ALL", | ||
| + | "empty", | ||
| + | "managedClients", | ||
| + | "ungrouped" | ||
| + | ] | ||
| + | }, | ||
| + | "cluster": { | ||
| + | "hosts": [ | ||
| + | "cl1labo", | ||
| + | "cltest" | ||
| + | ] | ||
| + | }, | ||
| + | "test1": { | ||
| + | "hosts": [ | ||
| + | "laboh" | ||
| + | ] | ||
| + | } | ||
| + | } | ||
| + | </cli> | ||
| + | |||
| + | <cli prompt='#'> | ||
| + | [root@srv1]/usr/bdl/ansible# ansible-inventory -i inventory/hosts --list --yaml | ||
| + | all: | ||
| + | children: | ||
| + | ALL: | ||
| + | children: | ||
| + | cluster: | ||
| + | hosts: | ||
| + | cl1labo: {} | ||
| + | cltest: {} | ||
| + | test1: | ||
| + | hosts: | ||
| + | laboh: {} | ||
| + | empty: {} | ||
| + | managedClients: {} | ||
| + | ungrouped: {} | ||
| </cli> | </cli> | ||
| Line 268: | Line 392: | ||
| persistent: yes | persistent: yes | ||
| when: ansible_selinux.status == 'enabled' | when: ansible_selinux.status == 'enabled' | ||
| + | </cli> | ||
| + | |||
| + | ==== change /etc/rc.tcpip ==== | ||
| + | |||
| + | <cli prompt='#'> | ||
| + | # cat update_rc_tcpip.yml | ||
| + | - name: Update /etc/rc.tcpip | ||
| + | gather_facts: no | ||
| + | hosts: ALL | ||
| + | |||
| + | ######################################################################################################### | ||
| + | # Latest version date 06/10/2022 | ||
| + | ######################################################################################################### | ||
| + | # Playbook will comment line "sendmail ..." | ||
| + | ######################################################################################################### | ||
| + | |||
| + | tasks: | ||
| + | |||
| + | - name: Comment line begining with time_last_login | ||
| + | ansible.builtin.lineinfile: | ||
| + | path: /etc/rc.tcpip | ||
| + | regexp: '^start /usr/lib/sendmail' | ||
| + | line: '#start /usr/lib/sendmail "$src_running" "-bd -q${qpi}"' | ||
| + | backup: yes | ||
| + | firstmatch: yes | ||
| + | backrefs: yes | ||
| + | register: results | ||
| + | |||
| + | - name: Restart tcpip demon group if file modified | ||
| + | shell: "/usr/sbin/refresh -g tcpip" | ||
| + | when: results.changed == true | ||
| </cli> | </cli> | ||