7- awx에서 rest api 사용하기
awx에서 제공하는 restapi 사용방법
- 전체 기능은 : http://192.168.20.10:8801/api/v2/ 에 접속하면 전체 api 기능 확인가능
사전작업
- admin 계정으로 awx 로그인
- 관리 → application → 새로생성
- authorization grant type은 resource owner password=based,
- client type → confidential
- 관리 → user 에서 api용으로 사용할 서비스 계정 생성 후 token 생성
- application에는 2번에서 생성한 application 선택
- scope는 write로 선택
1.생성된 token값은 복사 (재 확인할 수 있는 절차가 없음)
API 사용예제
- inventory 조회
$ curl -X GET http://192.168.20.10:8801/api/v2/inventory/ \ --header 'Content-Type: application/json' \ --header 'Authorization: Bearer LQHjuUuJIn0jWjXLZ9LGVJEqXalvJM' | sed -e 's/[{}]/''/g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}'
- Credential 정보 조회
$ curl -X GET http://192.168.20.10:8801/api/v2/credentials/ \ --header 'Content-Type: application/json' \ --header 'Authorization: Bearer LQHjuUuJIn0jWjXLZ9LGVJEqXalvJM' | sed -e 's/[{}]/''/g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}'
- job templates조회
$ curl -X GET http://192.168.20.10:8801/api/v2/job_templates/ \ --header 'Content-Type: application/json' \ --header 'Authorization: Bearer LQHjuUuJIn0jWjXLZ9LGVJEqXalvJM' | sed -e 's/[{}]/''/g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}'
- job template 실행
$ curl -X POST http://192.168.20.10:8801/api/v2/job_templates/46/launch/ \ --header 'Content-Type: application/json' \ --header 'Authorization: Bearer LQHjuUuJIn0jWjXLZ9LGVJEqXalvJM' \ --data '{ "inventory": 2, "limit": "192.168.209.10", "credentials": [2], "extra_vars": { "MAX_JOB": 1, "SVC_USER": "user", "INSTALL_PATH": "/home/", "RUN_TYPE": "CMD", "RUN_CMD":"ls -l" } }'
- template ID가 46이고, inventory id가 2, credential id 가 2 인 job 수행
- 호스트 등록
$ curl -X POST http://192.168.20.10:8801/api/v2/inventories/2/hosts/ \ --header 'Content-Type: application/json' \ --header 'Authorization: Bearer LQHjuUuJIn0jWjXLZ9LGVJEqXalvJM' \ --data '{ "name": "192.168.200.10", "enabled": "true", "variables": "use: yes\ntest: no" }'
- inventory id가 2이고, 182.168.200.10에 use, test라는 변수를 적용해서 호스트 등록
- 그룹에 해당 호스트 추가
$ curl -X POST http://192.168.20.10:8801/api/v2/groups/6/hosts/ \ --header 'Content-Type: application/json' \ --header 'Authorization: Bearer LQHjuUuJIn0jWjXLZ9LGVJEqXalvJM' \ --data '{ "name": "192.168.200.10", "enabled": true }'
- group id가 6번인 그룹에 192.168.200.10 호스트 등록
- 호스트 삭제
$ curl -X POST http://192.168.20.10:8801/api/v2/inventories/2/hosts/ \ --header 'Content-Type: application/json' \ --header 'Authorization: Bearer LQHjuUuJIn0jWjXLZ9LGVJEqXalvJM' \ --data '{ "id": 75, "disassociate": "True" }'